Through stage 2
This commit is contained in:
		@@ -104,5 +104,7 @@ project(":core") {
 | 
				
			|||||||
        api "com.badlogicgames.ashley:ashley:$ashleyVersion"
 | 
					        api "com.badlogicgames.ashley:ashley:$ashleyVersion"
 | 
				
			||||||
        api "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
 | 
					        api "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
 | 
				
			||||||
        api "io.github.libktx:ktx-app:$ktxVersion"
 | 
					        api "io.github.libktx:ktx-app:$ktxVersion"
 | 
				
			||||||
 | 
					        api "io.github.libktx:ktx-collections:$ktxVersion"
 | 
				
			||||||
 | 
					        api "io.github.libktx:ktx-graphics:$ktxVersion"
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,34 +1,25 @@
 | 
				
			|||||||
package me.msoucy.ptures
 | 
					package me.msoucy.ptures
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.badlogic.gdx.Game
 | 
					 | 
				
			||||||
import com.badlogic.gdx.Gdx
 | 
					 | 
				
			||||||
import com.badlogic.gdx.graphics.GL20
 | 
					 | 
				
			||||||
import com.badlogic.gdx.graphics.Texture
 | 
					 | 
				
			||||||
import com.badlogic.gdx.graphics.g2d.BitmapFont
 | 
					import com.badlogic.gdx.graphics.g2d.BitmapFont
 | 
				
			||||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch
 | 
					import com.badlogic.gdx.graphics.g2d.SpriteBatch
 | 
				
			||||||
import ktx.app.KtxGame
 | 
					import ktx.app.KtxGame
 | 
				
			||||||
import ktx.app.KtxScreen
 | 
					import ktx.app.KtxScreen
 | 
				
			||||||
import me.msoucy.ptures.screens.MainMenuScreen
 | 
					import me.msoucy.ptures.screens.MainMenuScreen
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class PTures : Game() {
 | 
					class PTures : KtxGame<KtxScreen>() {
 | 
				
			||||||
    public lateinit var batch: SpriteBatch
 | 
					    val batch by lazy { SpriteBatch() }
 | 
				
			||||||
    public lateinit var font: BitmapFont
 | 
					    // use LibGDX's default Arial font
 | 
				
			||||||
 | 
					    val font by lazy { BitmapFont() }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    override fun create() {
 | 
					    override fun create() {
 | 
				
			||||||
        batch = SpriteBatch()
 | 
					        addScreen(MainMenuScreen(this))
 | 
				
			||||||
        // use LibGDX's default Arial font
 | 
					        setScreen<MainMenuScreen>()
 | 
				
			||||||
        font = BitmapFont()
 | 
					        super.create()
 | 
				
			||||||
        this.setScreen(MainMenuScreen(this))
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    override fun render() {
 | 
					 | 
				
			||||||
        super.render()  // important!
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    override fun dispose() {
 | 
					    override fun dispose() {
 | 
				
			||||||
        this.getScreen().dispose()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        batch.dispose()
 | 
					        batch.dispose()
 | 
				
			||||||
        font.dispose()
 | 
					        font.dispose()
 | 
				
			||||||
 | 
					        super.dispose()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,12 +1,7 @@
 | 
				
			|||||||
package me.msoucy.ptures.screens
 | 
					package me.msoucy.ptures.screens
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.badlogic.gdx.Game
 | 
					 | 
				
			||||||
import com.badlogic.gdx.Gdx
 | 
					import com.badlogic.gdx.Gdx
 | 
				
			||||||
import com.badlogic.gdx.Input
 | 
					import com.badlogic.gdx.Input
 | 
				
			||||||
import com.badlogic.gdx.Screen
 | 
					 | 
				
			||||||
import com.badlogic.gdx.audio.Music
 | 
					 | 
				
			||||||
import com.badlogic.gdx.audio.Sound
 | 
					 | 
				
			||||||
import com.badlogic.gdx.graphics.GL20
 | 
					 | 
				
			||||||
import com.badlogic.gdx.graphics.OrthographicCamera
 | 
					import com.badlogic.gdx.graphics.OrthographicCamera
 | 
				
			||||||
import com.badlogic.gdx.graphics.Texture
 | 
					import com.badlogic.gdx.graphics.Texture
 | 
				
			||||||
import com.badlogic.gdx.math.MathUtils
 | 
					import com.badlogic.gdx.math.MathUtils
 | 
				
			||||||
@@ -14,142 +9,87 @@ import com.badlogic.gdx.math.Rectangle
 | 
				
			|||||||
import com.badlogic.gdx.math.Vector3
 | 
					import com.badlogic.gdx.math.Vector3
 | 
				
			||||||
import com.badlogic.gdx.utils.Array
 | 
					import com.badlogic.gdx.utils.Array
 | 
				
			||||||
import com.badlogic.gdx.utils.TimeUtils
 | 
					import com.badlogic.gdx.utils.TimeUtils
 | 
				
			||||||
 | 
					import ktx.app.KtxScreen
 | 
				
			||||||
 | 
					import ktx.collections.iterate
 | 
				
			||||||
 | 
					import ktx.graphics.use
 | 
				
			||||||
import me.msoucy.ptures.PTures
 | 
					import me.msoucy.ptures.PTures
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class GameScreen(val game: PTures) : Screen {
 | 
					class GameScreen(val game: PTures) : KtxScreen {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private var dropImage: Texture
 | 
					    private val dropImage = Texture(Gdx.files.internal("images/drop.png"))
 | 
				
			||||||
    private var bucketImage: Texture
 | 
					    private val bucketImage = Texture(Gdx.files.internal("images/bucket.png"))
 | 
				
			||||||
    private var dropSound: Sound
 | 
					    private val dropSound = Gdx.audio.newSound(Gdx.files.internal("sounds/drop.wav"))
 | 
				
			||||||
    private var rainMusic: Music
 | 
					    private val rainMusic = Gdx.audio.newMusic(Gdx.files.internal("music/rain.mp3")).apply { isLooping = true }
 | 
				
			||||||
    // The camera ensures we can render using our target resolution of 800x480
 | 
					    // The camera ensures we can render using our target resolution of 800x480
 | 
				
			||||||
    //    pixels no matter what the screen resolution is.
 | 
					    //    pixels no matter what the screen resolution is.
 | 
				
			||||||
    private var camera: OrthographicCamera
 | 
					    private val camera = OrthographicCamera().apply { setToOrtho(false, 800f, 480f) }
 | 
				
			||||||
    private var bucket: Rectangle
 | 
					    private val bucket = Rectangle(800f / 2f - 64f / 2f, 20f, 64f, 64f)
 | 
				
			||||||
    private var touchPos: Vector3
 | 
					    private val touchPos = Vector3()
 | 
				
			||||||
    private var raindrops: Array<Rectangle> // gdx, not Kotlin Array
 | 
					    private val raindrops = Array<Rectangle>() // gdx, not Kotlin Array
 | 
				
			||||||
    private var lastDropTime: Long = 0L
 | 
					    private var lastDropTime = 0L
 | 
				
			||||||
    private var dropsGathered: Int = 0
 | 
					    private var dropsGathered = 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private fun spawnRaindrop() {
 | 
					    private fun spawnRaindrop() {
 | 
				
			||||||
        var raindrop = Rectangle()
 | 
					        raindrops.add(Rectangle(MathUtils.random(0f, 800f - 64f), 480f, 64f, 64f))
 | 
				
			||||||
        raindrop.x = MathUtils.random(0f, 800f - 64f)
 | 
					 | 
				
			||||||
        raindrop.y = 480f
 | 
					 | 
				
			||||||
        raindrop.width = 64f
 | 
					 | 
				
			||||||
        raindrop.height = 64f
 | 
					 | 
				
			||||||
        raindrops.add(raindrop)
 | 
					 | 
				
			||||||
        lastDropTime = TimeUtils.nanoTime()
 | 
					        lastDropTime = TimeUtils.nanoTime()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // initializer block
 | 
					 | 
				
			||||||
    init {
 | 
					 | 
				
			||||||
        // load the images for the droplet & bucket, 64x64 pixels each
 | 
					 | 
				
			||||||
        dropImage = Texture(Gdx.files.internal("images/drop.png"))
 | 
					 | 
				
			||||||
        bucketImage = Texture(Gdx.files.internal("images/bucket.png"))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // load the drop sound effect and the rain background music
 | 
					 | 
				
			||||||
        dropSound = Gdx.audio.newSound(Gdx.files.internal("sounds/drop.wav"))
 | 
					 | 
				
			||||||
        rainMusic = Gdx.audio.newMusic(Gdx.files.internal("music/rain.mp3"))
 | 
					 | 
				
			||||||
        rainMusic.setLooping(true)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // create the camera
 | 
					 | 
				
			||||||
        camera = OrthographicCamera()
 | 
					 | 
				
			||||||
        camera.setToOrtho(false, 800f, 480f)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // create a Rectangle to logically represent the bucket
 | 
					 | 
				
			||||||
        bucket = Rectangle()
 | 
					 | 
				
			||||||
        bucket.x = 800f / 2f - 64f / 2f  // center the bucket horizontally
 | 
					 | 
				
			||||||
        bucket.y = 20f               // bottom left bucket corner is 20px above
 | 
					 | 
				
			||||||
        //    bottom screen edge
 | 
					 | 
				
			||||||
        bucket.width = 64f
 | 
					 | 
				
			||||||
        bucket.height = 64f
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // create the touchPos to store mouse click position
 | 
					 | 
				
			||||||
        touchPos = Vector3()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // create the raindrops array and spawn the first raindrop
 | 
					 | 
				
			||||||
        raindrops = Array<Rectangle>()
 | 
					 | 
				
			||||||
        spawnRaindrop()
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    override fun render(delta: Float) {
 | 
					    override fun render(delta: Float) {
 | 
				
			||||||
        // clear the screen with a dark blue color. The arguments to glClearColor
 | 
					 | 
				
			||||||
        //    are the RGB and alpha component in the range [0,1] of the color to
 | 
					 | 
				
			||||||
        //    be used to clear the screen.
 | 
					 | 
				
			||||||
        Gdx.gl.glClearColor(0f, 0f, 0.2f, 1f)
 | 
					 | 
				
			||||||
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // generally good practice to update the camera's matrices once per frame
 | 
					        // generally good practice to update the camera's matrices once per frame
 | 
				
			||||||
        camera.update()
 | 
					        camera.update()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // tell the SpriteBatch to render in the coordinate system specified by the camera.
 | 
					        // tell the SpriteBatch to render in the coordinate system specified by the camera.
 | 
				
			||||||
        game.batch.setProjectionMatrix(camera.combined)
 | 
					        game.batch.projectionMatrix = camera.combined
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // begin a new batch and draw the bucket and all drops
 | 
					        // begin a new batch and draw the bucket and all drops
 | 
				
			||||||
        game.batch.begin()
 | 
					        game.batch.use { batch ->
 | 
				
			||||||
        game.font.draw(game.batch, "Drops Collected: " + dropsGathered, 0f, 480f)
 | 
					            game.font.draw(batch, "Drops Collected: " + dropsGathered, 0f, 480f)
 | 
				
			||||||
        game.batch.draw(bucketImage, bucket.x, bucket.y,
 | 
					            batch.draw(bucketImage, bucket.x, bucket.y, bucket.width, bucket.height)
 | 
				
			||||||
                bucket.width, bucket.height)
 | 
					            raindrops.forEach { r -> batch.draw(dropImage, r.x, r.y) }
 | 
				
			||||||
        for (raindrop in raindrops) {
 | 
					 | 
				
			||||||
            game.batch.draw(dropImage, raindrop.x, raindrop.y)
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        game.batch.end()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // process user input
 | 
					        // process user input
 | 
				
			||||||
        if (Gdx.input.isTouched()) {
 | 
					        if (Gdx.input.isTouched) {
 | 
				
			||||||
            touchPos.set(Gdx.input.getX().toFloat(),
 | 
					            touchPos.set(Gdx.input.x.toFloat(), Gdx.input.y.toFloat(),0f)
 | 
				
			||||||
                    Gdx.input.getY().toFloat(),
 | 
					 | 
				
			||||||
                    0f)
 | 
					 | 
				
			||||||
            camera.unproject(touchPos)
 | 
					            camera.unproject(touchPos)
 | 
				
			||||||
            bucket.x = touchPos.x - 64f / 2f
 | 
					            bucket.x = touchPos.x - 64f / 2f
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
 | 
					        if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
 | 
				
			||||||
            // getDeltaTime returns the time passed between the last and the current frame in seconds
 | 
					            // getDeltaTime returns the time passed between the last and the current frame in seconds
 | 
				
			||||||
            bucket.x -= 200 * Gdx.graphics.getDeltaTime()
 | 
					            bucket.x -= 200 * delta
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
 | 
					        if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
 | 
				
			||||||
            bucket.x += 200 * Gdx.graphics.getDeltaTime()
 | 
					            bucket.x += 200 * delta
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // make sure the bucket stays within the screen bounds
 | 
					        // make sure the bucket stays within the screen bounds
 | 
				
			||||||
        if (bucket.x < 0f)
 | 
					        bucket.x = MathUtils.clamp(bucket.x, 0f, 800f - 64f)
 | 
				
			||||||
            bucket.x = 0f
 | 
					 | 
				
			||||||
        if (bucket.x > 800f - 64f)
 | 
					 | 
				
			||||||
            bucket.x = 800f - 64f
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // check if we need to create a new raindrop
 | 
					        // check if we need to create a new raindrop
 | 
				
			||||||
        if (TimeUtils.nanoTime() - lastDropTime > 1_000_000_000L)
 | 
					        if (TimeUtils.nanoTime() - lastDropTime > 1_000_000_000L) {
 | 
				
			||||||
            spawnRaindrop()
 | 
					            spawnRaindrop()
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // move the raindrops, remove any that are beneath the bottom edge of the
 | 
					        // move the raindrops, remove any that are beneath the bottom edge of the
 | 
				
			||||||
        //    screen or that hit the bucket.  In the latter case, play back a sound
 | 
					        //    screen or that hit the bucket.  In the latter case, play back a sound
 | 
				
			||||||
        //    effect also
 | 
					        //    effect also
 | 
				
			||||||
        var iter = raindrops.iterator()
 | 
					        raindrops.iterate { raindrop, iterator ->
 | 
				
			||||||
        while (iter.hasNext()) {
 | 
					            raindrop.y -= 200 * delta
 | 
				
			||||||
            var raindrop = iter.next()
 | 
					 | 
				
			||||||
            raindrop.y -= 200 * Gdx.graphics.getDeltaTime()
 | 
					 | 
				
			||||||
            if (raindrop.y + 64 < 0)
 | 
					            if (raindrop.y + 64 < 0)
 | 
				
			||||||
                iter.remove()
 | 
					                iterator.remove()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (raindrop.overlaps(bucket)) {
 | 
					            if (raindrop.overlaps(bucket)) {
 | 
				
			||||||
                dropsGathered++
 | 
					                dropsGathered++
 | 
				
			||||||
                dropSound.play()
 | 
					                dropSound.play()
 | 
				
			||||||
                iter.remove()
 | 
					                iterator.remove()
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // the following overrides are no-ops, unused in tutorial, but needed in
 | 
					 | 
				
			||||||
    //    order to compile a class that implements Screen
 | 
					 | 
				
			||||||
    override fun resize(width: Int, height: Int) {}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    override fun hide() {}
 | 
					 | 
				
			||||||
    override fun pause() {}
 | 
					 | 
				
			||||||
    override fun resume() {}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    override fun show() {
 | 
					    override fun show() {
 | 
				
			||||||
        // start the playback of the background music when the screen is shown
 | 
					        // start the playback of the background music when the screen is shown
 | 
				
			||||||
        rainMusic.play()
 | 
					        rainMusic.play()
 | 
				
			||||||
 | 
					        spawnRaindrop()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    override fun dispose() {
 | 
					    override fun dispose() {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,52 +1,30 @@
 | 
				
			|||||||
package me.msoucy.ptures.screens
 | 
					package me.msoucy.ptures.screens
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.badlogic.gdx.Gdx
 | 
					import com.badlogic.gdx.Gdx
 | 
				
			||||||
import com.badlogic.gdx.Screen
 | 
					 | 
				
			||||||
import com.badlogic.gdx.graphics.GL20
 | 
					 | 
				
			||||||
import com.badlogic.gdx.graphics.OrthographicCamera
 | 
					import com.badlogic.gdx.graphics.OrthographicCamera
 | 
				
			||||||
 | 
					import ktx.app.KtxScreen
 | 
				
			||||||
 | 
					import ktx.graphics.use
 | 
				
			||||||
import me.msoucy.ptures.PTures
 | 
					import me.msoucy.ptures.PTures
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class MainMenuScreen(val game: PTures) : Screen {
 | 
					class MainMenuScreen(val game: PTures) : KtxScreen {
 | 
				
			||||||
    private var camera: OrthographicCamera
 | 
					    private val camera = OrthographicCamera().apply {
 | 
				
			||||||
 | 
					        setToOrtho(false, 800f, 480f)
 | 
				
			||||||
    init {
 | 
					 | 
				
			||||||
        camera = OrthographicCamera();
 | 
					 | 
				
			||||||
        camera.setToOrtho(false, 800f, 480f);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    override fun render(delta: Float) {
 | 
					    override fun render(delta: Float) {
 | 
				
			||||||
        Gdx.gl.glClearColor(0f, 0f, 0.2f, 1f);
 | 
					        camera.update()
 | 
				
			||||||
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
 | 
					        game.batch.projectionMatrix = camera.combined
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        camera.update();
 | 
					        game.batch.use {
 | 
				
			||||||
        game.batch.setProjectionMatrix(camera.combined);
 | 
					            game.font.draw(it, "Welcome to Drop!!! ", 100f, 150f)
 | 
				
			||||||
 | 
					            game.font.draw(it, "Tap anywhere to begin!", 100f, 100f)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        game.batch.begin();
 | 
					        if (Gdx.input.isTouched) {
 | 
				
			||||||
        game.font.draw(game.batch, "Welcome to Drop!!! ", 100f, 150f);
 | 
					            game.addScreen(GameScreen(game))
 | 
				
			||||||
        game.font.draw(game.batch, "Tap anywhere to begin!", 100f, 100f);
 | 
					            game.setScreen<GameScreen>()
 | 
				
			||||||
        game.batch.end();
 | 
					            game.removeScreen<MainMenuScreen>()
 | 
				
			||||||
 | 
					            dispose()
 | 
				
			||||||
        if (Gdx.input.isTouched()) {
 | 
					 | 
				
			||||||
            game.setScreen(GameScreen(game));
 | 
					 | 
				
			||||||
            dispose();
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
    override fun hide() {
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    override fun show() {
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    override fun pause() {
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    override fun resume() {
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    override fun resize(width: Int, height: Int) {
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    override fun dispose() {
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user