1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2025-11-28 15:26:12 +00:00

Merge pull request #334 from FunkinCrew/bugfix/loading-state

Fix songs failing to load on HTML5 target
This commit is contained in:
Cameron Taylor 2024-02-17 03:48:45 -05:00 committed by GitHub
commit 73797688da
5 changed files with 70 additions and 72 deletions

View file

@ -309,15 +309,11 @@ class InitState extends FlxState
return; return;
} }
// Load and cache the song's charts. LoadingState.loadPlayState(
// TODO: Do this in the loading state.
songData.cacheCharts(true);
LoadingState.loadAndSwitchState(() -> new funkin.play.PlayState(
{ {
targetSong: songData, targetSong: songData,
targetDifficulty: difficultyId, targetDifficulty: difficultyId,
})); });
} }
/** /**
@ -343,11 +339,11 @@ class InitState extends FlxState
var targetSong:funkin.play.song.Song = SongRegistry.instance.fetchEntry(targetSongId); var targetSong:funkin.play.song.Song = SongRegistry.instance.fetchEntry(targetSongId);
LoadingState.loadAndSwitchState(() -> new funkin.play.PlayState( LoadingState.loadPlayState(
{ {
targetSong: targetSong, targetSong: targetSong,
targetDifficulty: difficultyId, targetDifficulty: difficultyId,
})); });
} }
function defineSong():String function defineSong():String

View file

@ -123,6 +123,11 @@ typedef PlayStateParams =
* and must be loaded externally. * and must be loaded externally.
*/ */
?overrideMusic:Bool, ?overrideMusic:Bool,
/**
* The initial camera follow point.
* Used to persist the position of the `cameraFollowPosition` between levels.
*/
?cameraFollowPoint:FlxPoint,
} }
/** /**
@ -216,7 +221,7 @@ class PlayState extends MusicBeatSubState
* The camera follow point from the last stage. * The camera follow point from the last stage.
* Used to persist the position of the `cameraFollowPosition` between levels. * Used to persist the position of the `cameraFollowPosition` between levels.
*/ */
public var previousCameraFollowPoint:FlxSprite = null; public var previousCameraFollowPoint:FlxPoint = null;
/** /**
* The current camera zoom level. * The current camera zoom level.
@ -544,6 +549,7 @@ class PlayState extends MusicBeatSubState
isMinimalMode = params.minimalMode ?? false; isMinimalMode = params.minimalMode ?? false;
startTimestamp = params.startTimestamp ?? 0.0; startTimestamp = params.startTimestamp ?? 0.0;
overrideMusic = params.overrideMusic ?? false; overrideMusic = params.overrideMusic ?? false;
previousCameraFollowPoint = params.cameraFollowPoint;
// Don't do anything else here! Wait until create() when we attach to the camera. // Don't do anything else here! Wait until create() when we attach to the camera.
} }
@ -2621,37 +2627,24 @@ class PlayState extends MusicBeatSubState
FlxG.sound.play(Paths.sound('Lights_Shut_off'), function() { FlxG.sound.play(Paths.sound('Lights_Shut_off'), function() {
// no camFollow so it centers on horror tree // no camFollow so it centers on horror tree
var targetSong:Song = SongRegistry.instance.fetchEntry(targetSongId); var targetSong:Song = SongRegistry.instance.fetchEntry(targetSongId);
// Load and cache the song's charts. LoadingState.loadPlayState(
// TODO: Do this in the loading state.
targetSong.cacheCharts(true);
LoadingState.loadAndSwitchState(() -> {
var nextPlayState:PlayState = new PlayState(
{ {
targetSong: targetSong, targetSong: targetSong,
targetDifficulty: PlayStatePlaylist.campaignDifficulty, targetDifficulty: PlayStatePlaylist.campaignDifficulty,
targetVariation: currentVariation, targetVariation: currentVariation,
}); cameraFollowPoint: cameraFollowPoint.getPosition(),
nextPlayState.previousCameraFollowPoint = new FlxSprite(cameraFollowPoint.x, cameraFollowPoint.y);
return nextPlayState;
}); });
}); });
} }
else else
{ {
var targetSong:Song = SongRegistry.instance.fetchEntry(targetSongId); var targetSong:Song = SongRegistry.instance.fetchEntry(targetSongId);
// Load and cache the song's charts. LoadingState.loadPlayState(
// TODO: Do this in the loading state.
targetSong.cacheCharts(true);
LoadingState.loadAndSwitchState(() -> {
var nextPlayState:PlayState = new PlayState(
{ {
targetSong: targetSong, targetSong: targetSong,
targetDifficulty: PlayStatePlaylist.campaignDifficulty, targetDifficulty: PlayStatePlaylist.campaignDifficulty,
targetVariation: currentVariation, targetVariation: currentVariation,
}); cameraFollowPoint: cameraFollowPoint.getPosition(),
nextPlayState.previousCameraFollowPoint = new FlxSprite(cameraFollowPoint.x, cameraFollowPoint.y);
return nextPlayState;
}); });
} }
} }

View file

@ -1135,18 +1135,14 @@ class FreeplayState extends MusicBeatSubState
FlxG.sound.play(Paths.sound('confirmMenu')); FlxG.sound.play(Paths.sound('confirmMenu'));
dj.confirm(); dj.confirm();
// Load and cache the song's charts.
// TODO: Do this in the loading state.
targetSong.cacheCharts(true);
new FlxTimer().start(1, function(tmr:FlxTimer) { new FlxTimer().start(1, function(tmr:FlxTimer) {
Paths.setCurrentLevel(cap.songData.levelId); Paths.setCurrentLevel(cap.songData.levelId);
LoadingState.loadAndSwitchState(() -> new PlayState( LoadingState.loadPlayState(
{ {
targetSong: targetSong, targetSong: targetSong,
targetDifficulty: targetDifficulty, targetDifficulty: targetDifficulty,
targetVariation: targetVariation, targetVariation: targetVariation,
}), true); }, true);
}); });
} }

View file

@ -554,22 +554,15 @@ class StoryMenuState extends MusicBeatState
PlayStatePlaylist.campaignTitle = currentLevel.getTitle(); PlayStatePlaylist.campaignTitle = currentLevel.getTitle();
PlayStatePlaylist.campaignDifficulty = currentDifficultyId; PlayStatePlaylist.campaignDifficulty = currentDifficultyId;
if (targetSong != null)
{
// Load and cache the song's charts.
// TODO: Do this in the loading state.
targetSong.cacheCharts(true);
}
new FlxTimer().start(1, function(tmr:FlxTimer) { new FlxTimer().start(1, function(tmr:FlxTimer) {
FlxTransitionableState.skipNextTransIn = false; FlxTransitionableState.skipNextTransIn = false;
FlxTransitionableState.skipNextTransOut = false; FlxTransitionableState.skipNextTransOut = false;
LoadingState.loadAndSwitchState(() -> new PlayState( LoadingState.loadPlayState(
{ {
targetSong: targetSong, targetSong: targetSong,
targetDifficulty: PlayStatePlaylist.campaignDifficulty, targetDifficulty: PlayStatePlaylist.campaignDifficulty,
}), true); }, true);
}); });
} }

View file

@ -9,7 +9,6 @@ import funkin.graphics.shaders.ScreenWipeShader;
import funkin.play.PlayState; import funkin.play.PlayState;
import funkin.play.PlayStatePlaylist; import funkin.play.PlayStatePlaylist;
import funkin.play.song.Song.SongDifficulty; import funkin.play.song.Song.SongDifficulty;
import funkin.ui.mainmenu.MainMenuState;
import funkin.ui.MusicBeatState; import funkin.ui.MusicBeatState;
import haxe.io.Path; import haxe.io.Path;
import funkin.graphics.FunkinSprite; import funkin.graphics.FunkinSprite;
@ -27,17 +26,19 @@ class LoadingState extends MusicBeatState
inline static var MIN_TIME = 1.0; inline static var MIN_TIME = 1.0;
var target:NextState; var target:NextState;
var stopMusic = false; var playParams:Null<PlayStateParams>;
var stopMusic:Bool = false;
var callbacks:MultiCallback; var callbacks:MultiCallback;
var danceLeft = false; var danceLeft:Bool = false;
var loadBar:FlxSprite; var loadBar:FlxSprite;
var funkay:FlxSprite; var funkay:FlxSprite;
function new(target:NextState, stopMusic:Bool) function new(target:NextState, stopMusic:Bool, playParams:Null<PlayStateParams> = null)
{ {
super(); super();
this.target = target; this.target = target;
this.playParams = playParams;
this.stopMusic = stopMusic; this.stopMusic = stopMusic;
} }
@ -62,10 +63,18 @@ class LoadingState extends MusicBeatState
callbacks = new MultiCallback(onLoad); callbacks = new MultiCallback(onLoad);
var introComplete = callbacks.add('introComplete'); var introComplete = callbacks.add('introComplete');
if (Std.isOfType(target, PlayState)) if (playParams != null)
{ {
var targetPlayState:PlayState = cast target; // Load and cache the song's charts.
var targetChart:SongDifficulty = targetPlayState.currentChart; if (playParams.targetSong != null)
{
playParams.targetSong.cacheCharts(true);
}
// Preload the song for the play state.
var difficulty:String = playParams.targetDifficulty ?? Constants.DEFAULT_DIFFICULTY;
var variation:String = playParams.targetVariation ?? Constants.DEFAULT_VARIATION;
var targetChart:SongDifficulty = playParams.targetSong?.getDifficulty(difficulty, variation);
var instPath:String = Paths.inst(targetChart.song.id); var instPath:String = Paths.inst(targetChart.song.id);
var voicesPaths:Array<String> = targetChart.buildVoiceList(); var voicesPaths:Array<String> = targetChart.buildVoiceList();
@ -172,25 +181,36 @@ class LoadingState extends MusicBeatState
return Paths.inst(PlayState.instance.currentSong.id); return Paths.inst(PlayState.instance.currentSong.id);
} }
inline static public function loadAndSwitchState(nextState:NextState, shouldStopMusic = false):Void /**
{ * Starts the transition to a new `PlayState` to start a new song.
FlxG.switchState(getNextState(nextState, shouldStopMusic)); * First switches to the `LoadingState` if assets need to be loaded.
} * @param params The parameters for the next `PlayState`.
* @param shouldStopMusic Whether to stop the current music while loading.
static function getNextState(nextState:NextState, shouldStopMusic = false):NextState */
public static function loadPlayState(params:PlayStateParams, shouldStopMusic = false):Void
{ {
Paths.setCurrentLevel(PlayStatePlaylist.campaignId); Paths.setCurrentLevel(PlayStatePlaylist.campaignId);
var playStateCtor:NextState = () -> new PlayState(params);
#if NO_PRELOAD_ALL #if NO_PRELOAD_ALL
// var loaded = isSoundLoaded(getSongPath()) // Switch to loading state while we load assets (default on HTML5 target).
// && (!PlayState.currentSong.needsVoices || isSoundLoaded(getVocalPath())) var loadStateCtor:NextState = () -> new LoadingState(playStateCtor, shouldStopMusic, params);
// && isLibraryLoaded('shared'); FlxG.switchState(loadStateCtor);
// #else
if (true) return () -> new LoadingState(nextState, shouldStopMusic); // All assets preloaded, switch directly to play state (defualt on other targets).
#end if (shouldStopMusic && FlxG.sound.music != null)
if (shouldStopMusic && FlxG.sound.music != null) FlxG.sound.music.stop(); {
FlxG.sound.music.stop();
}
return nextState; // Load and cache the song's charts.
if (params?.targetSong != null)
{
params.targetSong.cacheCharts(true);
}
FlxG.switchState(playStateCtor);
#end
} }
#if NO_PRELOAD_ALL #if NO_PRELOAD_ALL