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 9b9a4f0..dad8a81 100644 --- a/core/src/main/kotlin/com/last/commit/FirstScreen.kt +++ b/core/src/main/kotlin/com/last/commit/FirstScreen.kt @@ -21,6 +21,8 @@ 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 /** First screen of the application. Displayed after the application is created. */ @@ -59,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 { diff --git a/core/src/main/kotlin/com/last/commit/Game.kt b/core/src/main/kotlin/com/last/commit/Game.kt index 68d4009..852e442 100644 --- a/core/src/main/kotlin/com/last/commit/Game.kt +++ b/core/src/main/kotlin/com/last/commit/Game.kt @@ -4,6 +4,7 @@ import GameState import com.badlogic.gdx.Game import com.last.commit.config.GameSettings import com.last.commit.inventory.Inventory +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(), - GameSettings() + 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 e2796f7..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.GameSettings +import com.last.commit.audio.SoundEngine 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..e7e025c --- /dev/null +++ b/core/src/main/kotlin/com/last/commit/audio/SoundEngine.kt @@ -0,0 +1,54 @@ +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 val musicTracks: HashMap = HashMap() + + 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) { + val music = loadMusic(gameSound.name) + music.stop() + music.volume = volume + music.setLooping(true) + music.play() + } + } + + private fun loadEffect(name: String): Sound { + return loadSound("effects/$name") + } + + private fun loadMusic(name: String): Music { + var music = musicTracks.get(name) + if (music == null) { + println("Loading sound $name") + music = Gdx.audio.newMusic(Gdx.files.internal("sounds/music/$name")) + musicTracks[name] = music + } + return music!! + } + + 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!! + } +}