Skip to content

Commit

Permalink
separate maxSpeed and drag types
Browse files Browse the repository at this point in the history
  • Loading branch information
Geokureli committed Apr 21, 2024
1 parent a51d653 commit 71065fd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
29 changes: 20 additions & 9 deletions flixel/FlxObject.hx
Original file line number Diff line number Diff line change
Expand Up @@ -650,10 +650,9 @@ class FlxObject extends FlxBasic
*/
public var acceleration(default, null):FlxPoint;

public var maxSpeedMode = FlxMovementType.NONE;
public var maxSpeedMode = FlxMaxSpeedMode.NONE;

public var dragMode = FlxMovementType.NONE;
public var dragApplyMode:FlxDragApplyMode = INERTIAL;
public var dragMode = FlxDragMode.NONE;
public var angularDragApplyMode:FlxDragApplyMode = INERTIAL;

/**
Expand Down Expand Up @@ -847,7 +846,7 @@ class FlxObject extends FlxBasic
{
velocity = FlxPoint.get();
acceleration = FlxPoint.get();
function setDrag(p:FlxPoint) dragMode = XY(p.x, p.y);
function setDrag(p:FlxPoint) dragMode = XY(p.x, p.y, INERTIAL, INERTIAL);
function setMaxSpeed(p:FlxPoint) maxSpeedMode = XY(p.x, p.y);
drag = new FlxCallbackPoint(setDrag, setDrag, setDrag);
maxVelocity = new FlxCallbackPoint(setMaxSpeed, setMaxSpeed, setMaxSpeed);
Expand Down Expand Up @@ -915,7 +914,7 @@ class FlxObject extends FlxBasic
angularVelocity += velocityDelta;

final newVelocity = FlxPoint.get().copyFrom(velocity);
FlxVelocity.computeSpeed2D(elapsed, newVelocity, acceleration, maxSpeedMode, dragMode, dragApplyMode);
FlxVelocity.computeSpeed2D(elapsed, newVelocity, acceleration, maxSpeedMode, dragMode);

final velocityDeltaX = 0.5 * (newVelocity.x - velocity.x);
final velocityDeltaY = 0.5 * (newVelocity.y - velocity.y);
Expand Down Expand Up @@ -1558,15 +1557,27 @@ enum abstract CollisionDragType(Int)
var HEAVIER = 3;
}

enum FlxMovementType
enum FlxMaxSpeedMode
{
/** Along one axis, usually in the direction of movement */
/** The magnitude of velocity is capped */
LINEAR(value:Float);

/** Along two independant axes */
/** Each axis is capped independantly */
XY(x:Float, y:Float);

/** Shortcut for 0 */
/** No max speed */
NONE;
}

enum FlxDragMode
{
/** Along one axis, usually in the direction of movement */
UNIFORM(value:Float, applyMode:FlxDragApplyMode);

/** Drag is applied to each axes separately */
XY(x:Float, y:Float, xApplyMode:FlxDragApplyMode, ?yApplyMode:FlxDragApplyMode);

/** No drag */
NONE;
}

Expand Down
14 changes: 7 additions & 7 deletions flixel/math/FlxVelocity.hx
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ class FlxVelocity
* @return The altered Velocity value.
*/
public static function computeSpeed2D(elapsed:Float, velocity:FlxPoint, acceleration:FlxPoint,
max:FlxMovementType, drag:FlxMovementType, dragApply:FlxDragApplyMode)
max:FlxMaxSpeedMode, drag:FlxDragMode)
{
switch(drag)
{
Expand All @@ -329,12 +329,12 @@ class FlxVelocity
velocity.x += elapsed * acceleration.x;
velocity.y += elapsed * acceleration.y;

case XY(dragX, dragY):
case XY(dragX, dragY, applyX, applyY):

velocity.x = computeSpeed1D(elapsed, velocity.x, acceleration.x, dragX, dragApply);
velocity.y = computeSpeed1D(elapsed, velocity.y, acceleration.y, dragY, dragApply);
velocity.x = computeSpeed1D(elapsed, velocity.x, acceleration.x, dragX, applyX);
velocity.y = computeSpeed1D(elapsed, velocity.y, acceleration.y, dragY, applyY != null ? applyY : applyX);

case LINEAR(linearDrag):
case UNIFORM(linearDrag, dragApply):

final applyDrag = linearDrag > 0 && switch(dragApply)
{
Expand Down Expand Up @@ -366,7 +366,7 @@ class FlxVelocity
return capSpeed2D(velocity, max);
}

public static function capSpeed2D(velocity:FlxPoint, max:FlxMovementType)
public static function capSpeed2D(velocity:FlxPoint, max:FlxMaxSpeedMode)
{
switch(max)
{
Expand Down Expand Up @@ -408,6 +408,6 @@ class FlxVelocity
source.velocity.set(0, 0);

source.acceleration.set(cosA * acceleration, sinA * acceleration);
source.maxVeloctity.set(Math.abs(cosA * maxSpeed), Math.abs(sinA * maxSpeed));
source.maxVelocity.set(Math.abs(cosA * maxSpeed), Math.abs(sinA * maxSpeed));
}
}

0 comments on commit 71065fd

Please sign in to comment.