diff --git a/android/assets/images/bucket.png b/android/assets/images/bucket.png deleted file mode 100644 index 0538dc3..0000000 Binary files a/android/assets/images/bucket.png and /dev/null differ diff --git a/android/assets/images/drop.png b/android/assets/images/drop.png deleted file mode 100644 index 8ce41f4..0000000 Binary files a/android/assets/images/drop.png and /dev/null differ diff --git a/android/assets/images/game.atlas b/android/assets/images/game.atlas new file mode 100644 index 0000000..2f31d8f --- /dev/null +++ b/android/assets/images/game.atlas @@ -0,0 +1,20 @@ + +game.png +size: 256,128 +format: RGBA8888 +filter: Nearest,Nearest +repeat: none +bucket + rotate: false + xy: 2, 2 + size: 64, 64 + orig: 64, 64 + offset: 0, 0 + index: -1 +drop + rotate: false + xy: 70, 2 + size: 64, 64 + orig: 64, 64 + offset: 0, 0 + index: -1 \ No newline at end of file diff --git a/android/assets/images/game.png b/android/assets/images/game.png new file mode 100644 index 0000000..7dc8361 Binary files /dev/null and b/android/assets/images/game.png differ diff --git a/build.gradle b/build.gradle index f2c7aef..3b7f460 100644 --- a/build.gradle +++ b/build.gradle @@ -104,7 +104,9 @@ project(":core") { api "com.badlogicgames.ashley:ashley:$ashleyVersion" api "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion" api "io.github.libktx:ktx-app:$ktxVersion" + 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-log:$ktxVersion" } } diff --git a/core/src/me/msoucy/ptures/EAssets.kt b/core/src/me/msoucy/ptures/EAssets.kt new file mode 100644 index 0000000..c601f82 --- /dev/null +++ b/core/src/me/msoucy/ptures/EAssets.kt @@ -0,0 +1,32 @@ +package me.msoucy.ptures + +import com.badlogic.gdx.assets.AssetManager +import com.badlogic.gdx.audio.Music +import com.badlogic.gdx.audio.Sound +import com.badlogic.gdx.graphics.g2d.TextureAtlas +import ktx.assets.getAsset +import ktx.assets.load + +// sounds +enum class SoundAssets(val path: String) { + Drop("sounds/drop.wav") +} + +inline fun AssetManager.load(asset: SoundAssets) = load(asset.path) +inline operator fun AssetManager.get(asset: SoundAssets) = getAsset(asset.path) + +// music +enum class MusicAssets(val path: String) { + Rain("music/rain.mp3") +} + +inline fun AssetManager.load(asset: MusicAssets) = load(asset.path) +inline operator fun AssetManager.get(asset: MusicAssets) = getAsset(asset.path) + +// texture atlas +enum class TextureAtlasAssets(val path: String) { + Game("images/game.atlas") +} + +inline fun AssetManager.load(asset: TextureAtlasAssets) = load(asset.path) +inline operator fun AssetManager.get(asset: TextureAtlasAssets) = getAsset(asset.path) \ No newline at end of file diff --git a/core/src/me/msoucy/ptures/PTures.kt b/core/src/me/msoucy/ptures/PTures.kt index 79d6978..0d52a5b 100644 --- a/core/src/me/msoucy/ptures/PTures.kt +++ b/core/src/me/msoucy/ptures/PTures.kt @@ -1,25 +1,27 @@ package me.msoucy.ptures +import com.badlogic.gdx.assets.AssetManager import com.badlogic.gdx.graphics.g2d.BitmapFont import com.badlogic.gdx.graphics.g2d.SpriteBatch import ktx.app.KtxGame import ktx.app.KtxScreen -import me.msoucy.ptures.screens.MainMenuScreen +import me.msoucy.ptures.screens.LoadingScreen class PTures : KtxGame() { val batch by lazy { SpriteBatch() } - // use LibGDX's default Arial font val font by lazy { BitmapFont() } + val assets = AssetManager() override fun create() { - addScreen(MainMenuScreen(this)) - setScreen() + addScreen(LoadingScreen(this)) + setScreen() super.create() } override fun dispose() { batch.dispose() font.dispose() + assets.dispose() super.dispose() } } diff --git a/core/src/me/msoucy/ptures/screens/GameScreen.kt b/core/src/me/msoucy/ptures/screens/GameScreen.kt index e508657..0c63bd9 100644 --- a/core/src/me/msoucy/ptures/screens/GameScreen.kt +++ b/core/src/me/msoucy/ptures/screens/GameScreen.kt @@ -3,7 +3,6 @@ package me.msoucy.ptures.screens import com.badlogic.gdx.Gdx import com.badlogic.gdx.Input import com.badlogic.gdx.graphics.OrthographicCamera -import com.badlogic.gdx.graphics.Texture import com.badlogic.gdx.math.MathUtils import com.badlogic.gdx.math.Rectangle import com.badlogic.gdx.math.Vector3 @@ -12,14 +11,18 @@ import com.badlogic.gdx.utils.TimeUtils import ktx.app.KtxScreen import ktx.collections.iterate import ktx.graphics.use -import me.msoucy.ptures.PTures +import ktx.log.logger +import me.msoucy.ptures.* + +private val log = logger() class GameScreen(val game: PTures) : KtxScreen { - private val dropImage = Texture(Gdx.files.internal("images/drop.png")) - private val bucketImage = Texture(Gdx.files.internal("images/bucket.png")) - private val dropSound = Gdx.audio.newSound(Gdx.files.internal("sounds/drop.wav")) - private val rainMusic = Gdx.audio.newMusic(Gdx.files.internal("music/rain.mp3")).apply { isLooping = true } + 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 } + // 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) } @@ -75,8 +78,10 @@ class GameScreen(val game: PTures) : KtxScreen { // effect also raindrops.iterate { raindrop, iterator -> raindrop.y -= 200 * delta - if (raindrop.y + 64 < 0) + if (raindrop.y + 64 < 0) { iterator.remove() + log.debug { "Missed a raindrop!" } + } if (raindrop.overlaps(bucket)) { dropsGathered++ @@ -91,11 +96,4 @@ class GameScreen(val game: PTures) : KtxScreen { rainMusic.play() spawnRaindrop() } - - override fun dispose() { - dropImage.dispose() - bucketImage.dispose() - dropSound.dispose() - rainMusic.dispose() - } } \ No newline at end of file diff --git a/core/src/me/msoucy/ptures/screens/LoadingScreen.kt b/core/src/me/msoucy/ptures/screens/LoadingScreen.kt new file mode 100644 index 0000000..99fb926 --- /dev/null +++ b/core/src/me/msoucy/ptures/screens/LoadingScreen.kt @@ -0,0 +1,43 @@ +package me.msoucy.ptures.screens + +import com.badlogic.gdx.Gdx +import com.badlogic.gdx.graphics.OrthographicCamera +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) + } + + override fun render(delta: Float) { + // Continue loading assets + game.assets.update() + + camera.update() + game.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) + } else { + game.font.draw(it, "Loading assets...", 100f, 100f) + } + } + + if (Gdx.input.isTouched && game.assets.isFinished) { + game.addScreen(GameScreen(game)) + game.setScreen() + game.removeScreen() + dispose() + } + } + + override fun show() { + MusicAssets.values().forEach { game.assets.load(it) } + SoundAssets.values().forEach { game.assets.load(it) } + TextureAtlasAssets.values().forEach { game.assets.load(it) } + } +} \ No newline at end of file diff --git a/core/src/me/msoucy/ptures/screens/MainMenuScreen.kt b/core/src/me/msoucy/ptures/screens/MainMenuScreen.kt deleted file mode 100644 index 2b21f57..0000000 --- a/core/src/me/msoucy/ptures/screens/MainMenuScreen.kt +++ /dev/null @@ -1,30 +0,0 @@ -package me.msoucy.ptures.screens - -import com.badlogic.gdx.Gdx -import com.badlogic.gdx.graphics.OrthographicCamera -import ktx.app.KtxScreen -import ktx.graphics.use -import me.msoucy.ptures.PTures - -class MainMenuScreen(val game: PTures) : KtxScreen { - private val camera = OrthographicCamera().apply { - setToOrtho(false, 800f, 480f) - } - - override fun render(delta: Float) { - camera.update() - game.batch.projectionMatrix = camera.combined - - game.batch.use { - game.font.draw(it, "Welcome to Drop!!! ", 100f, 150f) - game.font.draw(it, "Tap anywhere to begin!", 100f, 100f) - } - - if (Gdx.input.isTouched) { - game.addScreen(GameScreen(game)) - game.setScreen() - game.removeScreen() - dispose() - } - } -} \ No newline at end of file diff --git a/desktop/src/me/msoucy/ptures/desktop/DesktopLauncher.kt b/desktop/src/me/msoucy/ptures/desktop/DesktopLauncher.kt index 2fac773..bdeaa00 100644 --- a/desktop/src/me/msoucy/ptures/desktop/DesktopLauncher.kt +++ b/desktop/src/me/msoucy/ptures/desktop/DesktopLauncher.kt @@ -1,5 +1,6 @@ package me.msoucy.ptures.desktop +import com.badlogic.gdx.Application import com.badlogic.gdx.backends.lwjgl.LwjglApplication import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration import me.msoucy.ptures.PTures @@ -7,7 +8,11 @@ import me.msoucy.ptures.PTures object DesktopLauncher { @JvmStatic fun main(arg: Array) { - val config = LwjglApplicationConfiguration() - LwjglApplication(PTures(), config) + val config = LwjglApplicationConfiguration().apply { + title = "Drop" + width = 800 + height = 480 + } + LwjglApplication(PTures(), config).logLevel = Application.LOG_DEBUG } }