mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-11 11:04:05 +09:00
test play music from appdata/Custom/Music
This commit is contained in:
@@ -258,6 +258,7 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) {
|
||||
override var gameFullyLoaded = false
|
||||
internal set
|
||||
|
||||
override val musicGovernor = TerrarumMusicGovernor()
|
||||
|
||||
|
||||
//////////////
|
||||
@@ -883,12 +884,11 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) {
|
||||
oldSelectedWireRenderClass = selectedWireRenderClass
|
||||
}
|
||||
|
||||
musicGovernor.update(this, delta)
|
||||
|
||||
////////////////////////
|
||||
// ui-related updates //
|
||||
////////////////////////
|
||||
//uiContainer.forEach { it.update(delta) }
|
||||
//debugWindow.update(delta)
|
||||
//notifier.update(delta)
|
||||
// open/close fake blur UI according to what's opened
|
||||
if (uiInventoryPlayer.isVisible ||
|
||||
getUIFixture.get()?.isVisible == true || worldTransitionOngoing) {
|
||||
@@ -1520,6 +1520,8 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) {
|
||||
catch (e: IllegalArgumentException) {}
|
||||
}
|
||||
|
||||
musicGovernor.dispose()
|
||||
|
||||
super.dispose()
|
||||
}
|
||||
}
|
||||
|
||||
132
src/net/torvald/terrarum/modulebasegame/TerrarumMusicGovernor.kt
Normal file
132
src/net/torvald/terrarum/modulebasegame/TerrarumMusicGovernor.kt
Normal file
@@ -0,0 +1,132 @@
|
||||
package net.torvald.terrarum.modulebasegame
|
||||
|
||||
import com.badlogic.gdx.Gdx
|
||||
import com.badlogic.gdx.audio.Music
|
||||
import com.badlogic.gdx.utils.GdxRuntimeException
|
||||
import net.torvald.terrarum.App
|
||||
import net.torvald.terrarum.App.printdbg
|
||||
import net.torvald.terrarum.IngameInstance
|
||||
import net.torvald.terrarum.MusicGovernor
|
||||
import net.torvald.terrarum.tryDispose
|
||||
import java.io.File
|
||||
|
||||
data class MusicContainer(
|
||||
val name: String,
|
||||
val file: File,
|
||||
val gdxMusic: Music
|
||||
) {
|
||||
override fun toString() = if (name.isEmpty()) file.nameWithoutExtension else name
|
||||
}
|
||||
|
||||
class TerrarumMusicGovernor : MusicGovernor() {
|
||||
|
||||
private val songs: List<MusicContainer> =
|
||||
File(App.customMusicDir).listFiles()?.mapNotNull {
|
||||
printdbg(this, "Music: ${it.absolutePath}")
|
||||
try {
|
||||
MusicContainer(
|
||||
it.nameWithoutExtension,
|
||||
it,
|
||||
Gdx.audio.newMusic(Gdx.files.absolute(it.absolutePath))
|
||||
)
|
||||
}
|
||||
catch (e: GdxRuntimeException) {
|
||||
e.printStackTrace()
|
||||
null
|
||||
}
|
||||
} ?: emptyList() // TODO test code
|
||||
|
||||
private var currentMusic: MusicContainer? = null
|
||||
|
||||
private var musicBin: ArrayList<Int> = ArrayList(songs.indices.toList().shuffled())
|
||||
|
||||
|
||||
private fun stopMusic() {
|
||||
printdbg(this, "Now stopping: $currentMusic")
|
||||
state = 2
|
||||
intermissionAkku = 0f
|
||||
intermissionLength = 30f + 60f * Math.random().toFloat() // 30s-90m
|
||||
musicFired = false
|
||||
currentMusic = null
|
||||
fadeoutFired = false
|
||||
printdbg(this, "Intermission: $intermissionLength seconds")
|
||||
}
|
||||
|
||||
|
||||
private var warningPrinted = false
|
||||
|
||||
private val musicVolume: Float
|
||||
get() = (App.getConfigDouble("musicvolume") * App.getConfigDouble("mastervolume")).toFloat()
|
||||
|
||||
override fun update(ingame: IngameInstance, delta: Float) {
|
||||
if (songs.isEmpty()) {
|
||||
if (!warningPrinted) {
|
||||
warningPrinted = true
|
||||
printdbg(this, "Warning: songs list is empty")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
val ingame = ingame as TerrarumIngame
|
||||
if (state == 0) state = 2
|
||||
|
||||
|
||||
when (state) {
|
||||
1 -> {
|
||||
if (!musicFired) {
|
||||
musicFired = true
|
||||
|
||||
val song = songs[musicBin.removeAt(0)]
|
||||
// prevent same song to play twice
|
||||
if (musicBin.isEmpty()) {
|
||||
musicBin = ArrayList(songs.indices.toList().shuffled())
|
||||
}
|
||||
|
||||
song.gdxMusic.volume = musicVolume
|
||||
song.gdxMusic.play()
|
||||
printdbg(this, "Now playing: $song")
|
||||
|
||||
currentMusic = song
|
||||
|
||||
// process fadeout request
|
||||
if (fadeoutFired) {
|
||||
fadeoutAkku += delta
|
||||
currentMusic?.gdxMusic?.volume = 1f - musicVolume * (fadeoutAkku / fadeoutLength)
|
||||
|
||||
if (fadeoutAkku >= fadeoutLength) {
|
||||
currentMusic?.gdxMusic?.pause()
|
||||
}
|
||||
}
|
||||
// process fadein request
|
||||
else if (fadeinFired) {
|
||||
if (currentMusic?.gdxMusic?.isPlaying == false) {
|
||||
currentMusic?.gdxMusic?.play()
|
||||
}
|
||||
fadeoutAkku += delta
|
||||
currentMusic?.gdxMusic?.volume = musicVolume * (fadeoutAkku / fadeoutLength)
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (currentMusic?.gdxMusic?.isPlaying == false) {
|
||||
stopMusic()
|
||||
}
|
||||
}
|
||||
}
|
||||
2 -> {
|
||||
intermissionAkku += delta
|
||||
|
||||
if (intermissionAkku >= intermissionLength) {
|
||||
intermissionAkku = 0f
|
||||
state = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override fun dispose() {
|
||||
currentMusic?.gdxMusic?.stop()
|
||||
stopMusic()
|
||||
songs.forEach { it.gdxMusic.tryDispose() }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user