From 645dd2e90cfda3a196ee81eda67d72958a4fd051 Mon Sep 17 00:00:00 2001 From: Matt Soucy Date: Tue, 5 Nov 2019 23:25:22 -0500 Subject: [PATCH] Through stage 6 --- build.gradle | 1 + core/src/me/msoucy/ptures/PTures.kt | 20 ++++++---- .../me/msoucy/ptures/screens/GameScreen.kt | 23 +++++++----- .../me/msoucy/ptures/screens/LoadingScreen.kt | 37 ++++++++++--------- 4 files changed, 48 insertions(+), 33 deletions(-) diff --git a/build.gradle b/build.gradle index 3b7f460..f3722a2 100644 --- a/build.gradle +++ b/build.gradle @@ -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" } } diff --git a/core/src/me/msoucy/ptures/PTures.kt b/core/src/me/msoucy/ptures/PTures.kt index 0d52a5b..629ad8a 100644 --- a/core/src/me/msoucy/ptures/PTures.kt +++ b/core/src/me/msoucy/ptures/PTures.kt @@ -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() { - 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(SpriteBatch()) + bindSingleton(BitmapFont()) + bindSingleton(AssetManager()) + bindSingleton(OrthographicCamera().apply { setToOrtho(false, 800f, 480f) }) + addScreen(LoadingScreen(inject(), inject(), inject(), inject(), inject())) + } setScreen() super.create() } override fun dispose() { - batch.dispose() - font.dispose() - assets.dispose() + context.dispose() super.dispose() } } diff --git a/core/src/me/msoucy/ptures/screens/GameScreen.kt b/core/src/me/msoucy/ptures/screens/GameScreen.kt index a620c5a..13665b5 100644 --- a/core/src/me/msoucy/ptures/screens/GameScreen.kt +++ b/core/src/me/msoucy/ptures/screens/GameScreen.kt @@ -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() -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) } } diff --git a/core/src/me/msoucy/ptures/screens/LoadingScreen.kt b/core/src/me/msoucy/ptures/screens/LoadingScreen.kt index 99fb926..ced7655 100644 --- a/core/src/me/msoucy/ptures/screens/LoadingScreen.kt +++ b/core/src/me/msoucy/ptures/screens/LoadingScreen.kt @@ -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() game.removeScreen() 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) } } } \ No newline at end of file