revamped jump
This commit is contained in:
@ -31,7 +31,7 @@ public partial class PlayerController : CharacterBody3D
|
||||
public WallHugSystem WallHugSystem;
|
||||
public PlayerUi PlayerUi;
|
||||
public TextureRect DashIndicator;
|
||||
public ColorRect DashCooldownIndicator;
|
||||
public ColorRect PowerCooldownIndicator;
|
||||
|
||||
private bool _movementEnabled = true;
|
||||
|
||||
@ -52,7 +52,7 @@ public partial class PlayerController : CharacterBody3D
|
||||
private Timer _coyoteTimer;
|
||||
private Timer _timeScaleAimInAirTimer;
|
||||
private Timer _timeAfterDashingTimer;
|
||||
private Timer _dashCooldownTimer;
|
||||
private Timer _powerCooldownTimer;
|
||||
private Timer _empowerTimeDownscale;
|
||||
|
||||
[ExportCategory("Movement")]
|
||||
@ -68,7 +68,21 @@ public partial class PlayerController : CharacterBody3D
|
||||
public float DecelerationSpeedFactorAir = 1.0f;
|
||||
[Export(PropertyHint.Range, "0,10,0.01,or_greater")]
|
||||
public float Weight { get; set; } = 3.0f;
|
||||
|
||||
// Jump
|
||||
[ExportGroup("Jump")]
|
||||
|
||||
// Simple jump
|
||||
[ExportSubgroup("Simple jump")]
|
||||
[Export(PropertyHint.Range, "0,100,1,or_greater")]
|
||||
public float SimpleJumpStartVelocity { get; set; } = 3.0f;
|
||||
[Export(PropertyHint.Range, "0,10,1,or_greater")]
|
||||
public int HangTimeInFrames { get; set; } = 5;
|
||||
[Export(PropertyHint.Range, "1,10,0.1,or_greater")]
|
||||
public float GravityLesseningFactorUpward { get; set; } = 3f;
|
||||
|
||||
// Other jump
|
||||
[ExportSubgroup("Other jump")]
|
||||
[Export(PropertyHint.Range, "0,2,0.01,or_greater")]
|
||||
public float StartVelocity { get; set; } = 1.0f;
|
||||
[Export(PropertyHint.Range, "0.1,10,0.1,or_greater")]
|
||||
@ -138,6 +152,7 @@ public partial class PlayerController : CharacterBody3D
|
||||
private StateChartState _movHanging;
|
||||
private StateChartState _airborne;
|
||||
private StateChartState _coyoteEnabled;
|
||||
private StateChartState _simpleJump;
|
||||
private StateChartState _doubleJumpEnabled;
|
||||
private StateChartState _onWall;
|
||||
private StateChartState _onWallHugCanceled;
|
||||
@ -155,8 +170,8 @@ public partial class PlayerController : CharacterBody3D
|
||||
TweenQueueSystem = GetNode<TweenQueueSystem>("TweenQueueSystem");
|
||||
PlayerUi = GetNode<PlayerUi>("UI");
|
||||
// DashIndicator = GetNode<TextureRect>("%DashIndicator");
|
||||
DashCooldownIndicator = GetNode<ColorRect>("%DashCooldownIndicator");
|
||||
DashCooldownIndicator.Visible = false;
|
||||
PowerCooldownIndicator = GetNode<ColorRect>("%DashCooldownIndicator");
|
||||
PowerCooldownIndicator.Visible = false;
|
||||
EmpoweredActionsLeft = MaxNumberOfDashActions;
|
||||
_targetSpeed = WalkSpeed;
|
||||
|
||||
@ -214,6 +229,7 @@ public partial class PlayerController : CharacterBody3D
|
||||
_movHanging = StateChartState.Of(GetNode("StateChart/Root/Movement/OnWall/Hanging"));
|
||||
_airborne = StateChartState.Of(GetNode("StateChart/Root/Movement/Airborne"));
|
||||
_coyoteEnabled = StateChartState.Of(GetNode("StateChart/Root/Movement/Airborne/CoyoteEnabled"));
|
||||
_simpleJump = StateChartState.Of(GetNode("StateChart/Root/Movement/Jump/SimpleJump"));
|
||||
_doubleJumpEnabled = StateChartState.Of(GetNode("StateChart/Root/Movement/Airborne/DoubleJumpEnabled"));
|
||||
_onWall = StateChartState.Of(GetNode("StateChart/Root/Movement/OnWall"));
|
||||
_onWallHugging = StateChartState.Of(GetNode("StateChart/Root/Movement/OnWall/Hugging"));
|
||||
@ -222,7 +238,7 @@ public partial class PlayerController : CharacterBody3D
|
||||
_falling = StateChartState.Of(GetNode("StateChart/Root/Movement/Airborne/Falling"));
|
||||
// State timers
|
||||
_coyoteTimer = GetNode<Timer>("CoyoteTime");
|
||||
_dashCooldownTimer = GetNode<Timer>("DashCooldown");
|
||||
_powerCooldownTimer = GetNode<Timer>("PowerCooldown");
|
||||
_timeScaleAimInAirTimer = GetNode<Timer>("TimeScaleAimInAir");
|
||||
_timeAfterDashingTimer = GetNode<Timer>("TimeAfterDashing");
|
||||
_empowerTimeDownscale = GetNode<Timer>("EmpowerTimeDownscale");
|
||||
@ -295,33 +311,125 @@ public partial class PlayerController : CharacterBody3D
|
||||
// _empowerOff.StateEntered += EmpowerStopped;
|
||||
// _empowerTimeDownscale.Timeout += EmpowerTimerTimeout;
|
||||
|
||||
_powerFull.StateExited += StartDashCooldown;
|
||||
_powerRecharging.StateEntered += StartDashCooldown;
|
||||
_powerFull.StateEntered += StopDashCooldown;
|
||||
_dashCooldownTimer.Timeout += DashCooldownExpired;
|
||||
_powerFull.StateExited += StartPowerCooldown;
|
||||
_powerRecharging.StateEntered += StartPowerCooldown;
|
||||
_powerFull.StateEntered += StopPowerCooldown;
|
||||
_powerCooldownTimer.Timeout += PowerCooldownExpired;
|
||||
_powerRecharging.StateProcessing += PowerRecharging;
|
||||
_powerExpired.StateProcessing += PowerRecharging;
|
||||
|
||||
_simpleJump.StateEntered += OnSimpleJumpStarted;
|
||||
_simpleJump.StatePhysicsProcessing += HandleSimpleJump;
|
||||
_simpleJump.StateExited += OnSimpleJumpEnded;
|
||||
}
|
||||
|
||||
public void OnInputJumpStarted()
|
||||
{
|
||||
if (CanMantle())
|
||||
{
|
||||
Mantle();
|
||||
return;
|
||||
}
|
||||
|
||||
// if (_grounded.Active || _coyoteEnabled.Active)
|
||||
// {
|
||||
// if (_empowerOn.Active && CanPerformEmpoweredAction())
|
||||
// {
|
||||
// PerformEmpoweredAction();
|
||||
// PerformJump(JumpTypes.JumpFromDash);
|
||||
// _playerState.SendEvent("megajump");
|
||||
// }
|
||||
// }
|
||||
// else if (_doubleJumpEnabled.Active)
|
||||
// if (_empowerOn.Active && CanPerformEmpoweredAction())
|
||||
// {
|
||||
// PerformEmpoweredAction();
|
||||
// PerformJump(JumpTypes.JumpFromDash);
|
||||
// _playerState.SendEvent("megajump");
|
||||
// }
|
||||
// else
|
||||
// PerformJump(JumpTypes.DoubleJump);
|
||||
// else if (_onWall.Active)
|
||||
// JumpFromWall(_empowerOn.Active);
|
||||
|
||||
_playerState.SendEvent("jump");
|
||||
}
|
||||
|
||||
public void OnInputJumpOngoing()
|
||||
{
|
||||
}
|
||||
|
||||
public void OnInputJumpEnded()
|
||||
{
|
||||
_playerState.SendEvent("jump_ended");
|
||||
}
|
||||
|
||||
private int _framesSinceJumpAtApex = 0;
|
||||
public void OnSimpleJumpStarted()
|
||||
{
|
||||
_framesSinceJumpAtApex = 0;
|
||||
|
||||
Velocity = new Vector3(
|
||||
x: Velocity.X,
|
||||
y: SimpleJumpStartVelocity,
|
||||
z: Velocity.Z);
|
||||
}
|
||||
|
||||
public void HandleSimpleJump(float delta)
|
||||
{
|
||||
// Hang time at the top of the jump
|
||||
if (Velocity.Y <= Mathf.Epsilon)
|
||||
{
|
||||
_framesSinceJumpAtApex++;
|
||||
Velocity = new Vector3(
|
||||
x: Velocity.X,
|
||||
y: 0,
|
||||
z: Velocity.Z);
|
||||
}
|
||||
|
||||
// Cancel gravity on jump apex
|
||||
var gravity = CalculateGravityForce() / GravityLesseningFactorUpward;
|
||||
var isAtApex = _framesSinceJumpAtApex > 0;
|
||||
if (isAtApex)
|
||||
{
|
||||
gravity = 0;
|
||||
}
|
||||
// Update velocity accordingly
|
||||
var newVerticalSpeed = Velocity.Y - gravity * delta;
|
||||
Velocity = new Vector3(
|
||||
x: Velocity.X,
|
||||
y: newVerticalSpeed,
|
||||
z: Velocity.Z);
|
||||
|
||||
|
||||
// Move back to Airborne state management when starting to go down again
|
||||
if (_framesSinceJumpAtApex > HangTimeInFrames)
|
||||
_playerState.SendEvent("jump_ended");
|
||||
}
|
||||
|
||||
public void OnSimpleJumpEnded()
|
||||
{
|
||||
}
|
||||
|
||||
public void PowerRecharging(float delta)
|
||||
{
|
||||
var progress = (float) (_dashCooldownTimer.TimeLeft / _dashCooldownTimer.WaitTime);
|
||||
DashCooldownIndicator.SetSize(new Vector2(100 * progress, 10));
|
||||
var progress = (float) (_powerCooldownTimer.TimeLeft / _powerCooldownTimer.WaitTime);
|
||||
PowerCooldownIndicator.SetSize(new Vector2(100 * progress, 10));
|
||||
}
|
||||
|
||||
public void StartDashCooldown()
|
||||
public void StartPowerCooldown()
|
||||
{
|
||||
_dashCooldownTimer.Start();
|
||||
DashCooldownIndicator.Visible = true;
|
||||
_powerCooldownTimer.Start();
|
||||
PowerCooldownIndicator.Visible = true;
|
||||
}
|
||||
|
||||
public void StopDashCooldown()
|
||||
public void StopPowerCooldown()
|
||||
{
|
||||
_dashCooldownTimer.Stop();
|
||||
DashCooldownIndicator.Visible = false;
|
||||
_powerCooldownTimer.Stop();
|
||||
PowerCooldownIndicator.Visible = false;
|
||||
}
|
||||
|
||||
public void DashCooldownExpired()
|
||||
public void PowerCooldownExpired()
|
||||
{
|
||||
EmpoweredActionsLeft += 1;
|
||||
var eventToSend = EmpoweredActionsLeft == MaxNumberOfDashActions ? "fully_charged" : "recharge";
|
||||
@ -372,38 +480,6 @@ public partial class PlayerController : CharacterBody3D
|
||||
OnWeaponThrown();
|
||||
}
|
||||
}
|
||||
public void OnInputJumpPressed()
|
||||
{
|
||||
if (CanMantle())
|
||||
{
|
||||
Mantle();
|
||||
return;
|
||||
}
|
||||
|
||||
if (_grounded.Active || _coyoteEnabled.Active)
|
||||
if (_empowerOn.Active && CanPerformEmpoweredAction())
|
||||
{
|
||||
PerformEmpoweredAction();
|
||||
PerformJump(JumpTypes.JumpFromDash);
|
||||
_playerState.SendEvent("megajump");
|
||||
}
|
||||
else
|
||||
PerformJump(JumpTypes.SimpleJump);
|
||||
else if (_doubleJumpEnabled.Active)
|
||||
if (_empowerOn.Active && CanPerformEmpoweredAction())
|
||||
{
|
||||
PerformEmpoweredAction();
|
||||
PerformJump(JumpTypes.JumpFromDash);
|
||||
_playerState.SendEvent("megajump");
|
||||
}
|
||||
else
|
||||
PerformJump(JumpTypes.DoubleJump);
|
||||
else if (_onWall.Active)
|
||||
JumpFromWall(_empowerOn.Active);
|
||||
|
||||
_playerState.SendEvent("jump");
|
||||
|
||||
}
|
||||
public void OnInputDashPressed()
|
||||
{
|
||||
_playerState.SendEvent("dash");
|
||||
@ -452,15 +528,8 @@ public partial class PlayerController : CharacterBody3D
|
||||
// Stateful logic /////////
|
||||
///////////////////////////
|
||||
|
||||
// Simple states
|
||||
public void OnGrounded()
|
||||
{
|
||||
RestoreEmpoweredActions();
|
||||
}
|
||||
|
||||
private void RestoreEmpoweredActions()
|
||||
{
|
||||
// EmpoweredActionsLeft = MaxNumberOfDashActions;
|
||||
_isWallJumpAvailable = true;
|
||||
}
|
||||
|
||||
@ -508,13 +577,6 @@ public partial class PlayerController : CharacterBody3D
|
||||
var jumpVector = (effectiveJumpDirection.Normalized() + Vector3.Up).Normalized();
|
||||
if (jumpType == JumpTypes.DoubleJump)
|
||||
_canDash = false;
|
||||
|
||||
// var proportionOfTimeGone = _timeAfterDashingTimer.TimeLeft / _timeAfterDashingTimer.WaitTime;
|
||||
// var actualBoost = 1 + MaxJumpBoostAfterDashing * proportionOfTimeGone;
|
||||
// var makeItDouble = actualBoost > 1;
|
||||
// if (makeItDouble && jumpType == MoveSystem.JumpTypes.SimpleJump)
|
||||
// jumpType = MoveSystem.JumpTypes.DoubleJump; // convert simple jump to double if done right after a dash
|
||||
// _timeAfterDashingTimer.Stop();
|
||||
|
||||
bool doesCapsuleHaveCrouchingHeight = CapsuleCollider.IsCrouchingHeight();
|
||||
bool isPlayerDead = HealthSystem.IsDead();
|
||||
@ -668,8 +730,6 @@ public partial class PlayerController : CharacterBody3D
|
||||
|
||||
// Weapon planted anywhere else
|
||||
_playerState.SendEvent("dash_ended");
|
||||
if (isOnFloorCustom())
|
||||
RestoreEmpoweredActions(); // Make sure to restore actions if we're still on the ground
|
||||
}
|
||||
public void OnWeaponThrown()
|
||||
{
|
||||
@ -858,7 +918,7 @@ public partial class PlayerController : CharacterBody3D
|
||||
IsCapsuleHeightLessThanNormal = CapsuleCollider.IsCapsuleHeightLessThanNormal(),
|
||||
CurrentSpeedGreaterThanWalkSpeed = false,
|
||||
BetweenCrouchingAndNormalHeight = CapsuleCollider.IsBetweenCrouchingAndNormalHeight(),
|
||||
Delta = (float)delta
|
||||
Delta = delta
|
||||
};
|
||||
StairsSystem.SlideCameraSmoothBackToOrigin(slideCameraParams);
|
||||
}
|
||||
@ -876,7 +936,7 @@ public partial class PlayerController : CharacterBody3D
|
||||
FieldOfView.FovParameters fovParams = new FieldOfView.FovParameters
|
||||
{
|
||||
IsCrouchingHeight = CapsuleCollider.IsCrouchingHeight(),
|
||||
Delta = (float)delta,
|
||||
Delta = delta,
|
||||
SprintSpeed = WalkSpeed,
|
||||
Velocity = Velocity
|
||||
};
|
||||
|
Reference in New Issue
Block a user