|
1 | 1 | package backend; |
2 | 2 |
|
| 3 | +import haxe.ds.HashMap; |
3 | 4 | import backend.window.CppAPI; |
4 | 5 | import flixel.FlxState; |
5 | 6 | import backend.PsychCamera; |
@@ -310,6 +311,63 @@ class MusicBeatState extends FlxState |
310 | 311 |
|
311 | 312 | } |
312 | 313 | } |
| 314 | + var preloadFunctions:Map<String, (FlxState)->Void> = [ |
| 315 | + "PlayState" => function(state:FlxState) { |
| 316 | + if (state is PlayState) { |
| 317 | + @:privateAccess |
| 318 | + (cast state:PlayState).generateSong(); |
| 319 | + } |
| 320 | + } |
| 321 | + ]; |
| 322 | + |
| 323 | + public function hashCode():Int |
| 324 | + { |
| 325 | + return Type.getClassName(Type.getClass(this)).hashcode(); |
| 326 | + } |
| 327 | + |
| 328 | + public function preloadState(switchState:Bool = false, state:FlxState, ?proceedOnError:Bool = true) |
| 329 | + { |
| 330 | + var stateClassName = Type.getClassName(Type.getClass(state)).split(".")[Lambda.count(Type.getClassName(Type.getClass(state)).split(".")) - 1]; |
| 331 | + var preloadFunction = preloadFunctions.get(stateClassName); |
| 332 | + var errored = false; |
| 333 | + if (preloadFunction == null) |
| 334 | + { |
| 335 | + trace('No preload function for state: ' + stateClassName); |
| 336 | + } |
| 337 | + var preloader = function() |
| 338 | + { |
| 339 | + if (preloadFunction != null) |
| 340 | + { |
| 341 | + trace('Preloading state: ' + stateClassName); |
| 342 | + try { |
| 343 | + preloadFunction(state); |
| 344 | + } catch (e:Dynamic) { |
| 345 | + trace('Error during state preloading: ' + e); |
| 346 | + } |
| 347 | + preloadFunction = null; |
| 348 | + } |
| 349 | + if (switchState && (!errored || proceedOnError)) |
| 350 | + return MusicBeatState.switchState(this); |
| 351 | + }; |
| 352 | + |
| 353 | + var stateName = Type.getClassName(Type.getClass(state)); |
| 354 | + |
| 355 | + yutautil.Threader.runInThread(preloader(), 1, 'State Preloader'); |
| 356 | + } |
| 357 | + |
| 358 | + |
| 359 | + public static function preloadAndSwitchState(state:MusicBeatState) |
| 360 | + { |
| 361 | + if (state == null) |
| 362 | + state = cast(FlxG.state, MusicBeatState); |
| 363 | + if (state == FlxG.state) |
| 364 | + { |
| 365 | + resetState(); |
| 366 | + return; |
| 367 | + } |
| 368 | + |
| 369 | + state.preloadState(true, state); |
| 370 | + } |
313 | 371 |
|
314 | 372 | public static function switchState(nextState:FlxState = null) |
315 | 373 | { |
|
0 commit comments