Skip to content

stop using static fields for everything in quadtrees #2891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
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))

5.6.0 (TBD)

#### 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 @@ -1147,6 +1147,7 @@ class FlxCamera extends FlxBasic
if (target != null)
{
updateFollow();
updateLerp(elapsed);
}

updateScroll();
Expand Down Expand Up @@ -1194,7 +1195,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 @@ -1271,15 +1272,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 @@ -1456,29 +1463,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 @@ -1925,11 +1926,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
46 changes: 9 additions & 37 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 Expand Up @@ -455,9 +428,8 @@ class FlxG
objectOrGroup2 = null;

FlxQuadTree.divisions = worldDivisions;
final quadTree = FlxQuadTree.recycle(worldBounds.x, worldBounds.y, worldBounds.width, worldBounds.height);
quadTree.load(objectOrGroup1, objectOrGroup2, notifyCallback, processCallback);
final result:Bool = quadTree.execute();
var quadTree = FlxQuadTree.recycle(worldBounds.x, worldBounds.y, worldBounds.width, worldBounds.height);
var result:Bool = quadTree.loadAndExecute(objectOrGroup1, objectOrGroup2, notifyCallback, processCallback);
quadTree.destroy();
return result;
}
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 @@ -1057,7 +1057,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
Loading