Through stage 6

This commit is contained in:
Matt Soucy 2019-11-05 23:25:22 -05:00
parent ec58d8638e
commit 645dd2e90c
4 changed files with 48 additions and 33 deletions

View File

@ -107,6 +107,7 @@ project(":core") {
api "io.github.libktx:ktx-assets:$ktxVersion"
api "io.github.libktx:ktx-collections:$ktxVersion"
api "io.github.libktx:ktx-graphics:$ktxVersion"
api "io.github.libktx:ktx-inject:$ktxVersion"
api "io.github.libktx:ktx-log:$ktxVersion"
}
}

View File

@ -1,27 +1,33 @@
package me.msoucy.ptures
import com.badlogic.gdx.assets.AssetManager
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.g2d.Batch
import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import ktx.app.KtxGame
import ktx.app.KtxScreen
import ktx.inject.Context
import me.msoucy.ptures.screens.LoadingScreen
class PTures : KtxGame<KtxScreen>() {
val batch by lazy { SpriteBatch() }
val font by lazy { BitmapFont() }
val assets = AssetManager()
private val context = Context()
override fun create() {
addScreen(LoadingScreen(this))
context.register {
bindSingleton(this@PTures)
bindSingleton<Batch>(SpriteBatch())
bindSingleton(BitmapFont())
bindSingleton(AssetManager())
bindSingleton(OrthographicCamera().apply { setToOrtho(false, 800f, 480f) })
addScreen(LoadingScreen(inject(), inject(), inject(), inject(), inject()))
}
setScreen<LoadingScreen>()
super.create()
}
override fun dispose() {
batch.dispose()
font.dispose()
assets.dispose()
context.dispose()
super.dispose()
}
}

View File

@ -2,7 +2,10 @@ package me.msoucy.ptures.screens
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.assets.AssetManager
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.g2d.Batch
import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.math.MathUtils
import com.badlogic.gdx.math.Rectangle
import com.badlogic.gdx.math.Vector3
@ -18,16 +21,18 @@ import me.msoucy.ptures.*
private val log = logger<GameScreen>()
class GameScreen(val game: PTures) : KtxScreen {
class GameScreen(private val batch: Batch,
private val font: BitmapFont,
assets: AssetManager,
private val camera: OrthographicCamera) : KtxScreen {
private val dropImage = game.assets[TextureAtlasAssets.Game].findRegion("drop")
private val bucketImage = game.assets[TextureAtlasAssets.Game].findRegion("bucket")
private val dropSound = game.assets[SoundAssets.Drop]
private val rainMusic = game.assets[MusicAssets.Rain].apply { isLooping = true }
private val dropImage = assets[TextureAtlasAssets.Game].findRegion("drop")
private val bucketImage = assets[TextureAtlasAssets.Game].findRegion("bucket")
private val dropSound = assets[SoundAssets.Drop]
private val rainMusic = assets[MusicAssets.Rain].apply { isLooping = true }
// The camera ensures we can render using our target resolution of 800x480
// pixels no matter what the screen resolution is.
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 raindropsPool = pool { Rectangle() }
@ -45,11 +50,11 @@ class GameScreen(val game: PTures) : KtxScreen {
camera.update()
// tell the SpriteBatch to render in the coordinate system specified by the camera.
game.batch.projectionMatrix = camera.combined
batch.projectionMatrix = camera.combined
// begin a new batch and draw the bucket and all drops
game.batch.use { batch ->
game.font.draw(batch, "Drops Collected: " + dropsGathered, 0f, 480f)
batch.use { batch ->
font.draw(batch, "Drops Collected: " + dropsGathered, 0f, 480f)
batch.draw(bucketImage, bucket.x, bucket.y, bucket.width, bucket.height)
activeRaindrops.forEach { r -> batch.draw(dropImage, r.x, r.y) }
}

View File

@ -1,34 +1,37 @@
package me.msoucy.ptures.screens
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.assets.AssetManager
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.g2d.Batch
import com.badlogic.gdx.graphics.g2d.BitmapFont
import ktx.app.KtxScreen
import ktx.graphics.use
import me.msoucy.ptures.*
class LoadingScreen(val game: PTures) : KtxScreen {
private val camera = OrthographicCamera().apply {
setToOrtho(false, 800f, 480f)
}
class LoadingScreen(private val game: PTures,
private val batch: Batch,
private val font: BitmapFont,
private val assets: AssetManager,
private val camera: OrthographicCamera) : KtxScreen {
override fun render(delta: Float) {
// Continue loading assets
game.assets.update()
assets.update()
camera.update()
game.batch.projectionMatrix = camera.combined
batch.projectionMatrix = camera.combined
game.batch.use {
game.font.draw(it, "Welcome to Drop!!! ", 100f, 150f)
if (game.assets.isFinished) {
game.font.draw(it, "Tap anywhere to begin!", 100f, 100f)
batch.use {
font.draw(it, "Welcome to Drop!!! ", 100f, 150f)
if (assets.isFinished) {
font.draw(it, "Tap anywhere to begin!", 100f, 100f)
} else {
game.font.draw(it, "Loading assets...", 100f, 100f)
font.draw(it, "Loading assets...", 100f, 100f)
}
}
if (Gdx.input.isTouched && game.assets.isFinished) {
game.addScreen(GameScreen(game))
if (Gdx.input.isTouched && assets.isFinished) {
game.addScreen(GameScreen(batch, font, assets, camera))
game.setScreen<GameScreen>()
game.removeScreen<LoadingScreen>()
dispose()
@ -36,8 +39,8 @@ class LoadingScreen(val game: PTures) : KtxScreen {
}
override fun show() {
MusicAssets.values().forEach { game.assets.load(it) }
SoundAssets.values().forEach { game.assets.load(it) }
TextureAtlasAssets.values().forEach { game.assets.load(it) }
MusicAssets.values().forEach { assets.load(it) }
SoundAssets.values().forEach { assets.load(it) }
TextureAtlasAssets.values().forEach { assets.load(it) }
}
}