Skip to content

Commit

Permalink
Make sound effects pause with the game
Browse files Browse the repository at this point in the history
  • Loading branch information
NotHyper-474 committed Jan 29, 2025
1 parent 6216655 commit 78d5218
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions source/funkin/play/PlayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import flixel.FlxObject;
import flixel.FlxSubState;
import flixel.math.FlxMath;
import flixel.math.FlxPoint;
import flixel.sound.FlxSound;
import flixel.text.FlxText;
import flixel.tweens.FlxTween;
import flixel.ui.FlxBar;
Expand Down Expand Up @@ -428,6 +429,11 @@ class PlayState extends MusicBeatSubState
*/
var cameraTweensPausedBySubState:List<FlxTween> = new List<FlxTween>();

/**
* Track any sounds we've paused for a Pause substate, so we can unpause them when we return.
*/
var soundsPausedBySubState:List<FlxSound> = new List<FlxSound>();

/**
* False until `create()` has completed.
*/
Expand Down Expand Up @@ -1207,9 +1213,17 @@ class PlayState extends MusicBeatSubState
musicPausedBySubState = true;
}

// Pause vocals.
// Not tracking that we've done this via a bool because vocal re-syncing involves pausing the vocals anyway.
if (vocals != null) vocals.pause();
// Pause any sounds that are playing and keep track of them.
// Vocals are also paused here but are not included as they are handled separately.
var flixelVocals = vocals?.members.map(function(voice:FunkinSound):FlxSound {
return cast voice;
});
FlxG.sound.list.forEachAlive(function(sound:FlxSound) {
if (sound != null && (sound == FlxG.sound.music || !sound.playing)) return;
sound.pause();

if (flixelVocals != null && flixelVocals.indexOf(sound) == -1) soundsPausedBySubState.add(sound);
});
}

// Pause camera tweening, and keep track of which tweens we pause.
Expand Down Expand Up @@ -1275,6 +1289,12 @@ class PlayState extends MusicBeatSubState
// Resume camera follow
FlxG.camera.followLerp = Constants.DEFAULT_CAMERA_FOLLOW_RATE;

for (sound in soundsPausedBySubState)
{
sound.resume();
}
soundsPausedBySubState.clear();

if (currentConversation != null)
{
currentConversation.resumeMusic();
Expand Down Expand Up @@ -1315,6 +1335,16 @@ class PlayState extends MusicBeatSubState

justUnpaused = true;
}
else if (Std.isOfType(subState, GameOverSubState))
{
// Prevents a sound (like thunder) from playing after dying,
// pausing then resuming the game even though it's not supposed to.
for (sound in soundsPausedBySubState)
{
sound.kill();
}
soundsPausedBySubState.clear();
}
else if (Std.isOfType(subState, Transition))
{
// Do nothing.
Expand Down

0 comments on commit 78d5218

Please sign in to comment.