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

View file

@ -74,6 +74,12 @@ attack={
, Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":1,"position":Vector2(207, 11),"global_position":Vector2(211, 49),"factor":1.0,"button_index":1,"canceled":false,"pressed":true,"double_click":false,"script":null) , Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":1,"position":Vector2(207, 11),"global_position":Vector2(211, 49),"factor":1.0,"button_index":1,"canceled":false,"pressed":true,"double_click":false,"script":null)
] ]
} }
dash={
"deadzone": 0.2,
"events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":5,"axis_value":1.0,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194325,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}
[layer_names] [layer_names]

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