Added dash and reusable ticker

This commit is contained in:
Lilith Schier 2026-07-16 15:01:46 +02:00
parent 003cc7c589
commit 2088037606
4 changed files with 97 additions and 13 deletions

30
src/Ticker.cs Normal file
View file

@ -0,0 +1,30 @@
namespace Dancer;
public sealed class Ticker
{
[Export]
public float Duration { get; set; }
public float RemainingTime { get; private set; }
public bool IsRunning => RemainingTime > 0f;
public bool HasFinishedLastTick { get; private set; }
public Result Tick(float delta)
{
HasFinishedLastTick = false;
if (RemainingTime == 0f) return Result.NotRunning;
RemainingTime -= delta;
if (RemainingTime > 0f) return Result.Running;
RemainingTime = 0f;
HasFinishedLastTick = true;
return Result.Finished;
}
public void Start() => RemainingTime = Duration;
public enum Result
{
Running,
Finished,
NotRunning,
}
}

1
src/Ticker.cs.uid Normal file
View file

@ -0,0 +1 @@
uid://csmiba8w3bw7d

View file

@ -48,6 +48,15 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
[Export(PropertyHint.None, "suffix: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;
@ -77,6 +86,9 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
private bool _isInWater;
private float PlatformIgnoreTime { get; set; }
private float WaterJumpAllowedTime { get; set; }
private Ticker DashTicker { get; set; } = new();
private Ticker DashCooldown { get; set; } = new();
private Vector2 DashDirection { get; set; }
[Node] private AnimationTree AnimationTree { get; set; } = null!;
[Node] private Area2D SubmersedSensor { get; set; } = null!;
@ -87,6 +99,8 @@ 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;
}
public override void _UnhandledInput(InputEvent @event)
@ -107,7 +121,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
"move_down"));
}
if (@event.IsAction("jump") || @event.IsAction("attack"))
if (@event.IsAction("jump") || @event.IsAction("attack") || @event.IsAction("dash"))
{
_inputBuffer.Add(new()
{
@ -121,6 +135,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
public void OnPhysicsProcess(double delta)
{
HandleBufferedInputs();
var deltaF = (float)delta;
// Temporary copy of the velocity for easier editing.
var velocity = Velocity;
@ -130,7 +145,14 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
// Water movement
if (_isInWater)
{
velocity += _currentDirection.Normalized() * WaterAccel * deltaF;
if (DashTicker.IsRunning)
{
velocity = DashDirection * DashSpeed;
}
else
{
velocity += _currentDirection.Normalized() * WaterAccel * deltaF;
}
}
// Walking on the floor
else if (IsOnFloor())
@ -163,20 +185,23 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
if (JumpAscentRemainingTime < 0)
velocity += GetGravity() * -JumpAscentRemainingTime;
}
else if (!IsOnFloor() && !_isInWater)
else if (!IsOnFloor() && !_isInWater && !DashTicker.IsRunning)
{
velocity += GetGravity() * deltaF;
}
// Drag in water
if (_isInWater)
if (!DashTicker.IsRunning)
{
velocity += Physics.DragForce(-velocity, 0.02f) * deltaF;
}
// Drag while in air
else if (!IsOnFloor() && JumpAscentRemainingTime <= 0)
{
velocity += Physics.DragForce(-velocity, 0.009f) * deltaF;
// Drag in water
if (_isInWater)
{
velocity += Physics.DragForce(-velocity, 0.02f) * deltaF;
}
// Drag while in air
else if (!IsOnFloor() && JumpAscentRemainingTime <= 0)
{
velocity += Physics.DragForce(-velocity, 0.009f) * deltaF;
}
}
if (IsHoldingWall())
@ -203,6 +228,8 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
JumpAllowedTime = LateJumpTimeLimit;
WaterJumpAllowedTime = Mathf.Max(0, WaterJumpAllowedTime - deltaF);
DashTicker.Tick(deltaF);
DashCooldown.Tick(deltaF);
if (IsOnCeiling())
{
@ -228,6 +255,8 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
_isFacingRight = true;
}
private Vector2 FacingVector => _isFacingRight ? Vector2.Right : Vector2.Left;
private bool IsHoldingWall()
{
return
@ -239,7 +268,6 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
public override void _Process(double delta)
{
HandleBufferedInputs();
GetNode<Node2D>("%Body").Scale = new(_isFacingRight ? 1 : -1, 1);
}
@ -297,6 +325,14 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
inputFound = true;
Attack();
}
else if (input.Event.IsActionPressed("dash"))
{
if (_isInWater && !DashCooldown.IsRunning)
{
inputFound = true;
Dash();
}
}
if (inputFound)
{
@ -351,9 +387,20 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
IsJumping = true;
}
private void Dash()
{
AdjustFacing();
DashTicker.Start();
DashCooldown.Start();
DashDirection = (_currentDirection.IsZeroApprox()
? FacingVector
: _currentDirection
).Normalized();
}
private static Vector2 To8DirectionVector(Vector2 input)
{
if (input.IsEqualApprox(Vector2.Zero)) return Vector2.Zero;
if (input.IsZeroApprox()) return Vector2.Zero;
var angle = input.Angle();
const float eighthCircle = float.Pi / 4;
const float sixteenthCircle = float.Pi / 8;