Skip to content

Commit 2afdd54

Browse files
Merge remote-tracking branch 'flixel/release6' into dev-6.0.0
2 parents 25c84b2 + a292a5b commit 2afdd54

File tree

14 files changed

+112
-173
lines changed

14 files changed

+112
-173
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
6.0.0 (TBD)
2+
#### Changes and improvements:
3+
- `FlxSpritegroup`: Setting `origin` now causes members to pivot around the same point ([#2981](https://github.com/HaxeFlixel/flixel/pull/2981))
4+
- `FlxCamera`: Smoother camera lerping, particularly with non-fixed timesteps ([#2922](https://github.com/HaxeFlixel/flixel/pull/2922))
5+
- `FlxState`: Remove deprecated `switchTo` ([#2733](https://github.com/HaxeFlixel/flixel/pull/2733))Z
6+
- `FlxG`: Add deprecation warning on `switchState` with instances ([#2733](https://github.com/HaxeFlixel/flixel/pull/2733))Z
7+
18
5.6.0 (February 2, 2024)
29

310
#### New features:

flixel/FlxCamera.hx

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ class FlxCamera extends FlxBasic
126126
public var targetOffset(default, null):FlxPoint = FlxPoint.get();
127127

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

136136
/**
137137
* You can assign a "dead zone" to the camera in order to better control its movement.
@@ -1149,6 +1149,7 @@ class FlxCamera extends FlxBasic
11491149
if (target != null)
11501150
{
11511151
updateFollow();
1152+
updateLerp(elapsed);
11521153
}
11531154

11541155
updateScroll();
@@ -1196,7 +1197,7 @@ class FlxCamera extends FlxBasic
11961197
* Updates camera's scroll.
11971198
* Called every frame by camera's `update()` method (if camera's `target` isn't `null`).
11981199
*/
1199-
public function updateFollow():Void
1200+
function updateFollow():Void
12001201
{
12011202
// Either follow the object closely,
12021203
// or double check our deadzone and update accordingly.
@@ -1273,15 +1274,21 @@ class FlxCamera extends FlxBasic
12731274
_lastTargetPosition.y = target.y;
12741275
}
12751276
}
1276-
1277-
if (followLerp >= 60 / FlxG.updateFramerate)
1277+
}
1278+
1279+
function updateLerp(elapsed:Float)
1280+
{
1281+
final boundLerp = FlxMath.bound(followLerp, 0, 1);
1282+
// Adjust lerp based on the current frame rate so lerp is less framerate dependant
1283+
final adjustedLerp = 1.0 - Math.pow(1.0 - boundLerp, elapsed * 60);
1284+
if (adjustedLerp >= 1)
12781285
{
12791286
scroll.copyFrom(_scrollTarget); // no easing
12801287
}
12811288
else
12821289
{
1283-
scroll.x += (_scrollTarget.x - scroll.x) * followLerp * (60 / FlxG.updateFramerate);
1284-
scroll.y += (_scrollTarget.y - scroll.y) * followLerp * (60 / FlxG.updateFramerate);
1290+
scroll.x += (_scrollTarget.x - scroll.x) * adjustedLerp;
1291+
scroll.y += (_scrollTarget.y - scroll.y) * adjustedLerp;
12851292
}
12861293
}
12871294

@@ -1458,29 +1465,23 @@ class FlxCamera extends FlxBasic
14581465
/**
14591466
* Tells this camera object what `FlxObject` to track.
14601467
*
1461-
* @param Target The object you want the camera to track. Set to `null` to not follow anything.
1462-
* @param Style Leverage one of the existing "deadzone" presets. Default is `LOCKON`.
1468+
* @param target The object you want the camera to track. Set to `null` to not follow anything.
1469+
* @param style Leverage one of the existing "deadzone" presets. Default is `LOCKON`.
14631470
* If you use a custom deadzone, ignore this parameter and
14641471
* manually specify the deadzone after calling `follow()`.
1465-
* @param Lerp How much lag the camera should have (can help smooth out the camera movement).
1472+
* @param lerp How much lag the camera should have (can help smooth out the camera movement).
14661473
*/
1467-
public function follow(Target:FlxObject, ?Style:FlxCameraFollowStyle, ?Lerp:Float):Void
1474+
public function follow(target:FlxObject, style = LOCKON, lerp = 1.0):Void
14681475
{
1469-
if (Style == null)
1470-
Style = LOCKON;
1471-
1472-
if (Lerp == null)
1473-
Lerp = 60 / FlxG.updateFramerate;
1474-
1475-
style = Style;
1476-
target = Target;
1477-
followLerp = Lerp;
1476+
this.style = style;
1477+
this.target = target;
1478+
followLerp = lerp;
14781479
var helper:Float;
14791480
var w:Float = 0;
14801481
var h:Float = 0;
14811482
_lastTargetPosition = null;
14821483

1483-
switch (Style)
1484+
switch (style)
14841485
{
14851486
case LOCKON:
14861487
if (target != null)
@@ -1927,11 +1928,6 @@ class FlxCamera extends FlxBasic
19271928
return contained;
19281929
}
19291930

1930-
function set_followLerp(Value:Float):Float
1931-
{
1932-
return followLerp = FlxMath.bound(Value, 0, 60 / FlxG.updateFramerate);
1933-
}
1934-
19351931
function set_width(Value:Int):Int
19361932
{
19371933
if (width != Value && Value > 0)

flixel/FlxG.hx

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -377,34 +377,13 @@ class FlxG
377377
public static inline function switchState(nextState:NextState):Void
378378
{
379379
final stateOnCall = FlxG.state;
380-
381-
if (!nextState.isInstance() || canSwitchTo(cast nextState))
380+
state.startOutro(function()
382381
{
383-
state.startOutro(function()
384-
{
385-
if (FlxG.state == stateOnCall)
386-
game._nextState = nextState;
387-
else
388-
FlxG.log.warn("`onOutroComplete` was called after the state was switched. This will be ignored");
389-
});
390-
}
391-
}
392-
393-
/**
394-
* Calls state.switchTo(nextState) without a deprecation warning.
395-
* This will be removed in Flixel 6.0.0
396-
* @since 5.6.0
397-
*/
398-
@:noCompletion
399-
@:haxe.warning("-WDeprecated")
400-
static function canSwitchTo(nextState:FlxState)
401-
{
402-
#if (haxe < version("4.3.0"))
403-
// Use reflection because @:haxe.warning("-WDeprecated") doesn't work until haxe 4.3
404-
return Reflect.callMethod(state, Reflect.field(state, 'switchTo'), [nextState]);
405-
#else
406-
return state.switchTo(nextState);
407-
#end
382+
if (FlxG.state == stateOnCall)
383+
game._nextState = nextState;
384+
else
385+
FlxG.log.warn("`onOutroComplete` was called after the state was switched. This will be ignored");
386+
});
408387
}
409388

410389
/**
@@ -413,13 +392,7 @@ class FlxG
413392
*/
414393
public static inline function resetState():Void
415394
{
416-
if (state == null || state._constructor == null)
417-
FlxG.log.error("FlxG.resetState was called while switching states");
418-
else if(!state._constructor.isInstance())
419-
switchState(state._constructor);
420-
else
421-
// create new instance here so that state.switchTo is called (for backwards compatibility)
422-
switchState(Type.createInstance(Type.getClass(state), []));
395+
switchState(state._constructor);
423396
}
424397

425398
/**

flixel/FlxGame.hx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ class FlxGame extends Sprite
253253
* [`scaleMode`](https://api.haxeflixel.com/flixel/system/scaleModes/index.html)
254254
* will determine the actual display size of the game.
255255
* @param initialState A constructor for the initial state, ex: `PlayState.new` or `()->new PlayState()`.
256-
* Note: Also allows `Class<FlxState>` for backwards compatibility.
256+
* Note: Before Flixel 6, this took a `Class<FlxState>`, this has been
257+
* deprecated, but is still available, for backwards compatibility.
257258
* @param updateFramerate How frequently the game should update. Default is 60 fps.
258259
* @param drawFramerate Sets the actual display / draw framerate for the game. Default is 60 fps.
259260
* @param skipSplash Whether you want to skip the flixel splash screen with `FLX_NO_DEBUG`.
@@ -628,7 +629,7 @@ class FlxGame extends Sprite
628629

629630
// Finally assign and create the new state
630631
_state = _nextState.createInstance();
631-
_state._constructor = _nextState;
632+
_state._constructor = _nextState.getConstructor();
632633
_nextState = null;
633634

634635
if (_gameJustStarted)
@@ -647,8 +648,8 @@ class FlxGame extends Sprite
647648

648649
FlxG.signals.postStateSwitch.dispatch();
649650
}
650-
651-
function gameStart():Void
651+
652+
function gameStart()
652653
{
653654
FlxG.signals.postGameStart.dispatch();
654655
_gameJustStarted = false;

flixel/FlxState.hx

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ import flixel.util.typeLimit.NextState;
1111
* It is for all intents and purpose a fancy `FlxGroup`. And really, it's not even that fancy.
1212
*/
1313
@:keepSub // workaround for HaxeFoundation/haxe#3749
14-
#if FLX_NO_UNIT_TEST
15-
@:autoBuild(flixel.system.macros.FlxMacroUtil.deprecateOverride("switchTo", "switchTo is deprecated, use startOutro"))
16-
#end
17-
// show deprecation warning when `switchTo` is overriden in dereived classes
1814
class FlxState extends FlxGroup
1915
{
2016
/**
@@ -52,7 +48,7 @@ class FlxState extends FlxGroup
5248
*/
5349
@:allow(flixel.FlxGame)
5450
@:allow(flixel.FlxG)
55-
var _constructor:NextState;
51+
var _constructor:()->FlxState;
5652

5753
/**
5854
* Current substate. Substates also can be nested.
@@ -105,7 +101,7 @@ class FlxState extends FlxGroup
105101
*/
106102
public function create():Void {}
107103

108-
override public function draw():Void
104+
override function draw():Void
109105
{
110106
if (persistentDraw || subState == null)
111107
super.draw();
@@ -186,18 +182,6 @@ class FlxState extends FlxGroup
186182
super.destroy();
187183
}
188184

189-
/**
190-
* Called from `FlxG.switchState()`. If `false` is returned, the state
191-
* switch is cancelled - the default implementation returns `true`.
192-
*
193-
* Useful for customizing state switches, e.g. for transition effects.
194-
*/
195-
@:deprecated("switchTo is deprecated, use startOutro")
196-
public function switchTo(nextState:FlxState):Bool
197-
{
198-
return true;
199-
}
200-
201185
/**
202186
* Called from `FlxG.switchState()`, when `onOutroComplete` is called, the actual state
203187
* switching will happen.

flixel/group/FlxSpriteGroup.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ class FlxTypedSpriteGroup<T:FlxSprite> extends FlxSprite
10601060
Sprite.offset.copyFrom(Offset);
10611061

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

10651065
inline function scaleTransform(Sprite:FlxSprite, Scale:FlxPoint)
10661066
Sprite.scale.copyFrom(Scale);

flixel/util/typeLimit/NextState.hx

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import flixel.FlxState;
3535
abstract NextState(Dynamic)
3636
{
3737
@:from
38-
// @:deprecated("use `MyState.new` or `()->new MyState()` instead of `new MyState()`)") // wait until 6.0.0
38+
@:deprecated("use `MyState.new` or `()->new MyState()` instead of `new MyState()`)")
3939
public static function fromState(state:FlxState):NextState
4040
{
4141
return cast state;
@@ -47,42 +47,25 @@ abstract NextState(Dynamic)
4747
return cast func;
4848
}
4949

50-
@:allow(flixel.FlxG)
51-
inline function isInstance():Bool
52-
{
53-
return this is FlxState;
54-
}
55-
56-
@:allow(flixel.FlxG)
57-
inline function isClass():Bool
58-
{
59-
return this is Class;
60-
}
61-
6250
public function createInstance():FlxState
6351
{
64-
if (isInstance())
52+
if (this is FlxState)
6553
return cast this;
66-
else if (isClass())
54+
else if (this is Class)
6755
return Type.createInstance(this, []);
6856
else
6957
return cast this();
7058
}
7159

7260
public function getConstructor():()->FlxState
7361
{
74-
if (isInstance())
62+
if (this is FlxState)
7563
{
7664
return function ():FlxState
7765
{
7866
return cast Type.createInstance(Type.getClass(this), []);
7967
}
8068
}
81-
else if (isClass())
82-
return function ():FlxState
83-
{
84-
return cast Type.createInstance(this, []);
85-
}
8669
else
8770
return cast this;
8871
}
@@ -91,7 +74,7 @@ abstract NextState(Dynamic)
9174
/**
9275
* A utility type that allows methods to accept multiple types, when dealing with "future" `FlxStates`.
9376
* Prior to haxeFlixel 6, the `FlxGame` constructor took a `FlxState` class which meant initial
94-
`FlxStates`could not have constructor args. In version 6.0.0 and higher, it now takes a function
77+
* `FlxStates`could not have constructor args. In version 6.0.0 and higher, it now takes a function
9578
* that returns a newly created instance.
9679
*
9780
* ## examples:
@@ -143,4 +126,4 @@ abstract InitialState(Dynamic) to NextState
143126
else
144127
return cast this;
145128
}
146-
}
129+
}

tests/unit/src/TestMain.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class TestMain
2121
public function new()
2222
{
2323
// Flixel was not designed for unit testing so we can only have one instance for now.
24-
Lib.current.stage.addChild(new FlxGame(640, 480, FlxState, 60, 60, true));
24+
Lib.current.stage.addChild(new FlxGame(640, 480, FlxState.new, 60, 60, true));
2525

2626
var suites = new Array<Class<massive.munit.TestSuite>>();
2727
suites.push(TestSuite);

tests/unit/src/flixel/FlxCameraTest.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class FlxCameraTest extends FlxTest
4747
function testDefaultCamerasStateSwitch():Void
4848
{
4949
FlxCamera._defaultCameras = [FlxG.camera];
50-
switchState(new FlxState());
50+
switchState(FlxState.new);
5151

5252
Assert.areEqual(FlxG.cameras.defaults, FlxCamera._defaultCameras);
5353
}

tests/unit/src/flixel/FlxObjectTest.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class FlxObjectTest extends FlxTest
159159

160160
function velocityCollidingWith(ground:FlxObject)
161161
{
162-
switchState(new CollisionState());
162+
switchState(CollisionState.new);
163163

164164
ground.setPosition(0, 10);
165165
object1.setSize(10, 10);

0 commit comments

Comments
 (0)