From 6d121230630ddbc2f931bf8786977b5bec65d26f Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 19 Nov 2022 15:04:42 +0100 Subject: [PATCH] Fix whatever is wrong with the music --- .../kotlin/com/last/commit/audio/SoundEngine.kt | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/core/src/main/kotlin/com/last/commit/audio/SoundEngine.kt b/core/src/main/kotlin/com/last/commit/audio/SoundEngine.kt index e1207e1..e7e025c 100644 --- a/core/src/main/kotlin/com/last/commit/audio/SoundEngine.kt +++ b/core/src/main/kotlin/com/last/commit/audio/SoundEngine.kt @@ -10,7 +10,7 @@ import com.last.commit.audio.GameMusic public class SoundEngine { private val sounds: HashMap = HashMap() - private var playingMusic: Music? = null + private val musicTracks: HashMap = HashMap() public fun play(gameSound: GameSound, volume: Float = 1f) { if (gameSound is GameSoundEffect) { @@ -18,14 +18,11 @@ public class SoundEngine { 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.stop() music.volume = volume music.setLooping(true) music.play() - playingMusic = music } } @@ -34,7 +31,13 @@ public class SoundEngine { } private fun loadMusic(name: String): Music { - return Gdx.audio.newMusic(Gdx.files.internal("sounds/music/$name")) + 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 {