converting to built-in Godot Timer
This commit is contained in:
parent
7d95a97181
commit
c019911ef3
8 changed files with 156 additions and 154 deletions
|
|
@ -32,27 +32,15 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
[Export(PropertyHint.None, "suffix:px/s²")]
|
||||
public float WaterAccel { get; set; } = 1200.0f;
|
||||
|
||||
[Export(PropertyHint.None, "suffix:s")]
|
||||
public float JumpAscentMaxDuration { get; set; } = 0.25f;
|
||||
|
||||
[Export(PropertyHint.None, "suffix:px/s")]
|
||||
public float JumpAscentSpeed { get; set; } = 200.0f;
|
||||
|
||||
[Export(PropertyHint.None, "suffix:s")]
|
||||
public float LateJumpLimitDuration { get; set; } = 0.1f;
|
||||
|
||||
[Export(PropertyHint.None, "suffix:px/s")]
|
||||
public float SlashSpeedBoost { get; set; } = 90.0f;
|
||||
|
||||
[Export(PropertyHint.None, "suffix:s")]
|
||||
public float DashDuration { get; set; } = 0.15f;
|
||||
|
||||
[Export(PropertyHint.None, "suffix:px/s")]
|
||||
public float DashSpeed { get; set; } = 400.0f;
|
||||
|
||||
[Export(PropertyHint.None, "suffix:s")]
|
||||
public float DashCooldownDuration { get; set; } = 0.6f;
|
||||
|
||||
/// <summary>The angle difference from perfect left/right that still counts.</summary>
|
||||
[Export(PropertyHint.Range, "0,360,0.1,radians_as_degrees")]
|
||||
public float SideInputAngle { get; set; } = Mathf.Pi / 4;
|
||||
|
|
@ -66,11 +54,8 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
[Export] public bool CanTurn { get; set; } = true;
|
||||
[Export] public float MaxDashPoints { get; set; } = 2f;
|
||||
|
||||
[Export(PropertyHint.None, "suffix:s")]
|
||||
public float GlobalDamageImmunityDuration { get; set; } = 0.5f;
|
||||
|
||||
private ulong MaxInputBufferTimeMicroseconds => (ulong)(MaxInputBufferTime * 1_000_000);
|
||||
public bool IsJumping => JumpTicker.IsRunning;
|
||||
public bool IsJumping => !JumpTimer.IsStopped();
|
||||
|
||||
/// The direction the player is currently holding.
|
||||
/// This vectors components should always be -1/0/1.
|
||||
|
|
@ -78,22 +63,22 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
|
||||
public bool IsFacingRight { get; private set; } = true;
|
||||
private AnimationNodeStateMachinePlayback _playback = null!;
|
||||
private List<BufferedInput> _inputBuffer = [];
|
||||
public List<BufferedInput> InputBuffer { get; } = [];
|
||||
public bool IsInWater { get; private set; }
|
||||
private float PlatformIgnoreTime { get; set; }
|
||||
public Ticker JumpAllowedTicker { get; } = new();
|
||||
private Ticker WaterJumpAllowedTicker { get; } = new();
|
||||
private Ticker DashTicker { get; } = new();
|
||||
private Ticker DashCooldown { get; } = new();
|
||||
private Ticker JumpTicker { get; } = new();
|
||||
private Vector2 DashDirection { get; set; }
|
||||
public Vector2 DashDirection { get; set; }
|
||||
public float DashPoints { get; set; }
|
||||
public float BankedDashPoints { get; set; }
|
||||
private Ticker GlobalDamageImmunityTicker { get; } = new();
|
||||
|
||||
[Node] private AnimationTree AnimationTree { get; set; } = null!;
|
||||
[Node] private Area2D SubmersedSensor { get; set; } = null!;
|
||||
[Node] private Hitbox DashHitbox { get; set; } = null!;
|
||||
[Node] private Timer JumpTimer { get; set; } = null!;
|
||||
[Node] private Timer JumpAllowedTimer { get; set; } = null!;
|
||||
[Node] private Timer WaterJumpAllowedTimer { get; set; } = null!;
|
||||
[Node] private Timer DashTimer { get; set; } = null!;
|
||||
[Node] private Timer DashCooldownTimer { get; set; } = null!;
|
||||
[Node] private Timer GlobalDamageImmunityTimer { get; set; } = null!;
|
||||
[Node] private Timer PlatformIgnoreTimer { get; set; } = null!;
|
||||
|
||||
public void OnReady()
|
||||
{
|
||||
|
|
@ -102,11 +87,13 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
SetPhysicsProcess(true);
|
||||
Health = MaxHealth;
|
||||
_playback = AnimationTree.Get("parameters/playback").As<AnimationNodeStateMachinePlayback>();
|
||||
DashTicker.Duration = DashDuration;
|
||||
DashCooldown.Duration = DashCooldownDuration;
|
||||
JumpAllowedTicker.Duration = LateJumpLimitDuration;
|
||||
WaterJumpAllowedTicker.Duration = LateJumpLimitDuration;
|
||||
GlobalDamageImmunityTicker.Duration = GlobalDamageImmunityDuration;
|
||||
SubmersedSensor.BodyEntered += OnEnteredWater;
|
||||
SubmersedSensor.BodyExited += OnLeftWater;
|
||||
DashTimer.Timeout += () =>
|
||||
{
|
||||
Velocity *= 0.6f;
|
||||
DashHitbox.StopAttack();
|
||||
};
|
||||
}
|
||||
|
||||
public override void _UnhandledInput(InputEvent @event)
|
||||
|
|
@ -129,7 +116,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
|
||||
if (Action.TryFromEvent(@event, out var action))
|
||||
{
|
||||
_inputBuffer.Add(new()
|
||||
InputBuffer.Add(new()
|
||||
{
|
||||
Direction = _currentDirection,
|
||||
Action = action,
|
||||
|
|
@ -148,13 +135,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
|
||||
if (CanTurn) AdjustFacing();
|
||||
|
||||
if (DashTicker.HasFinishedLastTick)
|
||||
{
|
||||
velocity *= 0.6f;
|
||||
DashHitbox.StopAttack();
|
||||
}
|
||||
|
||||
if (DashTicker.IsRunning)
|
||||
if (!DashTimer.IsStopped())
|
||||
{
|
||||
velocity = DashDirection * DashSpeed;
|
||||
}
|
||||
|
|
@ -187,18 +168,16 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
}
|
||||
|
||||
// Move up while holding jump
|
||||
if (JumpTicker.IsRunning)
|
||||
if (!JumpTimer.IsStopped())
|
||||
{
|
||||
velocity.Y = -JumpAscentSpeed;
|
||||
var overflow = JumpTicker.TickWithOverflow(deltaF);
|
||||
velocity += GetGravity() * overflow;
|
||||
}
|
||||
else if (!IsOnFloor() && !IsInWater && !DashTicker.IsRunning)
|
||||
else if (!IsOnFloor() && !IsInWater && DashTimer.IsStopped())
|
||||
{
|
||||
velocity += GetGravity() * deltaF;
|
||||
}
|
||||
|
||||
if (!DashTicker.IsRunning)
|
||||
if (DashTimer.IsStopped())
|
||||
{
|
||||
// Drag in water
|
||||
if (IsInWater)
|
||||
|
|
@ -206,7 +185,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
velocity += Physics.DragForce(-velocity, 0.02f) * deltaF;
|
||||
}
|
||||
// Drag while in air
|
||||
else if (!IsOnFloor() && !JumpTicker.IsRunning)
|
||||
else if (!IsOnFloor() && JumpTimer.IsStopped())
|
||||
{
|
||||
velocity += Physics.DragForce(-velocity, 0.009f) * deltaF;
|
||||
}
|
||||
|
|
@ -219,10 +198,9 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
}
|
||||
|
||||
Velocity = velocity;
|
||||
if (PlatformIgnoreTime > 0f)
|
||||
if (!PlatformIgnoreTimer.IsStopped())
|
||||
{
|
||||
CollisionMask = 0b1;
|
||||
PlatformIgnoreTime = Mathf.Max(0f, PlatformIgnoreTime - deltaF);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -255,12 +233,8 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
}
|
||||
}
|
||||
|
||||
JumpAllowedTicker.Tick(deltaF);
|
||||
if (IsOnFloor())
|
||||
JumpAllowedTicker.Start();
|
||||
WaterJumpAllowedTicker.Tick(deltaF);
|
||||
DashTicker.Tick(deltaF);
|
||||
DashCooldown.Tick(deltaF);
|
||||
JumpAllowedTimer.Start();
|
||||
{
|
||||
var addedDashPoints = 3f * deltaF;
|
||||
if (IsInWater)
|
||||
|
|
@ -271,9 +245,8 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
}
|
||||
else BankedDashPoints = Mathf.Min(1, BankedDashPoints + 3f * addedDashPoints);
|
||||
}
|
||||
GlobalDamageImmunityTicker.Tick(deltaF);
|
||||
|
||||
if (IsOnCeiling()) JumpTicker.Reset();
|
||||
if (IsOnCeiling()) JumpTimer.Stop();
|
||||
}
|
||||
|
||||
public void OnEnteredWater(Node2D body) => IsInWater = true;
|
||||
|
|
@ -282,7 +255,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
{
|
||||
if (!IsInWater || SubmersedSensor.HasOverlappingBodies()) return;
|
||||
IsInWater = false;
|
||||
WaterJumpAllowedTicker.Start();
|
||||
WaterJumpAllowedTimer.Start();
|
||||
}
|
||||
|
||||
private void AdjustFacing()
|
||||
|
|
@ -330,18 +303,18 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
|
||||
// Clear expired input buffer items
|
||||
while (
|
||||
_inputBuffer.Count > 0
|
||||
&& _inputBuffer[0].Time + MaxInputBufferTimeMicroseconds < currentTime)
|
||||
InputBuffer.Count > 0
|
||||
&& InputBuffer[0].Time + MaxInputBufferTimeMicroseconds < currentTime)
|
||||
{
|
||||
_inputBuffer.RemoveAt(0);
|
||||
InputBuffer.RemoveAt(0);
|
||||
}
|
||||
|
||||
// Check new commands
|
||||
for (var i = 0; i < _inputBuffer.Count; i++)
|
||||
for (var i = 0; i < InputBuffer.Count; i++)
|
||||
{
|
||||
if (MatchInput(_inputBuffer[i]))
|
||||
if (MatchInput(InputBuffer[i]))
|
||||
{
|
||||
_inputBuffer.RemoveRange(0, i + 1);
|
||||
InputBuffer.RemoveRange(0, i + 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -362,7 +335,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
return true;
|
||||
|
||||
case { Action: Action.Jump, IsPressed: true }
|
||||
when JumpAllowedTicker.IsRunning
|
||||
when !JumpAllowedTimer.IsStopped()
|
||||
&& (!IsOnFloor() || input.Direction.Y <= 0):
|
||||
GroundJump();
|
||||
return true;
|
||||
|
|
@ -373,7 +346,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
return true;
|
||||
|
||||
case { Action: Action.Jump, IsPressed: true }
|
||||
when WaterJumpAllowedTicker.IsRunning:
|
||||
when !WaterJumpAllowedTimer.IsStopped():
|
||||
WaterExitJump();
|
||||
return true;
|
||||
|
||||
|
|
@ -387,7 +360,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
return true;
|
||||
|
||||
case { Action: Action.Dash, IsPressed: true }
|
||||
when !DashCooldown.IsRunning && DashPoints >= 1f:
|
||||
when DashCooldownTimer.IsStopped() && DashPoints >= 1f:
|
||||
Dash();
|
||||
return true;
|
||||
|
||||
|
|
@ -396,32 +369,27 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
}
|
||||
}
|
||||
|
||||
private void DropDown()
|
||||
{
|
||||
PlatformIgnoreTime = 0.1f;
|
||||
}
|
||||
private void DropDown() => PlatformIgnoreTimer.Start();
|
||||
|
||||
private void GroundJump()
|
||||
{
|
||||
AdjustFacing();
|
||||
JumpAllowedTicker.Reset();
|
||||
JumpTicker.Duration = JumpAscentMaxDuration;
|
||||
JumpTicker.Start();
|
||||
JumpAllowedTimer.Stop();
|
||||
JumpTimer.Start();
|
||||
}
|
||||
|
||||
private void WallJump()
|
||||
{
|
||||
AdjustFacing();
|
||||
JumpAllowedTicker.Reset();
|
||||
JumpTicker.Duration = JumpAscentMaxDuration;
|
||||
JumpTicker.Start();
|
||||
JumpAllowedTimer.Stop();
|
||||
JumpTimer.Start();
|
||||
var direction = new Vector2(Mathf.Sign(GetWallNormal().X), -.5f).Normalized();
|
||||
Velocity = direction * 200.0f;
|
||||
}
|
||||
|
||||
private void JumpCut()
|
||||
{
|
||||
JumpTicker.Reset();
|
||||
JumpTimer.Stop();
|
||||
// Jump cut only once, if we're still moving up
|
||||
if (Velocity.Y < 0)
|
||||
Velocity = Velocity with { Y = Velocity.Y * 0.1f };
|
||||
|
|
@ -437,17 +405,16 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
private void WaterExitJump()
|
||||
{
|
||||
AdjustFacing();
|
||||
WaterJumpAllowedTicker.Reset();
|
||||
JumpTicker.Duration = JumpAscentMaxDuration * 1.5f;
|
||||
JumpTicker.Start();
|
||||
WaterJumpAllowedTimer.Stop();
|
||||
JumpTimer.Start();
|
||||
}
|
||||
|
||||
private void Dash()
|
||||
{
|
||||
AdjustFacing();
|
||||
DashPoints -= 1f;
|
||||
DashTicker.Start();
|
||||
DashCooldown.Start();
|
||||
DashTimer.Start();
|
||||
DashCooldownTimer.Start();
|
||||
DashDirection = (_currentDirection.IsZeroApprox()
|
||||
? FacingVector
|
||||
: _currentDirection
|
||||
|
|
@ -458,7 +425,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
private static Vector2 To8DirectionVector(Vector2 input)
|
||||
{
|
||||
if (input.IsZeroApprox()) return Vector2.Zero;
|
||||
const float tan225 = 0.414213562373f; // tan(22.5°)
|
||||
const float tan225 = 0.414213562f; // tan(22.5°)
|
||||
Vector2 a = input.Abs(), s = input.Sign();
|
||||
return
|
||||
a.Y < a.X * tan225 ? new(s.X, 0) :
|
||||
|
|
@ -484,9 +451,9 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
|
||||
public void TakeDamage(float amount)
|
||||
{
|
||||
if (GlobalDamageImmunityTicker.IsRunning) return;
|
||||
if (!GlobalDamageImmunityTimer.IsStopped()) return;
|
||||
Health -= amount;
|
||||
GlobalDamageImmunityTicker.Start();
|
||||
GlobalDamageImmunityTimer.Start();
|
||||
}
|
||||
|
||||
public void OnHitboxAdded(Hitbox hitbox) => hitbox.BodyHit += OnEnemyHit;
|
||||
|
|
@ -494,6 +461,11 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
|
||||
IHitboxOwner IProvide<IHitboxOwner>.
|
||||
Value() => this;
|
||||
|
||||
public enum Move
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static class GameActionExtensions
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue