From c019911ef35d937f289b304710d3465fe3ab7759 Mon Sep 17 00:00:00 2001 From: Lilith Schier Date: Sat, 1 Aug 2026 17:51:54 +0200 Subject: [PATCH] converting to built-in Godot Timer --- script_templates/Node/AutoNode.cs.uid | 2 +- src/Ticker.cs | 54 ---------- src/Ticker.cs.uid | 1 - src/combat/Attack.cs | 1 + src/player/Player.cs | 136 ++++++++++---------------- src/player/attack.aseprite | Bin 743 -> 0 bytes src/player/hair.aseprite | Bin 383 -> 0 bytes src/player/player.tscn | 116 +++++++++++++++++++--- 8 files changed, 156 insertions(+), 154 deletions(-) delete mode 100644 src/Ticker.cs delete mode 100644 src/Ticker.cs.uid delete mode 100644 src/player/attack.aseprite delete mode 100644 src/player/hair.aseprite diff --git a/script_templates/Node/AutoNode.cs.uid b/script_templates/Node/AutoNode.cs.uid index d70bd49..bcd6dce 100644 --- a/script_templates/Node/AutoNode.cs.uid +++ b/script_templates/Node/AutoNode.cs.uid @@ -1 +1 @@ -uid://dqlh48eqmee0s +uid://3n2m6dvndkh3 diff --git a/src/Ticker.cs b/src/Ticker.cs deleted file mode 100644 index 00abaa1..0000000 --- a/src/Ticker.cs +++ /dev/null @@ -1,54 +0,0 @@ -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 float LastTickOverflow { get; private set; } - - - public void Start() => RemainingTime = Duration; - - public Result Tick(float delta) - { - TickInternal(delta); - return - RemainingTime > 0f ? Result.Running : - HasFinishedLastTick ? Result.Finished : - Result.NotRunning; - } - - public float TickWithOverflow(float delta) - { - TickInternal(delta); - return LastTickOverflow; - } - - public float Reset() - { - var result = RemainingTime; - RemainingTime = 0f; - return result; - } - - private void TickInternal(float delta) - { - HasFinishedLastTick = false; - LastTickOverflow = 0f; - if (RemainingTime == 0f) return; - RemainingTime -= delta; - if (RemainingTime > 0f) return; - LastTickOverflow = -RemainingTime; - RemainingTime = 0f; - HasFinishedLastTick = true; - } - - public enum Result - { - Running, - Finished, - NotRunning, - } -} diff --git a/src/Ticker.cs.uid b/src/Ticker.cs.uid deleted file mode 100644 index f4d06a4..0000000 --- a/src/Ticker.cs.uid +++ /dev/null @@ -1 +0,0 @@ -uid://csmiba8w3bw7d diff --git a/src/combat/Attack.cs b/src/combat/Attack.cs index 2d61353..1bc348f 100644 --- a/src/combat/Attack.cs +++ b/src/combat/Attack.cs @@ -1,5 +1,6 @@ namespace Dancer; + public sealed class Attack { public required Node2D Attacker { get; init; } diff --git a/src/player/Player.cs b/src/player/Player.cs index 3cb268e..5cc90ac 100644 --- a/src/player/Player.cs +++ b/src/player/Player.cs @@ -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; - /// 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; @@ -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 _inputBuffer = []; + public List 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(); - 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. Value() => this; + + public enum Move + { + + } } public static class GameActionExtensions diff --git a/src/player/attack.aseprite b/src/player/attack.aseprite deleted file mode 100644 index 69fd86cf9cb77107f518641db42dffb8244bc747..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 743 zcmaFP#K7=iDKmoug8~pTGB7Zt05Jjp#Xy1#0zgO$Uzb#DJJx0c<6b zby+|*3&?c}K!%cniK~W9nx$cXnDzZawVMml-W{Ix`;*zsrvX{}49utGd1vYed8-)N zSlZi{7$h`B=RHiG{XhQflhe2V{Qv)d_S)?&b?up1S&?BuWlmjdjEruECtt7L)j!>< zE8jpKWRn6jkc5H%Kn6R5Phw?ik%D0xki)6~3JxZQ|G=Pw07V8thKf1K2@7;rSQmY; z?_Jw)ySeY<$wmXC1xsfa?3_HU>~RtIHiHFAZCma*WdDEs$y~Lt;k-@Jo8|NJ1K+Bo z*xO$E=WxUKL|xSL>Y6+Wp0dlA;$psCzWlCnlYF?%<;(BCY!hDX{!-#R(B@6^E|@F= zTKnrGBRJ4NK{W}8A+BOTa+NBBFwj+JFMD$xP~c$+@V~cPv`XJwoN=L%>`-i4$u1i$!zA+fUJE6=2P;#GxdYKRg7#b z?d?kp5}Km(9wyKJAAk1A>Dzz)|NlRG?e>-?dMmNKguUGHtpYGL_ zZy*n{Nr4$i!T`vH>Hn(;Mk%z#z?s|J|Forbqul{R{Q|jlTN