Fix whatever is wrong with the music

viewport-stuff
trivernis 2 years ago
parent 43523192d1
commit 6d12123063
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -10,7 +10,7 @@ import com.last.commit.audio.GameMusic
public class SoundEngine { public class SoundEngine {
private val sounds: HashMap<String, Sound> = HashMap() private val sounds: HashMap<String, Sound> = HashMap()
private var playingMusic: Music? = null private val musicTracks: HashMap<String, Music> = HashMap()
public fun play(gameSound: GameSound, volume: Float = 1f) { public fun play(gameSound: GameSound, volume: Float = 1f) {
if (gameSound is GameSoundEffect) { if (gameSound is GameSoundEffect) {
@ -18,14 +18,11 @@ public class SoundEngine {
sound.play(volume) sound.play(volume)
println("Playing sound ${gameSound.name}") println("Playing sound ${gameSound.name}")
} else if (gameSound is GameMusic) { } else if (gameSound is GameMusic) {
if (playingMusic != null && playingMusic!!.isPlaying()) {
playingMusic!!.stop()
}
val music = loadMusic(gameSound.name) val music = loadMusic(gameSound.name)
music.stop()
music.volume = volume music.volume = volume
music.setLooping(true) music.setLooping(true)
music.play() music.play()
playingMusic = music
} }
} }
@ -34,7 +31,13 @@ public class SoundEngine {
} }
private fun loadMusic(name: String): Music { 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 { private fun loadSound(name: String): Sound {

Loading…
Cancel
Save