Through stage 4
This commit is contained in:
		
							
								
								
									
										32
									
								
								core/src/me/msoucy/ptures/EAssets.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								core/src/me/msoucy/ptures/EAssets.kt
									
									
									
									
									
										Normal file
									
								
							@@ -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<Sound>(asset.path)
 | 
			
		||||
inline operator fun AssetManager.get(asset: SoundAssets) = getAsset<Sound>(asset.path)
 | 
			
		||||
 | 
			
		||||
// music
 | 
			
		||||
enum class MusicAssets(val path: String) {
 | 
			
		||||
    Rain("music/rain.mp3")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline fun AssetManager.load(asset: MusicAssets) = load<Music>(asset.path)
 | 
			
		||||
inline operator fun AssetManager.get(asset: MusicAssets) = getAsset<Music>(asset.path)
 | 
			
		||||
 | 
			
		||||
// texture atlas
 | 
			
		||||
enum class TextureAtlasAssets(val path: String) {
 | 
			
		||||
    Game("images/game.atlas")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline fun AssetManager.load(asset: TextureAtlasAssets) = load<TextureAtlas>(asset.path)
 | 
			
		||||
inline operator fun AssetManager.get(asset: TextureAtlasAssets) = getAsset<TextureAtlas>(asset.path)
 | 
			
		||||
@@ -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<KtxScreen>() {
 | 
			
		||||
    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<MainMenuScreen>()
 | 
			
		||||
        addScreen(LoadingScreen(this))
 | 
			
		||||
        setScreen<LoadingScreen>()
 | 
			
		||||
        super.create()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    override fun dispose() {
 | 
			
		||||
        batch.dispose()
 | 
			
		||||
        font.dispose()
 | 
			
		||||
        assets.dispose()
 | 
			
		||||
        super.dispose()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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<GameScreen>()
 | 
			
		||||
 | 
			
		||||
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()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										43
									
								
								core/src/me/msoucy/ptures/screens/LoadingScreen.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								core/src/me/msoucy/ptures/screens/LoadingScreen.kt
									
									
									
									
									
										Normal file
									
								
							@@ -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<GameScreen>()
 | 
			
		||||
            game.removeScreen<LoadingScreen>()
 | 
			
		||||
            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) }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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<GameScreen>()
 | 
			
		||||
            game.removeScreen<MainMenuScreen>()
 | 
			
		||||
            dispose()
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user