mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-14 20:44:05 +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.gameworld.fmod
|
||||||
import net.torvald.terrarum.modulebasegame.MusicContainer
|
import net.torvald.terrarum.modulebasegame.MusicContainer
|
||||||
import net.torvald.terrarum.modulebasegame.TerrarumIngame
|
import net.torvald.terrarum.modulebasegame.TerrarumIngame
|
||||||
|
import net.torvald.terrarum.modulebasegame.gameactors.PlaysMusic
|
||||||
import net.torvald.terrarum.ui.BasicDebugInfoWindow
|
import net.torvald.terrarum.ui.BasicDebugInfoWindow
|
||||||
import net.torvald.terrarum.ui.MouseLatch
|
import net.torvald.terrarum.ui.MouseLatch
|
||||||
import net.torvald.terrarum.ui.Toolkit
|
import net.torvald.terrarum.ui.Toolkit
|
||||||
@@ -97,6 +98,11 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
|||||||
private val songsInGovernor: List<MusicContainer>
|
private val songsInGovernor: List<MusicContainer>
|
||||||
get() = ingame.musicGovernor.extortField<List<MusicContainer>>("songs")!!
|
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
|
/** 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
|
* is recommended as an ingame interaction may cancel the playback from the playlist from the MusicPlayer
|
||||||
* (e.g. interacting with a jukebox) */
|
* (e.g. interacting with a jukebox) */
|
||||||
@@ -137,14 +143,9 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ingame.musicGovernor.addMusicStopHook { music ->
|
ingame.musicGovernor.addMusicStopHook { music ->
|
||||||
if (diskJockeyingMode == "intermittent") {
|
setIntermission()
|
||||||
setIntermission()
|
if (mode <= MODE_PLAYING)
|
||||||
if (mode <= MODE_PLAYING)
|
transitionRequest = MODE_IDLE
|
||||||
transitionRequest = MODE_IDLE
|
|
||||||
}
|
|
||||||
else if (diskJockeyingMode == "continuous") {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setPlaylistDisplayVars(songsInGovernor)
|
setPlaylistDisplayVars(songsInGovernor)
|
||||||
@@ -185,6 +186,8 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
|||||||
private var mouseOnList: Int? = null
|
private var mouseOnList: Int? = null
|
||||||
|
|
||||||
override fun updateUI(delta: Float) {
|
override fun updateUI(delta: Float) {
|
||||||
|
val shouldPlayerBeDisabled = shouldPlayerBeDisabled
|
||||||
|
|
||||||
// process transition request
|
// process transition request
|
||||||
if (transitionRequest != null) {
|
if (transitionRequest != null) {
|
||||||
modeNext = transitionRequest!!
|
modeNext = transitionRequest!!
|
||||||
@@ -369,7 +372,7 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
|||||||
ingame.musicGovernor.stopMusic()
|
ingame.musicGovernor.stopMusic()
|
||||||
thisMusic?.let { ingame.musicGovernor.queueMusicToPlayNext(it) }
|
thisMusic?.let { ingame.musicGovernor.queueMusicToPlayNext(it) }
|
||||||
}
|
}
|
||||||
else {
|
else if (!shouldPlayerBeDisabled) {
|
||||||
ingame.musicGovernor.startMusic()
|
ingame.musicGovernor.startMusic()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -427,7 +430,9 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
|||||||
|
|
||||||
// fade out
|
// fade out
|
||||||
App.audioMixer.requestFadeOut(App.audioMixer.musicTrack, AudioMixer.DEFAULT_FADEOUT_LEN / 3f) {
|
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
|
// fade out
|
||||||
App.audioMixer.requestFadeOut(App.audioMixer.musicTrack, AudioMixer.DEFAULT_FADEOUT_LEN / 3f) {
|
App.audioMixer.requestFadeOut(App.audioMixer.musicTrack, AudioMixer.DEFAULT_FADEOUT_LEN / 3f) {
|
||||||
loadNewAlbum(albumsList[index])
|
loadNewAlbum(albumsList[index])
|
||||||
ingame.musicGovernor.startMusic() // required for "intermittent" mode
|
if (!shouldPlayerBeDisabled) {
|
||||||
|
ingame.musicGovernor.startMusic() // required for "intermittent" mode
|
||||||
|
}
|
||||||
resetPlaylistScroll(App.audioMixer.musicTrack.nextTrack)
|
resetPlaylistScroll(App.audioMixer.musicTrack.nextTrack)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -458,8 +465,21 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
|||||||
|
|
||||||
|
|
||||||
// printdbg(this, "mode = $mode; req = $transitionRequest")
|
// 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() {
|
private fun resetAlbumlistScroll() {
|
||||||
val currentlyPlaying = albumsList.indexOfFirst { it.canonicalPath.replace('\\', '/') == ingame.musicGovernor.playlistSource }
|
val currentlyPlaying = albumsList.indexOfFirst { it.canonicalPath.replace('\\', '/') == ingame.musicGovernor.playlistSource }
|
||||||
if (currentlyPlaying >= 0) {
|
if (currentlyPlaying >= 0) {
|
||||||
|
|||||||
@@ -282,12 +282,19 @@ class MixerTrackProcessor(bufferSize: Int, val rate: Int, val track: TerrarumAud
|
|||||||
|
|
||||||
// method 1.
|
// method 1.
|
||||||
// https://www.desmos.com/calculator/uzbjw10lna
|
// https://www.desmos.com/calculator/uzbjw10lna
|
||||||
val K = 512.0
|
// val K = 512.0
|
||||||
return K.pow(-sqrt(1.0+x.sqr())) * K
|
// return K.pow(-sqrt(1.0+x.sqr())) * K
|
||||||
|
|
||||||
|
|
||||||
// method 2.
|
// method 2.
|
||||||
// https://www.desmos.com/calculator/3xsac66rsp
|
// 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) }
|
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 ambState = 0
|
||||||
protected var ambFired = false
|
protected var ambFired = false
|
||||||
|
|
||||||
private fun stopMusic(song: MusicContainer?, callStopMusicHook: Boolean = true) {
|
private fun stopMusic(song: MusicContainer?, callStopMusicHook: Boolean = true, customPauseLen: Float? = null) {
|
||||||
if (intermissionLength < Float.POSITIVE_INFINITY) {
|
musicState = STATE_INTERMISSION
|
||||||
musicState = STATE_INTERMISSION
|
intermissionAkku = 0f
|
||||||
intermissionAkku = 0f
|
intermissionLength = customPauseLen ?:
|
||||||
intermissionLength =
|
if (diskJockeyingMode == "intermittent") 30f + 30f * Math.random().toFloat() else Float.POSITIVE_INFINITY // 30s-60s
|
||||||
if (diskJockeyingMode == "intermittent") 30f + 30f * Math.random().toFloat() else 0f // 30s-60s
|
musicFired = false
|
||||||
musicFired = false
|
if (callStopMusicHook && musicStopHooks.isNotEmpty()) musicStopHooks.forEach {
|
||||||
if (callStopMusicHook && musicStopHooks.isNotEmpty()) musicStopHooks.forEach {
|
if (song != null) {
|
||||||
if (song != null) {
|
it(song)
|
||||||
it(song)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
printdbg(this, "StopMusic Intermission: $intermissionLength seconds")
|
|
||||||
}
|
}
|
||||||
|
// printdbg(this, "StopMusic Intermission: $intermissionLength seconds")
|
||||||
}
|
}
|
||||||
|
|
||||||
fun stopMusic(callStopMusicHook: Boolean = true, pauseLen: Float = Float.POSITIVE_INFINITY) {
|
fun stopMusic(callStopMusicHook: Boolean = true, pauseLen: Float = Float.POSITIVE_INFINITY) {
|
||||||
stopMusic(App.audioMixer.musicTrack.currentTrack, callStopMusicHook)
|
stopMusic(App.audioMixer.musicTrack.currentTrack, callStopMusicHook, pauseLen)
|
||||||
intermissionLength = pauseLen
|
|
||||||
// printdbg(this, "StopMusic Intermission2: $intermissionLength seconds")
|
// printdbg(this, "StopMusic Intermission2: $intermissionLength seconds")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ import org.dyn4j.geometry.Vector2
|
|||||||
/**
|
/**
|
||||||
* Created by minjaesong on 2024-01-11.
|
* Created by minjaesong on 2024-01-11.
|
||||||
*/
|
*/
|
||||||
class FixtureJukebox : Electric {
|
class FixtureJukebox : Electric, PlaysMusic {
|
||||||
|
|
||||||
constructor() : super(
|
constructor() : super(
|
||||||
BlockBox(BlockBox.NO_COLLISION, 2, 3),
|
BlockBox(BlockBox.NO_COLLISION, 2, 3),
|
||||||
@@ -83,7 +83,7 @@ class FixtureJukebox : Electric {
|
|||||||
|
|
||||||
// supress the normal background music playback
|
// supress the normal background music playback
|
||||||
if (musicIsPlaying && !flagDespawn) {
|
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