Through stage 5

This commit is contained in:
Matt Soucy 2019-11-05 23:16:16 -05:00
parent 37691d64ba
commit ec58d8638e

View File

@ -9,6 +9,8 @@ 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.app.KtxScreen
import ktx.assets.pool
import ktx.assets.invoke
import ktx.collections.iterate import ktx.collections.iterate
import ktx.graphics.use import ktx.graphics.use
import ktx.log.logger 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 camera = OrthographicCamera().apply { setToOrtho(false, 800f, 480f) }
private val bucket = Rectangle(800f / 2f - 64f / 2f, 20f, 64f, 64f) private val bucket = Rectangle(800f / 2f - 64f / 2f, 20f, 64f, 64f)
private val touchPos = Vector3() private val touchPos = Vector3()
private val raindrops = Array<Rectangle>() // gdx, not Kotlin Array private val raindropsPool = pool { Rectangle() }
private val activeRaindrops = Array<Rectangle>() // gdx, not Kotlin Array
private var lastDropTime = 0L private var lastDropTime = 0L
private var dropsGathered = 0 private var dropsGathered = 0
private fun spawnRaindrop() { 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() lastDropTime = TimeUtils.nanoTime()
} }
@ -48,7 +51,7 @@ class GameScreen(val game: PTures) : KtxScreen {
game.batch.use { batch -> game.batch.use { batch ->
game.font.draw(batch, "Drops Collected: " + dropsGathered, 0f, 480f) game.font.draw(batch, "Drops Collected: " + dropsGathered, 0f, 480f)
batch.draw(bucketImage, bucket.x, bucket.y, bucket.width, bucket.height) 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 // 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 // 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
raindrops.iterate { raindrop, iterator -> activeRaindrops.iterate { raindrop, iterator ->
raindrop.y -= 200 * delta raindrop.y -= 200 * delta
if (raindrop.y + 64 < 0) { if (raindrop.y + 64 < 0) {
iterator.remove() iterator.remove()
log.debug { "Missed a raindrop!" } raindropsPool(raindrop)
log.debug { "Missed a raindrop! Pool free objects: ${raindropsPool.free}" }
} }
if (raindrop.overlaps(bucket)) { if (raindrop.overlaps(bucket)) {
dropsGathered++ dropsGathered++
dropSound.play() dropSound.play()
iterator.remove() iterator.remove()
raindropsPool(raindrop)
log.debug { "Pool free objects: ${raindropsPool.free}" }
} }
} }
} }