Skip to content

Commit

Permalink
Merge remote-tracking branch 'flixel/release6' into dev-6.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
EliteMasterEric committed Feb 6, 2024
2 parents 25c84b2 + a292a5b commit 2afdd54
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 173 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
6.0.0 (TBD)
#### Changes and improvements:
- `FlxSpritegroup`: Setting `origin` now causes members to pivot around the same point ([#2981](https://github.com/HaxeFlixel/flixel/pull/2981))
- `FlxCamera`: Smoother camera lerping, particularly with non-fixed timesteps ([#2922](https://github.com/HaxeFlixel/flixel/pull/2922))
- `FlxState`: Remove deprecated `switchTo` ([#2733](https://github.com/HaxeFlixel/flixel/pull/2733))Z
- `FlxG`: Add deprecation warning on `switchState` with instances ([#2733](https://github.com/HaxeFlixel/flixel/pull/2733))Z

5.6.0 (February 2, 2024)

#### New features:
Expand Down
54 changes: 25 additions & 29 deletions flixel/FlxCamera.hx
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ class FlxCamera extends FlxBasic
public var targetOffset(default, null):FlxPoint = FlxPoint.get();

/**
* Used to smoothly track the camera as it follows:
* The percent of the distance to the follow `target` the camera moves per 1/60 sec.
* Values are bounded between `0.0` and `60 / FlxG.updateFramerate` for consistency across framerates.
* The maximum value means no camera easing. A value of `0` means the camera does not move.
* The ratio of the distance to the follow `target` the camera moves per 1/60 sec.
* Valid values range from `0.0` to `1.0`. `1.0` means the camera always snaps to its target
* position. `0.5` means the camera always travels halfway to the target position, `0.0` means
* the camera does not move. Generally, the lower the value, the more smooth.
*/
public var followLerp(default, set):Float = 60 / FlxG.updateFramerate;
public var followLerp:Float = 1.0;

/**
* You can assign a "dead zone" to the camera in order to better control its movement.
Expand Down Expand Up @@ -1149,6 +1149,7 @@ class FlxCamera extends FlxBasic
if (target != null)
{
updateFollow();
updateLerp(elapsed);
}

updateScroll();
Expand Down Expand Up @@ -1196,7 +1197,7 @@ class FlxCamera extends FlxBasic
* Updates camera's scroll.
* Called every frame by camera's `update()` method (if camera's `target` isn't `null`).
*/
public function updateFollow():Void
function updateFollow():Void
{
// Either follow the object closely,
// or double check our deadzone and update accordingly.
Expand Down Expand Up @@ -1273,15 +1274,21 @@ class FlxCamera extends FlxBasic
_lastTargetPosition.y = target.y;
}
}

if (followLerp >= 60 / FlxG.updateFramerate)
}

function updateLerp(elapsed:Float)
{
final boundLerp = FlxMath.bound(followLerp, 0, 1);
// Adjust lerp based on the current frame rate so lerp is less framerate dependant
final adjustedLerp = 1.0 - Math.pow(1.0 - boundLerp, elapsed * 60);
if (adjustedLerp >= 1)
{
scroll.copyFrom(_scrollTarget); // no easing
}
else
{
scroll.x += (_scrollTarget.x - scroll.x) * followLerp * (60 / FlxG.updateFramerate);
scroll.y += (_scrollTarget.y - scroll.y) * followLerp * (60 / FlxG.updateFramerate);
scroll.x += (_scrollTarget.x - scroll.x) * adjustedLerp;
scroll.y += (_scrollTarget.y - scroll.y) * adjustedLerp;
}
}

Expand Down Expand Up @@ -1458,29 +1465,23 @@ class FlxCamera extends FlxBasic
/**
* Tells this camera object what `FlxObject` to track.
*
* @param Target The object you want the camera to track. Set to `null` to not follow anything.
* @param Style Leverage one of the existing "deadzone" presets. Default is `LOCKON`.
* @param target The object you want the camera to track. Set to `null` to not follow anything.
* @param style Leverage one of the existing "deadzone" presets. Default is `LOCKON`.
* If you use a custom deadzone, ignore this parameter and
* manually specify the deadzone after calling `follow()`.
* @param Lerp How much lag the camera should have (can help smooth out the camera movement).
* @param lerp How much lag the camera should have (can help smooth out the camera movement).
*/
public function follow(Target:FlxObject, ?Style:FlxCameraFollowStyle, ?Lerp:Float):Void
public function follow(target:FlxObject, style = LOCKON, lerp = 1.0):Void
{
if (Style == null)
Style = LOCKON;

if (Lerp == null)
Lerp = 60 / FlxG.updateFramerate;

style = Style;
target = Target;
followLerp = Lerp;
this.style = style;
this.target = target;
followLerp = lerp;
var helper:Float;
var w:Float = 0;
var h:Float = 0;
_lastTargetPosition = null;

switch (Style)
switch (style)
{
case LOCKON:
if (target != null)
Expand Down Expand Up @@ -1927,11 +1928,6 @@ class FlxCamera extends FlxBasic
return contained;
}

function set_followLerp(Value:Float):Float
{
return followLerp = FlxMath.bound(Value, 0, 60 / FlxG.updateFramerate);
}

function set_width(Value:Int):Int
{
if (width != Value && Value > 0)
Expand Down
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
9 changes: 5 additions & 4 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 @@ -647,8 +648,8 @@ class FlxGame extends Sprite

FlxG.signals.postStateSwitch.dispatch();
}

function gameStart():Void
function gameStart()
{
FlxG.signals.postGameStart.dispatch();
_gameJustStarted = false;
Expand Down
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
2 changes: 1 addition & 1 deletion flixel/group/FlxSpriteGroup.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ class FlxTypedSpriteGroup<T:FlxSprite> extends FlxSprite
Sprite.offset.copyFrom(Offset);

inline function originTransform(Sprite:FlxSprite, Origin:FlxPoint)
Sprite.origin.copyFrom(Origin);
Sprite.origin.set(x + origin.x - Sprite.x, y + origin.y - Sprite.y);

inline function scaleTransform(Sprite:FlxSprite, Scale:FlxPoint)
Sprite.scale.copyFrom(Scale);
Expand Down
29 changes: 6 additions & 23 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 All @@ -91,7 +74,7 @@ abstract NextState(Dynamic)
/**
* A utility type that allows methods to accept multiple types, when dealing with "future" `FlxStates`.
* Prior to haxeFlixel 6, the `FlxGame` constructor took a `FlxState` class which meant initial
`FlxStates`could not have constructor args. In version 6.0.0 and higher, it now takes a function
* `FlxStates`could not have constructor args. In version 6.0.0 and higher, it now takes a function
* that returns a newly created instance.
*
* ## examples:
Expand Down Expand Up @@ -143,4 +126,4 @@ abstract InitialState(Dynamic) to NextState
else
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
Loading

0 comments on commit 2afdd54

Please sign in to comment.