Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sound effects pause with the game #4058

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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