diff --git a/assets/sounds/effects/steps_indoor.mp3 b/assets/sounds/effects/steps_indoor.mp3 new file mode 100644 index 0000000..4badd24 Binary files /dev/null and b/assets/sounds/effects/steps_indoor.mp3 differ diff --git a/assets/sounds/music/world_music.mp3 b/assets/sounds/music/world_music.mp3 new file mode 100644 index 0000000..b575097 Binary files /dev/null and b/assets/sounds/music/world_music.mp3 differ diff --git a/core/src/main/kotlin/com/last/commit/FirstScreen.kt b/core/src/main/kotlin/com/last/commit/FirstScreen.kt index eaae4c7..b0f608b 100644 --- a/core/src/main/kotlin/com/last/commit/FirstScreen.kt +++ b/core/src/main/kotlin/com/last/commit/FirstScreen.kt @@ -1,5 +1,6 @@ package com.last.commit +import GameState import com.badlogic.gdx.Gdx import com.badlogic.gdx.Input.Keys import com.badlogic.gdx.InputProcessor @@ -15,12 +16,14 @@ import com.badlogic.gdx.math.MathUtils import com.badlogic.gdx.math.Vector2 import com.badlogic.gdx.math.Vector3 import com.badlogic.gdx.utils.Json +import com.last.commit.config.ActionCommand import com.last.commit.config.GameConfig import com.last.commit.map.Interactable import com.last.commit.map.TimeMap import com.last.commit.stages.InventoryStage +import com.last.commit.audio.GameSoundEffect +import com.last.commit.audio.GameMusic import kotlin.math.floor -import GameState /** First screen of the application. Displayed after the application is created. */ class FirstScreen(val gameState: GameState) : Screen, InputProcessor { @@ -58,6 +61,7 @@ class FirstScreen(val gameState: GameState) : Screen, InputProcessor { shapeRenderer.setAutoShapeType(true) Gdx.input.setInputProcessor(this) + gameState.soundEngine.play(GameMusic.WORLD_MUSIC, 0.5f) } fun loadGameConfig(): GameConfig { @@ -245,26 +249,25 @@ class FirstScreen(val gameState: GameState) : Screen, InputProcessor { } override fun keyUp(keycode: Int): Boolean { - if (keycode == Keys.ESCAPE) { + if (gameState.settings.getAction(keycode) == ActionCommand.OPEN_MENU) { Gdx.app.exit() } return false } override fun keyTyped(character: Char): Boolean { - val keyCode = character.code + val characterUpperCase = character.uppercase() + val characterKey = Keys.valueOf(characterUpperCase) - if (gameState.settings.isInteractPressed(keyCode)) { + if (gameState.settings.getAction(characterKey) == ActionCommand.INTERACT) { openDoor() - } else if (gameState.settings.isTimeTravelPressed(keyCode)) { + } else if (gameState.settings.getAction(characterKey) == ActionCommand.TIME_TRAVEL) { map.teleport(player) - } else if (gameState.settings.isOpenInventoryPressed(keyCode)) { + } else if (gameState.settings.getAction(characterKey) == ActionCommand.OPEN_INVENTORY) { inventoryStage.visible = !inventoryStage.visible } else if (character == 'p') { gameState.inventory.add("compass") inventoryStage.refresh() - } else if (gameState.settings.isInteractPressed(keyCode)) { - this.openDoor() } return false } diff --git a/core/src/main/kotlin/com/last/commit/Game.kt b/core/src/main/kotlin/com/last/commit/Game.kt index 1c5501f..852e442 100644 --- a/core/src/main/kotlin/com/last/commit/Game.kt +++ b/core/src/main/kotlin/com/last/commit/Game.kt @@ -1,9 +1,10 @@ package com.last.commit +import GameState import com.badlogic.gdx.Game +import com.last.commit.config.GameSettings import com.last.commit.inventory.Inventory -import com.last.commit.config.PlayerSettings -import GameState +import com.last.commit.audio.SoundEngine /** [com.badlogic.gdx.ApplicationListener] implementation shared by all platforms. */ class Game : Game() { @@ -18,7 +19,8 @@ class Game : Game() { fun createState() { state = GameState( Inventory(), - PlayerSettings() + GameSettings(), + SoundEngine() ) } } \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/GameState.kt b/core/src/main/kotlin/com/last/commit/GameState.kt index e276981..96c0650 100644 --- a/core/src/main/kotlin/com/last/commit/GameState.kt +++ b/core/src/main/kotlin/com/last/commit/GameState.kt @@ -1,7 +1,9 @@ import com.last.commit.inventory.Inventory -import com.last.commit.config.PlayerSettings +import com.last.commit.config.GameSettings +import com.last.commit.audio.SoundEngine -public data class GameState( - public val inventory: Inventory, - public val settings: PlayerSettings, +data class GameState( + val inventory: Inventory, + val settings: GameSettings, + public val soundEngine: SoundEngine, ) \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/Player.kt b/core/src/main/kotlin/com/last/commit/Player.kt index 1f39d08..32cda19 100644 --- a/core/src/main/kotlin/com/last/commit/Player.kt +++ b/core/src/main/kotlin/com/last/commit/Player.kt @@ -5,6 +5,7 @@ import com.badlogic.gdx.graphics.g2d.TextureRegion import com.badlogic.gdx.math.Rectangle import com.badlogic.gdx.math.Vector2 import com.last.commit.inventory.Inventory +import com.last.commit.audio.GameSoundEffect import GameState diff --git a/core/src/main/kotlin/com/last/commit/audio/GameMusic.kt b/core/src/main/kotlin/com/last/commit/audio/GameMusic.kt new file mode 100644 index 0000000..81b915a --- /dev/null +++ b/core/src/main/kotlin/com/last/commit/audio/GameMusic.kt @@ -0,0 +1,9 @@ +package com.last.commit.audio + +import com.last.commit.audio.GameSound + +class GameMusic(name: String): GameSound(name) { + companion object { + val WORLD_MUSIC = GameMusic("world_music.mp3") + } +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/audio/GameSound.kt b/core/src/main/kotlin/com/last/commit/audio/GameSound.kt new file mode 100644 index 0000000..4cb33b9 --- /dev/null +++ b/core/src/main/kotlin/com/last/commit/audio/GameSound.kt @@ -0,0 +1,5 @@ + +package com.last.commit.audio; + +public abstract class GameSound(public val name: String) { +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/audio/GameSoundEffect.kt b/core/src/main/kotlin/com/last/commit/audio/GameSoundEffect.kt new file mode 100644 index 0000000..51b58dd --- /dev/null +++ b/core/src/main/kotlin/com/last/commit/audio/GameSoundEffect.kt @@ -0,0 +1,9 @@ +package com.last.commit.audio; + +import com.last.commit.audio.GameSound + +public class GameSoundEffect(name: String): GameSound(name) { + companion object { + val STEPS_INDOOR = GameSoundEffect("steps_indoor.mp3"); + } +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/audio/SoundEngine.kt b/core/src/main/kotlin/com/last/commit/audio/SoundEngine.kt new file mode 100644 index 0000000..e1207e1 --- /dev/null +++ b/core/src/main/kotlin/com/last/commit/audio/SoundEngine.kt @@ -0,0 +1,51 @@ +package com.last.commit.audio; + +import com.badlogic.gdx.Gdx +import com.badlogic.gdx.audio.Sound +import com.badlogic.gdx.audio.Music +import com.last.commit.audio.GameSound +import com.last.commit.audio.GameMusic + + +public class SoundEngine { + + private val sounds: HashMap = HashMap() + private var playingMusic: Music? = null + + public fun play(gameSound: GameSound, volume: Float = 1f) { + if (gameSound is GameSoundEffect) { + val sound = loadEffect(gameSound.name) + sound.play(volume) + println("Playing sound ${gameSound.name}") + } else if (gameSound is GameMusic) { + if (playingMusic != null && playingMusic!!.isPlaying()) { + playingMusic!!.stop() + } + val music = loadMusic(gameSound.name) + music.volume = volume + music.setLooping(true) + music.play() + playingMusic = music + } + } + + private fun loadEffect(name: String): Sound { + return loadSound("effects/$name") + } + + private fun loadMusic(name: String): Music { + return Gdx.audio.newMusic(Gdx.files.internal("sounds/music/$name")) + } + + private fun loadSound(name: String): Sound { + var sound = sounds.get(name) + + if (sound == null) { + println("Loading sound $name") + sound = Gdx.audio.newSound(Gdx.files.internal("sounds/$name")) + sounds[name] = sound + } + + return sound!! + } +} diff --git a/core/src/main/kotlin/com/last/commit/config/PlayerSettings.kt b/core/src/main/kotlin/com/last/commit/config/GameSettings.kt similarity index 54% rename from core/src/main/kotlin/com/last/commit/config/PlayerSettings.kt rename to core/src/main/kotlin/com/last/commit/config/GameSettings.kt index f24066a..d779b54 100644 --- a/core/src/main/kotlin/com/last/commit/config/PlayerSettings.kt +++ b/core/src/main/kotlin/com/last/commit/config/GameSettings.kt @@ -3,21 +3,20 @@ package com.last.commit.config import com.badlogic.gdx.Input.Keys import java.util.* -class PlayerSettings { +class GameSettings { val fullscreen: Boolean = false private val actionKeys: EnumMap> = EnumMap(ActionCommand::class.java) private val actionKeysReversed: HashMap = hashMapOf() - init { actionKeys[ActionCommand.UP] = listOf(Keys.UP, Keys.W) actionKeys[ActionCommand.DOWN] = listOf(Keys.DOWN, Keys.S) actionKeys[ActionCommand.LEFT] = listOf(Keys.LEFT, Keys.A) actionKeys[ActionCommand.RIGHT] = listOf(Keys.RIGHT, Keys.D) actionKeys[ActionCommand.OPEN_MENU] = listOf(Keys.ESCAPE) - actionKeys[ActionCommand.OPEN_INVENTORY] = listOf(Keys.I, Keys.A) + actionKeys[ActionCommand.OPEN_INVENTORY] = listOf(Keys.I) actionKeys[ActionCommand.TIME_TRAVEL] = listOf(Keys.T) actionKeys[ActionCommand.INTERACT] = listOf(Keys.E) @@ -38,41 +37,10 @@ class PlayerSettings { return actionKeysReversed[keyCode] } + fun getKeyCode(actionCommand: ActionCommand): List? { return actionKeys[actionCommand] } - private fun isActionPressed(actionCommand: ActionCommand, keyCode: Int): Boolean { - return actionCommand == getAction(keyCode) - } - - fun isOpenMenuPressed(keyCode: Int): Boolean { - return isActionPressed(ActionCommand.OPEN_MENU, keyCode) - } - fun isOpenInventoryPressed(keyCode: Int): Boolean { - return isActionPressed(ActionCommand.OPEN_INVENTORY, keyCode) - } - - fun isUpPressed(keyCode: Int): Boolean { - return isActionPressed(ActionCommand.UP, keyCode) - } - - fun isDownPressed(keyCode: Int): Boolean { - return isActionPressed(ActionCommand.DOWN, keyCode) - } - - fun isLeftPressed(keyCode: Int): Boolean { - return isActionPressed(ActionCommand.LEFT, keyCode) - } - fun isRightPressed(keyCode: Int): Boolean { - return isActionPressed(ActionCommand.RIGHT, keyCode) - } - fun isTimeTravelPressed(keyCode: Int): Boolean { - return isActionPressed(ActionCommand.TIME_TRAVEL, keyCode) - } - - fun isInteractPressed(keyCode: Int): Boolean { - return isActionPressed(ActionCommand.INTERACT, keyCode) - } } \ No newline at end of file