mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-07 12:21:52 +09:00
musicplayer: actively disables playback if jukebox is on
This commit is contained in:
@@ -15,6 +15,7 @@ import net.torvald.terrarum.audio.*
|
||||
import net.torvald.terrarum.gameworld.fmod
|
||||
import net.torvald.terrarum.modulebasegame.MusicContainer
|
||||
import net.torvald.terrarum.modulebasegame.TerrarumIngame
|
||||
import net.torvald.terrarum.modulebasegame.gameactors.PlaysMusic
|
||||
import net.torvald.terrarum.ui.BasicDebugInfoWindow
|
||||
import net.torvald.terrarum.ui.MouseLatch
|
||||
import net.torvald.terrarum.ui.Toolkit
|
||||
@@ -97,6 +98,11 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
||||
private val songsInGovernor: List<MusicContainer>
|
||||
get() = ingame.musicGovernor.extortField<List<MusicContainer>>("songs")!!
|
||||
|
||||
private val shouldPlayerBeDisabled: Boolean
|
||||
get() {
|
||||
return App.audioMixer.dynamicTracks.any { it.isPlaying && it.trackingTarget is PlaysMusic }
|
||||
}
|
||||
|
||||
/** Returns the playlist name from the MusicGovernor. Getting the value from the MusicGovernor
|
||||
* is recommended as an ingame interaction may cancel the playback from the playlist from the MusicPlayer
|
||||
* (e.g. interacting with a jukebox) */
|
||||
@@ -137,14 +143,9 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
||||
}
|
||||
|
||||
ingame.musicGovernor.addMusicStopHook { music ->
|
||||
if (diskJockeyingMode == "intermittent") {
|
||||
setIntermission()
|
||||
if (mode <= MODE_PLAYING)
|
||||
transitionRequest = MODE_IDLE
|
||||
}
|
||||
else if (diskJockeyingMode == "continuous") {
|
||||
|
||||
}
|
||||
setIntermission()
|
||||
if (mode <= MODE_PLAYING)
|
||||
transitionRequest = MODE_IDLE
|
||||
}
|
||||
|
||||
setPlaylistDisplayVars(songsInGovernor)
|
||||
@@ -185,6 +186,8 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
||||
private var mouseOnList: Int? = null
|
||||
|
||||
override fun updateUI(delta: Float) {
|
||||
val shouldPlayerBeDisabled = shouldPlayerBeDisabled
|
||||
|
||||
// process transition request
|
||||
if (transitionRequest != null) {
|
||||
modeNext = transitionRequest!!
|
||||
@@ -369,7 +372,7 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
||||
ingame.musicGovernor.stopMusic()
|
||||
thisMusic?.let { ingame.musicGovernor.queueMusicToPlayNext(it) }
|
||||
}
|
||||
else {
|
||||
else if (!shouldPlayerBeDisabled) {
|
||||
ingame.musicGovernor.startMusic()
|
||||
}
|
||||
}
|
||||
@@ -427,7 +430,9 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
||||
|
||||
// fade out
|
||||
App.audioMixer.requestFadeOut(App.audioMixer.musicTrack, AudioMixer.DEFAULT_FADEOUT_LEN / 3f) {
|
||||
ingame.musicGovernor.startMusic() // required for "intermittent" mode
|
||||
if (!shouldPlayerBeDisabled) {
|
||||
ingame.musicGovernor.startMusic() // required for "intermittent" mode
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -444,7 +449,9 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
||||
// fade out
|
||||
App.audioMixer.requestFadeOut(App.audioMixer.musicTrack, AudioMixer.DEFAULT_FADEOUT_LEN / 3f) {
|
||||
loadNewAlbum(albumsList[index])
|
||||
ingame.musicGovernor.startMusic() // required for "intermittent" mode
|
||||
if (!shouldPlayerBeDisabled) {
|
||||
ingame.musicGovernor.startMusic() // required for "intermittent" mode
|
||||
}
|
||||
resetPlaylistScroll(App.audioMixer.musicTrack.nextTrack)
|
||||
}
|
||||
}
|
||||
@@ -458,8 +465,21 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
||||
|
||||
|
||||
// printdbg(this, "mode = $mode; req = $transitionRequest")
|
||||
|
||||
if (shouldPlayerBeDisabled) {
|
||||
ingame.musicGovernor.stopMusic()
|
||||
}
|
||||
else if (!jukeboxStopMonitorAlert && !App.audioMixer.musicTrack.isPlaying) {
|
||||
jukeboxStopMonitorAlert = true
|
||||
ingame.musicGovernor.stopMusic(false, Math.random().toFloat() * 30f + 30f)
|
||||
}
|
||||
else if (App.audioMixer.musicTrack.isPlaying) {
|
||||
jukeboxStopMonitorAlert = false
|
||||
}
|
||||
}
|
||||
|
||||
private var jukeboxStopMonitorAlert = true
|
||||
|
||||
private fun resetAlbumlistScroll() {
|
||||
val currentlyPlaying = albumsList.indexOfFirst { it.canonicalPath.replace('\\', '/') == ingame.musicGovernor.playlistSource }
|
||||
if (currentlyPlaying >= 0) {
|
||||
|
||||
@@ -282,12 +282,19 @@ class MixerTrackProcessor(bufferSize: Int, val rate: Int, val track: TerrarumAud
|
||||
|
||||
// method 1.
|
||||
// https://www.desmos.com/calculator/uzbjw10lna
|
||||
val K = 512.0
|
||||
return K.pow(-sqrt(1.0+x.sqr())) * K
|
||||
// val K = 512.0
|
||||
// return K.pow(-sqrt(1.0+x.sqr())) * K
|
||||
|
||||
|
||||
// method 2.
|
||||
// https://www.desmos.com/calculator/3xsac66rsp
|
||||
|
||||
|
||||
// method 3.
|
||||
// comparison with method 1.
|
||||
// https://www.desmos.com/calculator/rbteowef8v
|
||||
val Q = 2.0
|
||||
return 1.0 / cosh(Q * x).sqr()
|
||||
}
|
||||
|
||||
private fun FloatArray.applyVolume(volume: Float) = FloatArray(this.size) { (this[it] * volume) }
|
||||
|
||||
@@ -323,25 +323,22 @@ class TerrarumMusicGovernor : MusicGovernor() {
|
||||
protected var ambState = 0
|
||||
protected var ambFired = false
|
||||
|
||||
private fun stopMusic(song: MusicContainer?, callStopMusicHook: Boolean = true) {
|
||||
if (intermissionLength < Float.POSITIVE_INFINITY) {
|
||||
musicState = STATE_INTERMISSION
|
||||
intermissionAkku = 0f
|
||||
intermissionLength =
|
||||
if (diskJockeyingMode == "intermittent") 30f + 30f * Math.random().toFloat() else 0f // 30s-60s
|
||||
musicFired = false
|
||||
if (callStopMusicHook && musicStopHooks.isNotEmpty()) musicStopHooks.forEach {
|
||||
if (song != null) {
|
||||
it(song)
|
||||
}
|
||||
private fun stopMusic(song: MusicContainer?, callStopMusicHook: Boolean = true, customPauseLen: Float? = null) {
|
||||
musicState = STATE_INTERMISSION
|
||||
intermissionAkku = 0f
|
||||
intermissionLength = customPauseLen ?:
|
||||
if (diskJockeyingMode == "intermittent") 30f + 30f * Math.random().toFloat() else Float.POSITIVE_INFINITY // 30s-60s
|
||||
musicFired = false
|
||||
if (callStopMusicHook && musicStopHooks.isNotEmpty()) musicStopHooks.forEach {
|
||||
if (song != null) {
|
||||
it(song)
|
||||
}
|
||||
printdbg(this, "StopMusic Intermission: $intermissionLength seconds")
|
||||
}
|
||||
// printdbg(this, "StopMusic Intermission: $intermissionLength seconds")
|
||||
}
|
||||
|
||||
fun stopMusic(callStopMusicHook: Boolean = true, pauseLen: Float = Float.POSITIVE_INFINITY) {
|
||||
stopMusic(App.audioMixer.musicTrack.currentTrack, callStopMusicHook)
|
||||
intermissionLength = pauseLen
|
||||
stopMusic(App.audioMixer.musicTrack.currentTrack, callStopMusicHook, pauseLen)
|
||||
// printdbg(this, "StopMusic Intermission2: $intermissionLength seconds")
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ import org.dyn4j.geometry.Vector2
|
||||
/**
|
||||
* Created by minjaesong on 2024-01-11.
|
||||
*/
|
||||
class FixtureJukebox : Electric {
|
||||
class FixtureJukebox : Electric, PlaysMusic {
|
||||
|
||||
constructor() : super(
|
||||
BlockBox(BlockBox.NO_COLLISION, 2, 3),
|
||||
@@ -83,7 +83,7 @@ class FixtureJukebox : Electric {
|
||||
|
||||
// supress the normal background music playback
|
||||
if (musicIsPlaying && !flagDespawn) {
|
||||
(INGAME.musicGovernor as TerrarumMusicGovernor).stopMusic()
|
||||
// (INGAME.musicGovernor as TerrarumMusicGovernor).stopMusic()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package net.torvald.terrarum.modulebasegame.gameactors
|
||||
|
||||
/**
|
||||
* A "marker" interface for fixtures that can play music.
|
||||
*/
|
||||
interface PlaysMusic {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user