Skip to content

Commit

Permalink
Handle stick.
Browse files Browse the repository at this point in the history
  • Loading branch information
D8H committed Nov 22, 2024
1 parent 985fccb commit 3b81184
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 40 deletions.
27 changes: 25 additions & 2 deletions Extensions/Physics3DBehavior/JsExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -1804,6 +1804,25 @@ module.exports = {
.addParameter('behavior', _('Behavior'), 'PhysicsCharacter3DBehavior')
.setFunctionName('simulateJumpKey');

aut
.addAction(
'SimulateStick',
_('Simulate stick control'),
_('Simulate a stick control.'),
_(
'Simulate a stick control for _PARAM0_ with a _PARAM2_ angle and a _PARAM3_ force'
),
_('Character controls'),
'res/physics3d.svg',
'res/physics3d.svg'
)
.addParameter('object', _('Object'), '', false)
.addParameter('behavior', _('Behavior'), 'PhysicsCharacter3DBehavior')
.addParameter('expression', _('Stick angle (in degrees)'))
.addParameter('expression', _('Stick force (between 0 and 1)'))
.markAsAdvanced()
.setFunctionName('simulateStick');

aut
.addScopedCondition(
'IsMovingEvenALittle',
Expand Down Expand Up @@ -1872,7 +1891,9 @@ module.exports = {
'number',
'CurrentForwardSpeed',
_('Current forward speed'),
_('the current forward speed of the object. The object moves backward with negative values and forward with positive ones'),
_(
'the current forward speed of the object. The object moves backward with negative values and forward with positive ones'
),
_('the current forward speed'),
_('Character state'),
'res/physics3d.svg'
Expand All @@ -1888,7 +1909,9 @@ module.exports = {
'number',
'CurrentSidewaysSpeed',
_('Current sideways speed'),
_('the current sideways speed of the object. The object moves to the left with negative values and to the right with positive ones'),
_(
'the current sideways speed of the object. The object moves to the left with negative values and to the right with positive ones'
),
_('the current sideways speed'),
_('Character state'),
'res/physics3d.svg'
Expand Down
125 changes: 87 additions & 38 deletions Extensions/Physics3DBehavior/PhysicsCharacter3DRuntimeBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ namespace gdjs {
hasPressedLeftKey: boolean = false;
hasPressedJumpKey: boolean = false;
hasJumpKeyBeenConsumed: boolean = false;
private _wasStickUsed: boolean = false;
private _stickAngle: float = 0;
private _stickForce: float = 0;
_currentForwardSpeed: float = 0;
_currentSidewaysSpeed: float = 0;
_currentFallSpeed: float = 0;
Expand Down Expand Up @@ -334,31 +337,52 @@ namespace gdjs {
const oldY = this.character.GetPosition().GetY();
const oldZ = this.character.GetPosition().GetZ();

let targetedForwardSpeed = 0;
// Change the speed according to the player's input.
// TODO Give priority to the last key for faster reaction time.
if (this.hasPressedBackwardKey !== this.hasPressedForwardKey) {
if (this.hasPressedBackwardKey) {
if (this._currentForwardSpeed <= 0) {
this._currentForwardSpeed -= this._forwardAcceleration * timeDelta;
} else {
// Turn back at least as fast as it would stop.
this._currentForwardSpeed -=
Math.max(this._forwardAcceleration, this._forwardDeceleration) *
timeDelta;
}
targetedForwardSpeed = -this._forwardSpeedMax;
} else if (this.hasPressedForwardKey) {
if (this._currentForwardSpeed >= 0) {
this._currentForwardSpeed += this._forwardAcceleration * timeDelta;
} else {
this._currentForwardSpeed +=
Math.max(this._forwardAcceleration, this._forwardDeceleration) *
timeDelta;
}
targetedForwardSpeed = this._forwardSpeedMax;
}
} else if (this._stickForce !== 0) {
targetedForwardSpeed =
-this._forwardSpeedMax *
this._stickForce *
Math.sin(gdjs.toRad(this._stickAngle));
}
// Take deceleration into account only if no key is pressed.
if (this.hasPressedBackwardKey === this.hasPressedForwardKey) {
// Set the speed to 0 if the speed was too low.
if (targetedForwardSpeed < 0) {
if (this._currentForwardSpeed <= targetedForwardSpeed) {
// Reduce the speed to match the stick force.
this._currentForwardSpeed = Math.min(
targetedForwardSpeed,
this._currentForwardSpeed + this._forwardDeceleration * timeDelta
);
} else if (this._currentForwardSpeed <= 0) {
this._currentForwardSpeed -= this._forwardAcceleration * timeDelta;
} else {
// Turn back at least as fast as it would stop.
this._currentForwardSpeed -=
Math.max(this._forwardAcceleration, this._forwardDeceleration) *
timeDelta;
}
} else if (targetedForwardSpeed > 0) {
if (this._currentForwardSpeed >= targetedForwardSpeed) {
// Reduce the speed to match the stick force.
this._currentForwardSpeed = Math.max(
targetedForwardSpeed,
this._currentForwardSpeed - this._forwardDeceleration * timeDelta
);
} else if (this._currentForwardSpeed >= 0) {
this._currentForwardSpeed += this._forwardAcceleration * timeDelta;
} else {
// Turn back at least as fast as it would stop.
this._currentForwardSpeed +=
Math.max(this._forwardAcceleration, this._forwardDeceleration) *
timeDelta;
}
} else {
if (this._currentForwardSpeed < 0) {
this._currentForwardSpeed = Math.max(
this._currentForwardSpeed + this._forwardDeceleration * timeDelta,
Expand All @@ -378,31 +402,49 @@ namespace gdjs {
this._forwardSpeedMax
);

let targetedSidewaysSpeed = 0;
if (this.hasPressedLeftKey !== this.hasPressedRightKey) {
if (this.hasPressedLeftKey) {
if (this._currentSidewaysSpeed <= 0) {
this._currentSidewaysSpeed -=
this._sidewaysAcceleration * timeDelta;
} else {
// Turn back at least as fast as it would stop.
this._currentSidewaysSpeed -=
Math.max(this._sidewaysAcceleration, this._sidewaysDeceleration) *
timeDelta;
}
targetedSidewaysSpeed = -this._sidewaysSpeedMax;
} else if (this.hasPressedRightKey) {
if (this._currentSidewaysSpeed >= 0) {
this._currentSidewaysSpeed +=
this._sidewaysAcceleration * timeDelta;
} else {
this._currentSidewaysSpeed +=
Math.max(this._sidewaysAcceleration, this._sidewaysDeceleration) *
timeDelta;
}
targetedSidewaysSpeed = this._sidewaysSpeedMax;
}
} else if (this._stickForce !== 0) {
targetedSidewaysSpeed =
this._sidewaysSpeedMax *
this._stickForce *
Math.cos(gdjs.toRad(this._stickAngle));
}
// Take deceleration into account only if no key is pressed.
if (this.hasPressedLeftKey === this.hasPressedRightKey) {
// Set the speed to 0 if the speed was too low.
if (targetedSidewaysSpeed < 0) {
if (this._currentSidewaysSpeed <= targetedSidewaysSpeed) {
// Reduce the speed to match the stick force.
this._currentSidewaysSpeed = Math.min(
targetedSidewaysSpeed,
this._currentSidewaysSpeed + this._sidewaysDeceleration * timeDelta
);
} else if (this._currentSidewaysSpeed <= 0) {
this._currentSidewaysSpeed -= this._sidewaysAcceleration * timeDelta;
} else {
// Turn back at least as fast as it would stop.
this._currentSidewaysSpeed -=
Math.max(this._sidewaysAcceleration, this._sidewaysDeceleration) *
timeDelta;
}
} else if (targetedSidewaysSpeed > 0) {
if (this._currentSidewaysSpeed >= targetedSidewaysSpeed) {
// Reduce the speed to match the stick force.
this._currentSidewaysSpeed = Math.max(
targetedSidewaysSpeed,
this._currentSidewaysSpeed - this._sidewaysDeceleration * timeDelta
);
} else if (this._currentSidewaysSpeed >= 0) {
this._currentSidewaysSpeed += this._sidewaysAcceleration * timeDelta;
} else {
this._currentSidewaysSpeed +=
Math.max(this._sidewaysAcceleration, this._sidewaysDeceleration) *
timeDelta;
}
} else {
if (this._currentSidewaysSpeed < 0) {
this._currentSidewaysSpeed = Math.max(
this._currentSidewaysSpeed + this._sidewaysDeceleration * timeDelta,
Expand Down Expand Up @@ -664,6 +706,8 @@ namespace gdjs {
this.hasPressedRightKey = false;
this.hasPressedLeftKey = false;
this.hasPressedJumpKey = false;
this._stickForce = 0;
this._stickAngle = 0;

this._hasReallyMoved =
Math.abs(this.character.GetPosition().GetX() - oldX) >
Expand Down Expand Up @@ -927,6 +971,11 @@ namespace gdjs {
this.hasPressedJumpKey = true;
}

simulateStick(stickAngle: float, stickForce: float) {
this._stickAngle = stickAngle;
this._stickForce = Math.max(0, Math.min(1, stickForce));
}

/**
* Check if the Platformer Object is on a floor.
* @returns Returns true if on a floor and false if not.
Expand Down

0 comments on commit 3b81184

Please sign in to comment.