From ec58d8638eb74a98b082293a6d9121992330e417 Mon Sep 17 00:00:00 2001 From: Matt Soucy Date: Tue, 5 Nov 2019 23:16:16 -0500 Subject: [PATCH] Through stage 5 --- core/src/me/msoucy/ptures/screens/GameScreen.kt | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/core/src/me/msoucy/ptures/screens/GameScreen.kt b/core/src/me/msoucy/ptures/screens/GameScreen.kt index 0c63bd9..a620c5a 100644 --- a/core/src/me/msoucy/ptures/screens/GameScreen.kt +++ b/core/src/me/msoucy/ptures/screens/GameScreen.kt @@ -9,6 +9,8 @@ import com.badlogic.gdx.math.Vector3 import com.badlogic.gdx.utils.Array import com.badlogic.gdx.utils.TimeUtils import ktx.app.KtxScreen +import ktx.assets.pool +import ktx.assets.invoke import ktx.collections.iterate import ktx.graphics.use import ktx.log.logger @@ -28,12 +30,13 @@ class GameScreen(val game: PTures) : KtxScreen { private val camera = OrthographicCamera().apply { setToOrtho(false, 800f, 480f) } private val bucket = Rectangle(800f / 2f - 64f / 2f, 20f, 64f, 64f) private val touchPos = Vector3() - private val raindrops = Array() // gdx, not Kotlin Array + private val raindropsPool = pool { Rectangle() } + private val activeRaindrops = Array() // gdx, not Kotlin Array private var lastDropTime = 0L private var dropsGathered = 0 private fun spawnRaindrop() { - raindrops.add(Rectangle(MathUtils.random(0f, 800f - 64f), 480f, 64f, 64f)) + activeRaindrops.add(raindropsPool().set(MathUtils.random(0f, 800f - 64f), 480f, 64f, 64f)) lastDropTime = TimeUtils.nanoTime() } @@ -48,7 +51,7 @@ class GameScreen(val game: PTures) : KtxScreen { game.batch.use { batch -> game.font.draw(batch, "Drops Collected: " + dropsGathered, 0f, 480f) batch.draw(bucketImage, bucket.x, bucket.y, bucket.width, bucket.height) - raindrops.forEach { r -> batch.draw(dropImage, r.x, r.y) } + activeRaindrops.forEach { r -> batch.draw(dropImage, r.x, r.y) } } // process user input @@ -76,17 +79,20 @@ class GameScreen(val game: PTures) : KtxScreen { // 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 // effect also - raindrops.iterate { raindrop, iterator -> + activeRaindrops.iterate { raindrop, iterator -> raindrop.y -= 200 * delta if (raindrop.y + 64 < 0) { iterator.remove() - log.debug { "Missed a raindrop!" } + raindropsPool(raindrop) + log.debug { "Missed a raindrop! Pool free objects: ${raindropsPool.free}" } } if (raindrop.overlaps(bucket)) { dropsGathered++ dropSound.play() iterator.remove() + raindropsPool(raindrop) + log.debug { "Pool free objects: ${raindropsPool.free}" } } } }