Skip to content
Merged
Show file tree
Hide file tree
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
41 changes: 7 additions & 34 deletions flixel/FlxG.hx
Original file line number Diff line number Diff line change
Expand Up @@ -377,34 +377,13 @@ class FlxG
public static inline function switchState(nextState:NextState):Void
{
final stateOnCall = FlxG.state;

if (!nextState.isInstance() || canSwitchTo(cast nextState))
state.startOutro(function()
{
state.startOutro(function()
{
if (FlxG.state == stateOnCall)
game._nextState = nextState;
else
FlxG.log.warn("`onOutroComplete` was called after the state was switched. This will be ignored");
});
}
}

/**
* Calls state.switchTo(nextState) without a deprecation warning.
* This will be removed in Flixel 6.0.0
* @since 5.6.0
*/
@:noCompletion
@:haxe.warning("-WDeprecated")
static function canSwitchTo(nextState:FlxState)
{
#if (haxe < version("4.3.0"))
// Use reflection because @:haxe.warning("-WDeprecated") doesn't work until haxe 4.3
return Reflect.callMethod(state, Reflect.field(state, 'switchTo'), [nextState]);
#else
return state.switchTo(nextState);
#end
if (FlxG.state == stateOnCall)
game._nextState = nextState;
else
FlxG.log.warn("`onOutroComplete` was called after the state was switched. This will be ignored");
});
}

/**
Expand All @@ -413,13 +392,7 @@ class FlxG
*/
public static inline function resetState():Void
{
if (state == null || state._constructor == null)
FlxG.log.error("FlxG.resetState was called while switching states");
else if(!state._constructor.isInstance())
switchState(state._constructor);
else
// create new instance here so that state.switchTo is called (for backwards compatibility)
switchState(Type.createInstance(Type.getClass(state), []));
switchState(state._constructor);
}

/**
Expand Down
18 changes: 8 additions & 10 deletions flixel/FlxGame.hx
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ class FlxGame extends Sprite
* [`scaleMode`](https://api.haxeflixel.com/flixel/system/scaleModes/index.html)
* will determine the actual display size of the game.
* @param initialState A constructor for the initial state, ex: `PlayState.new` or `()->new PlayState()`.
* Note: Also allows `Class<FlxState>` for backwards compatibility.
* Note: Before Flixel 6, this took a `Class<FlxState>`, this has been
* deprecated, but is still available, for backwards compatibility.
* @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`.
Expand Down Expand Up @@ -628,7 +629,7 @@ class FlxGame extends Sprite

// Finally assign and create the new state
_state = _nextState.createInstance();
_state._constructor = _nextState;
_state._constructor = _nextState.getConstructor();
_nextState = null;

if (_gameJustStarted)
Expand All @@ -639,7 +640,10 @@ class FlxGame extends Sprite
_state.create();

if (_gameJustStarted)
gameStart();
{
FlxG.signals.postGameStart.dispatch();
_gameJustStarted = false;
}

#if FLX_DEBUG
debugger.console.registerObject("state", _state);
Expand All @@ -648,12 +652,6 @@ class FlxGame extends Sprite
FlxG.signals.postStateSwitch.dispatch();
}

function gameStart():Void
{
FlxG.signals.postGameStart.dispatch();
_gameJustStarted = false;
}

/**
* This is the main game update logic section.
* The `onEnterFrame()` handler is in charge of calling this
Expand Down Expand Up @@ -904,4 +902,4 @@ private class FlxIntroSplash extends FlxSplash
FlxG.game._gameJustStarted = true;
super.startOutro(onOutroComplete);
}
}
}
20 changes: 2 additions & 18 deletions flixel/FlxState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import flixel.util.typeLimit.NextState;
* It is for all intents and purpose a fancy `FlxGroup`. And really, it's not even that fancy.
*/
@:keepSub // workaround for HaxeFoundation/haxe#3749
#if FLX_NO_UNIT_TEST
@:autoBuild(flixel.system.macros.FlxMacroUtil.deprecateOverride("switchTo", "switchTo is deprecated, use startOutro"))
#end
// show deprecation warning when `switchTo` is overriden in dereived classes
class FlxState extends FlxGroup
{
/**
Expand Down Expand Up @@ -52,7 +48,7 @@ class FlxState extends FlxGroup
*/
@:allow(flixel.FlxGame)
@:allow(flixel.FlxG)
var _constructor:NextState;
var _constructor:()->FlxState;

/**
* Current substate. Substates also can be nested.
Expand Down Expand Up @@ -105,7 +101,7 @@ class FlxState extends FlxGroup
*/
public function create():Void {}

override public function draw():Void
override function draw():Void
{
if (persistentDraw || subState == null)
super.draw();
Expand Down Expand Up @@ -186,18 +182,6 @@ class FlxState extends FlxGroup
super.destroy();
}

/**
* Called from `FlxG.switchState()`. If `false` is returned, the state
* switch is cancelled - the default implementation returns `true`.
*
* Useful for customizing state switches, e.g. for transition effects.
*/
@:deprecated("switchTo is deprecated, use startOutro")
public function switchTo(nextState:FlxState):Bool
{
return true;
}

/**
* Called from `FlxG.switchState()`, when `onOutroComplete` is called, the actual state
* switching will happen.
Expand Down
26 changes: 5 additions & 21 deletions flixel/util/typeLimit/NextState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import flixel.FlxState;
abstract NextState(Dynamic)
{
@:from
// @:deprecated("use `MyState.new` or `()->new MyState()` instead of `new MyState()`)") // wait until 6.0.0
@:deprecated("use `MyState.new` or `()->new MyState()` instead of `new MyState()`)")
public static function fromState(state:FlxState):NextState
{
return cast state;
Expand All @@ -47,42 +47,25 @@ abstract NextState(Dynamic)
return cast func;
}

@:allow(flixel.FlxG)
inline function isInstance():Bool
{
return this is FlxState;
}

@:allow(flixel.FlxG)
inline function isClass():Bool
{
return this is Class;
}

public function createInstance():FlxState
{
if (isInstance())
if (this is FlxState)
return cast this;
else if (isClass())
else if (this is Class)
return Type.createInstance(this, []);
else
return cast this();
}

public function getConstructor():()->FlxState
{
if (isInstance())
if (this is FlxState)
{
return function ():FlxState
{
return cast Type.createInstance(Type.getClass(this), []);
}
}
else if (isClass())
return function ():FlxState
{
return cast Type.createInstance(this, []);
}
else
return cast this;
}
Expand Down Expand Up @@ -144,3 +127,4 @@ abstract InitialState(Dynamic) to NextState
return cast this;
}
}

2 changes: 1 addition & 1 deletion tests/unit/src/TestMain.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TestMain
public function new()
{
// Flixel was not designed for unit testing so we can only have one instance for now.
Lib.current.stage.addChild(new FlxGame(640, 480, FlxState, 60, 60, true));
Lib.current.stage.addChild(new FlxGame(640, 480, FlxState.new, 60, 60, true));

var suites = new Array<Class<massive.munit.TestSuite>>();
suites.push(TestSuite);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/src/flixel/FlxCameraTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class FlxCameraTest extends FlxTest
function testDefaultCamerasStateSwitch():Void
{
FlxCamera._defaultCameras = [FlxG.camera];
switchState(new FlxState());
switchState(FlxState.new);

Assert.areEqual(FlxG.cameras.defaults, FlxCamera._defaultCameras);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/src/flixel/FlxObjectTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class FlxObjectTest extends FlxTest

function velocityCollidingWith(ground:FlxObject)
{
switchState(new CollisionState());
switchState(CollisionState.new);

ground.setPosition(0, 10);
object1.setSize(10, 10);
Expand Down
61 changes: 12 additions & 49 deletions tests/unit/src/flixel/FlxStateTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ class FlxStateTest extends FlxTest
@Ignore // TODO: investigate
function testSwitchState()
{
final state = new FlxState();

Assert.areNotEqual(state, FlxG.state);
switchState(state);
Assert.areEqual(state, FlxG.state);

// Make sure this compiles
switchState(FlxState.new);

var nextState:FlxState = null;
function createState()
{
Expand All @@ -37,19 +28,20 @@ class FlxStateTest extends FlxTest
}

@Test
function testResetStateInstance()
@:haxe.warning("-WDeprecated")
function testResetStateLegacy()
{
var state = new TestState();
switchState(state);
switchState(TestState.new);
var state = FlxG.state;
Assert.areEqual(state, FlxG.state);

resetState();
Assert.areNotEqual(state, FlxG.state);
Assert.isTrue((FlxG.state is TestState));
Assert.isTrue(FlxG.state is TestState);
}

@Test
function testResetStateFunction()
function testResetState()
{
var nextState:TestState = null;
function createState()
Expand All @@ -68,28 +60,11 @@ class FlxStateTest extends FlxTest
}

@Test // #1676
function testCancelStateSwitchInstance()
{
var finalState = new FinalStateLegacy();
switchState(finalState);
Assert.areEqual(finalState, FlxG.state);

switchState(new FlxState());
Assert.areEqual(finalState, FlxG.state);

resetState();
Assert.areEqual(finalState, FlxG.state);
}

@Test // #1676
function testCancelStateSwitchFunction()
function testCancelStateSwitch()
{
switchState(FinalState.new);
final finalState = FlxG.state;

switchState(new FlxState());
Assert.areEqual(finalState, FlxG.state);

switchState(FlxState.new);
Assert.areEqual(finalState, FlxG.state);

Expand All @@ -100,27 +75,15 @@ class FlxStateTest extends FlxTest
@Test
function testOutro()
{
var outroState = new OutroState();

FlxG.switchState(outroState);
FlxG.switchState(OutroState.new);
step();
Assert.areEqual(outroState, FlxG.state);
Assert.isType(FlxG.state, OutroState);

FlxG.switchState(new FlxState());
FlxG.switchState(FlxState.new);
step();
Assert.areEqual(outroState, FlxG.state);
Assert.isType(FlxG.state, OutroState);
step();
Assert.areNotEqual(outroState, FlxG.state);

}
}

class FinalStateLegacy extends FlxState
{
/* prevents state switches */
override function switchTo(state)
{
return false;
Assert.isNotType(FlxG.state, OutroState);
}
}

Expand Down
24 changes: 15 additions & 9 deletions tests/unit/src/flixel/FlxSubStateTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,25 @@ class FlxSubStateTest extends FlxTest
@Test // #1971
function testOpenPersistentSubStateFromNewParent()
{
var state1 = new FlxState();
var state2 = new FlxState();
state1.destroySubStates = false;
FlxG.switchState(state1);
FlxG.switchState(FlxStateNoDestroySubState.new.bind(false));
step();
FlxG.state.openSubState(subState1);
step();

Assert.areEqual(state1.subState, subState1);
Assert.areEqual(FlxG.state.subState, subState1);
subState1.close();
step();
Assert.isNull(state1.subState);
Assert.isNull(FlxG.state.subState);

FlxG.switchState(state2);
FlxG.switchState(FlxStateNoDestroySubState.new.bind(true));
step();
FlxG.state.openSubState(subState1);
step();

Assert.areEqual(state2.subState, subState1);
Assert.areEqual(FlxG.state.subState, subState1);
subState1.close();
step();
Assert.isNull(state2.subState);
Assert.isNull(FlxG.state.subState);
}

@Test // #2023
Expand All @@ -87,3 +84,12 @@ class FlxSubStateTest extends FlxTest
Assert.isTrue(closed);
}
}

class FlxStateNoDestroySubState extends FlxState
{
public function new (destroySubStates)
{
super();
this.destroySubStates = destroySubStates;
}
}
Loading