mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-30 22:58:50 +07:00
Fix city ambience sound not being stopped when the city screen is updated via UncivGame.replaceCurrentScreen
This commit is contained in:
@ -4,15 +4,23 @@ import com.badlogic.gdx.Files
|
||||
import com.badlogic.gdx.Gdx
|
||||
import com.badlogic.gdx.audio.Music
|
||||
import com.badlogic.gdx.files.FileHandle
|
||||
import com.badlogic.gdx.utils.Disposable
|
||||
import com.unciv.UncivGame
|
||||
import com.unciv.logic.city.CityInfo
|
||||
import com.unciv.utils.Log
|
||||
|
||||
class CityAmbiencePlayer {
|
||||
/** Must be [disposed][dispose]. Starts playing an ambience sound for the city when created. Stops playing the ambience sound when [disposed][dispose]. */
|
||||
class CityAmbiencePlayer(
|
||||
city: CityInfo
|
||||
) : Disposable {
|
||||
private val soundsLocation = Files.FileType.Local
|
||||
private var playingCitySound: Music? = null
|
||||
val fileExtensions = listOf("mp3", "ogg", "wav") // All Gdx formats
|
||||
|
||||
init {
|
||||
play(city)
|
||||
}
|
||||
|
||||
private fun getFile(path: String) =
|
||||
if (soundsLocation == Files.FileType.External && Gdx.files.isExternalStorageAvailable)
|
||||
Gdx.files.external(path)
|
||||
@ -36,7 +44,9 @@ class CityAmbiencePlayer {
|
||||
.filter { it.exists() && !it.isDirectory && it.extension() in fileExtensions }
|
||||
.firstOrNull { it.name().contains(fileName) }
|
||||
|
||||
fun play(city: CityInfo) {
|
||||
private fun play(city: CityInfo) {
|
||||
if (UncivGame.Current.settings.citySoundsVolume == 0f) return
|
||||
|
||||
if (playingCitySound != null)
|
||||
stop()
|
||||
try {
|
||||
@ -51,7 +61,11 @@ class CityAmbiencePlayer {
|
||||
}
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
private fun stop() {
|
||||
playingCitySound?.dispose()
|
||||
}
|
||||
|
||||
override fun dispose() {
|
||||
stop()
|
||||
}
|
||||
}
|
||||
|
@ -85,7 +85,6 @@ class CityScreen(
|
||||
keyShortcuts.add(KeyCharAndCode.BACK)
|
||||
onActivation {
|
||||
exit()
|
||||
cityAmbiencePlayer.stop()
|
||||
}
|
||||
}
|
||||
|
||||
@ -116,14 +115,12 @@ class CityScreen(
|
||||
// val should be OK as buying tiles is what changes this, and that would re-create the whole CityScreen
|
||||
private val nextTileToOwn = city.expansion.chooseNewTileToOwn()
|
||||
|
||||
private val cityAmbiencePlayer = CityAmbiencePlayer()
|
||||
private val cityAmbiencePlayer = CityAmbiencePlayer(city)
|
||||
|
||||
init {
|
||||
if (city.isWeLoveTheKingDayActive() && UncivGame.Current.settings.citySoundsVolume > 0) {
|
||||
SoundPlayer.play(UncivSound("WLTK"))
|
||||
}
|
||||
if (UncivGame.Current.settings.citySoundsVolume > 0)
|
||||
cityAmbiencePlayer.play(city)
|
||||
|
||||
UncivGame.Current.settings.addCompletedTutorialTask("Enter city screen")
|
||||
|
||||
@ -431,4 +428,9 @@ class CityScreen(
|
||||
}
|
||||
|
||||
override fun recreate(): BaseScreen = CityScreen(city)
|
||||
|
||||
override fun dispose() {
|
||||
cityAmbiencePlayer.dispose()
|
||||
super.dispose()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user