From 01f8ef5b70dc0b9c9cd35eceb7090fe722283c96 Mon Sep 17 00:00:00 2001 From: Victor G <84867412+Cheemsandfriends@users.noreply.github.com> Date: Fri, 27 May 2022 22:46:26 +0200 Subject: [PATCH 1/7] Add the new way of adding the substates --- flixel/FlxG.hx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/flixel/FlxG.hx b/flixel/FlxG.hx index e7a3f215f1..7e9bbe714f 100644 --- a/flixel/FlxG.hx +++ b/flixel/FlxG.hx @@ -367,10 +367,10 @@ class FlxG * Attempts to switch from the current game state to `nextState`. * The state switch is successful if `switchTo()` of the current `state` returns `true`. */ - public static inline function switchState(nextState:FlxState):Void + public static inline function switchState(nextState:() -> FlxState):Void { - if (state.switchTo(nextState)) - game._requestedState = nextState; + if (state.switchTo(nextState())) + game._requestedState = nextState(); } /** @@ -379,7 +379,7 @@ class FlxG */ public static inline function resetState():Void { - switchState(Type.createInstance(Type.getClass(state), [])); + switchState(() -> Type.createInstance(Type.getClass(state), [])); } /** From 3f72b87f9721cd1ac91aa05611f70c11f64b6f6e Mon Sep 17 00:00:00 2001 From: Victor G <84867412+Cheemsandfriends@users.noreply.github.com> Date: Fri, 27 May 2022 22:49:06 +0200 Subject: [PATCH 2/7] override the FlxGroup's new thing so that it works with the State.new thing --- flixel/FlxState.hx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/flixel/FlxState.hx b/flixel/FlxState.hx index ee7451ede1..d8df1124fc 100644 --- a/flixel/FlxState.hx +++ b/flixel/FlxState.hx @@ -80,6 +80,8 @@ class FlxState extends FlxGroup @:noCompletion var _subStateClosed:FlxTypedSignalVoid>; + public function new() {super();} + /** * This function is called after the game engine successfully switches states. * Override this function, NOT the constructor, to initialize or set up your game state. From f69464ceb01f3dec16d56539d548670aa5d86e5a Mon Sep 17 00:00:00 2001 From: Victor G <84867412+Cheemsandfriends@users.noreply.github.com> Date: Fri, 27 May 2022 22:49:52 +0200 Subject: [PATCH 3/7] adding the FlxSplash switch state thingy --- flixel/system/FlxSplash.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flixel/system/FlxSplash.hx b/flixel/system/FlxSplash.hx index 745645aad6..aa1bd05ac9 100644 --- a/flixel/system/FlxSplash.hx +++ b/flixel/system/FlxSplash.hx @@ -199,7 +199,7 @@ class FlxSplash extends FlxState #end FlxG.stage.removeChild(_sprite); FlxG.stage.removeChild(_text); - FlxG.switchState(Type.createInstance(nextState, [])); + FlxG.switchState(() -> Type.createInstance(nextState, [])); FlxG.game._gameJustStarted = true; } } From 921d325a78f01286190accf897a531a417d64904 Mon Sep 17 00:00:00 2001 From: Victor G <84867412+Cheemsandfriends@users.noreply.github.com> Date: Sun, 14 Aug 2022 14:54:10 +0200 Subject: [PATCH 4/7] Change the way the FlxGame is called --- flixel/FlxGame.hx | 88 +++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 41 deletions(-) diff --git a/flixel/FlxGame.hx b/flixel/FlxGame.hx index 1cfc7d368e..63642f86e4 100644 --- a/flixel/FlxGame.hx +++ b/flixel/FlxGame.hx @@ -92,7 +92,7 @@ class FlxGame extends Sprite /** * Class type of the initial/first game state for the game, usually `MenuState` or something like that. */ - var _initialState:Class; + var _initialState:()->FlxState; /** * Current game state. @@ -201,13 +201,18 @@ class FlxGame extends Sprite /** * If a state change was requested, the new state object is stored here until we switch to it. */ - var _requestedState:FlxState; + var _requestedState(default, set):()->FlxState; + /** + * A flag for checking if you can change states or not. + */ + var _requestState:Bool = true; + /** * A flag for keeping track of whether a game reset was requested or not. */ var _resetGame:Bool = false; - + #if FLX_RECORD /** * Container for a game replay object. @@ -243,53 +248,48 @@ class FlxGame extends Sprite /** * Instantiate a new game object. * - * @param gameWidth The width of your game in pixels. If `0`, the `Project.xml` width is used. - * If the demensions don't match the `Project.xml`, - * [`scaleMode`](https://api.haxeflixel.com/flixel/system/scaleModes/index.html) - * will determine the actual display size of the game. - * @param gameHeight The height of your game in pixels. If `0`, the `Project.xml` height is used. - * If the demensions don't match the `Project.xml`, - * [`scaleMode`](https://api.haxeflixel.com/flixel/system/scaleModes/index.html) - * will determine the actual display size of the game. - * @param initialState The class name of the state you want to create and switch to first (e.g. `MenuState`). - * @param updateFramerate How frequently the game should update. Default is 60 fps. - * @param drawFramerate Sets the actual display / draw framerate for the game. Default is 60 fps. - * @param skipSplash Whether you want to skip the flixel splash screen with `FLX_NO_DEBUG`. - * @param startFullscreen Whether to start the game in fullscreen mode (desktop targets only). - * - * @see [scale modes](https://api.haxeflixel.com/flixel/system/scaleModes/index.html) - */ - public function new(gameWidth = 0, gameHeight = 0, ?initialState:Class, updateFramerate = 60, drawFramerate = 60, skipSplash = false, - startFullscreen = false) + * @param GameWidth The width of your game in game pixels, not necessarily final display pixels (see `Zoom`). + * If equal to `0`, the window width specified in the `Project.xml` is used. + * @param GameHeight The height of your game in game pixels, not necessarily final display pixels (see `Zoom`). + * If equal to `0`, the window height specified in the `Project.xml` is used. + * @param InitialState The class name of the state you want to create and switch to first (e.g. `MenuState`). + * @param Zoom The default level of zoom for the game's cameras (e.g. `2` = all pixels are now drawn at 2x). + * @param UpdateFramerate How frequently the game should update (default is `60` times per second). + * @param DrawFramerate Sets the actual display / draw framerate for the game (default is `60` times per second). + * @param SkipSplash Whether you want to skip the flixel splash screen with `FLX_NO_DEBUG`. + * @param StartFullscreen Whether to start the game in fullscreen mode (desktop targets only). + */ + public function new(GameWidth:Int = 0, GameHeight:Int = 0, ?InitialState:()->FlxState, Zoom:Float = 1, UpdateFramerate:Int = 60, + DrawFramerate:Int = 60, SkipSplash:Bool = false, StartFullscreen:Bool = false) { super(); #if desktop - _startFullscreen = startFullscreen; + _startFullscreen = StartFullscreen; #end // Super high priority init stuff _inputContainer = new Sprite(); - if (gameWidth == 0) - gameWidth = FlxG.stage.stageWidth; - if (gameHeight == 0) - gameHeight = FlxG.stage.stageHeight; + if (GameWidth == 0) + GameWidth = FlxG.stage.stageWidth; + if (GameHeight == 0) + GameHeight = FlxG.stage.stageHeight; // Basic display and update setup stuff - FlxG.init(this, gameWidth, gameHeight); + FlxG.init(this, GameWidth, GameHeight, Zoom); - FlxG.updateFramerate = updateFramerate; - FlxG.drawFramerate = drawFramerate; + FlxG.updateFramerate = UpdateFramerate; + FlxG.drawFramerate = DrawFramerate; _accumulator = _stepMS; - _skipSplash = skipSplash; + _skipSplash = SkipSplash; #if FLX_RECORD _replay = new FlxReplay(); #end // Then get ready to create the game object for real - _initialState = (initialState == null) ? FlxState : initialState; + _initialState = (InitialState == null) ? FlxState.new : InitialState; addEventListener(Event.ADDED_TO_STAGE, create); } @@ -362,7 +362,6 @@ class FlxGame extends Sprite // Instantiate the initial state resetGame(); - switchState(); if (FlxG.updateFramerate < FlxG.drawFramerate) FlxG.log.warn("FlxG.updateFramerate: The update framerate shouldn't be smaller" + " than the draw framerate, since it can slow down your game."); @@ -423,7 +422,16 @@ class FlxGame extends Sprite #end FlxG.inputs.onFocus(); } - + function set__requestedState(State:()->FlxState) + { + var curState = State(); + if (_requestState && _state != curState) + { + trace("I'm different :D " + curState); + switchState(curState); + } + return State; + } function onFocusLost(event:Event):Void { #if next @@ -524,7 +532,7 @@ class FlxGame extends Sprite { FlxG.vcr.stepRequested = false; } - else if (_state == _requestedState) // don't pause a state switch request + else if (_requestState)// don't pause a state switch request { #if FLX_DEBUG debugger.update(); @@ -583,14 +591,14 @@ class FlxGame extends Sprite if (_skipSplash || FlxSplash.nextState != null) // already played { - _requestedState = cast Type.createInstance(_initialState, []); + _requestedState = _initialState; if (FlxSplash.nextState == null) _gameJustStarted = true; } else { FlxSplash.nextState = _initialState; - _requestedState = new FlxSplash(); + _requestedState = FlxSplash.new; _skipSplash = true; // only play it once } @@ -600,7 +608,6 @@ class FlxGame extends Sprite #end FlxG.reset(); - FlxG.signals.postGameReset.dispatch(); } @@ -609,8 +616,9 @@ class FlxGame extends Sprite * this function handles actual destroying the old state and related processes, * and calls creates on the new state and plugs it into the game object. */ - function switchState():Void + function switchState(_requestedState:FlxState):Void { + _requestState = false; // Basic reset stuff FlxG.cameras.reset(); FlxG.inputs.onStateSwitch(); @@ -647,6 +655,7 @@ class FlxGame extends Sprite #if FLX_DEBUG debugger.console.registerObject("state", _state); #end + _requestState = true; FlxG.signals.postStateSwitch.dispatch(); } @@ -725,9 +734,6 @@ class FlxGame extends Sprite if (!_state.active || !_state.exists) return; - if (_state != _requestedState) - switchState(); - #if FLX_DEBUG if (FlxG.debugger.visible) ticks = getTicks(); From 706f425f6531241f7931554833b38fb71a8f67df Mon Sep 17 00:00:00 2001 From: Victor G <84867412+Cheemsandfriends@users.noreply.github.com> Date: Sun, 14 Aug 2022 14:56:11 +0200 Subject: [PATCH 5/7] Change `switchState()` and `resetState()` --- flixel/FlxG.hx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flixel/FlxG.hx b/flixel/FlxG.hx index 7e8878f338..ad7c11ef09 100644 --- a/flixel/FlxG.hx +++ b/flixel/FlxG.hx @@ -369,7 +369,7 @@ class FlxG public static inline function switchState(nextState:() -> FlxState):Void { if (state.switchTo(nextState())) - game._requestedState = nextState(); + game._requestedState = nextState; } /** @@ -378,7 +378,7 @@ class FlxG */ public static inline function resetState():Void { - switchState(() -> Type.createInstance(Type.getClass(state), [])); + switchState(game._requestedState); } /** From 292fe033dbb4afa75466aee2b382716a6ee50a31 Mon Sep 17 00:00:00 2001 From: Victor G <84867412+Cheemsandfriends@users.noreply.github.com> Date: Sun, 14 Aug 2022 14:57:52 +0200 Subject: [PATCH 6/7] Change `nextState` variable --- flixel/system/FlxSplash.hx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flixel/system/FlxSplash.hx b/flixel/system/FlxSplash.hx index aa1bd05ac9..ea9e7896b1 100644 --- a/flixel/system/FlxSplash.hx +++ b/flixel/system/FlxSplash.hx @@ -15,7 +15,7 @@ import flixel.util.FlxTimer; class FlxSplash extends FlxState { - public static var nextState:Class; + public static var nextState:()->FlxState; /** * @since 4.8.0 @@ -199,7 +199,7 @@ class FlxSplash extends FlxState #end FlxG.stage.removeChild(_sprite); FlxG.stage.removeChild(_text); - FlxG.switchState(() -> Type.createInstance(nextState, [])); + FlxG.switchState(nextState); FlxG.game._gameJustStarted = true; } } From bff148bb11684072539daee1b1de7877ef0dc76e Mon Sep 17 00:00:00 2001 From: Victor G <84867412+Cheemsandfriends@users.noreply.github.com> Date: Sun, 14 Aug 2022 15:00:04 +0200 Subject: [PATCH 7/7] forgot to remove some trace testing --- flixel/FlxGame.hx | 1 - 1 file changed, 1 deletion(-) diff --git a/flixel/FlxGame.hx b/flixel/FlxGame.hx index 63642f86e4..b606d7e6ec 100644 --- a/flixel/FlxGame.hx +++ b/flixel/FlxGame.hx @@ -427,7 +427,6 @@ class FlxGame extends Sprite var curState = State(); if (_requestState && _state != curState) { - trace("I'm different :D " + curState); switchState(curState); } return State;