diff --git a/project.godot b/project.godot index f325b49..daa8747 100644 --- a/project.godot +++ b/project.godot @@ -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) ] } +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] diff --git a/src/Ticker.cs b/src/Ticker.cs new file mode 100644 index 0000000..ba0cc62 --- /dev/null +++ b/src/Ticker.cs @@ -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, + } +} diff --git a/src/Ticker.cs.uid b/src/Ticker.cs.uid new file mode 100644 index 0000000..f4d06a4 --- /dev/null +++ b/src/Ticker.cs.uid @@ -0,0 +1 @@ +uid://csmiba8w3bw7d diff --git a/src/player/Player.cs b/src/player/Player.cs index 1993054..fb626bf 100644 --- a/src/player/Player.cs +++ b/src/player/Player.cs @@ -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; + /// The angle difference from perfect left/right that still counts. [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(); + 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("%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;