Added dash and reusable ticker
This commit is contained in:
parent
003cc7c589
commit
2088037606
4 changed files with 97 additions and 13 deletions
|
|
@ -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]
|
||||
|
||||
|
|
|
|||
30
src/Ticker.cs
Normal file
30
src/Ticker.cs
Normal 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
1
src/Ticker.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://csmiba8w3bw7d
|
||||
|
|
@ -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;
|
||||
|
|
@ -129,9 +144,16 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
|
||||
// Water movement
|
||||
if (_isInWater)
|
||||
{
|
||||
if (DashTicker.IsRunning)
|
||||
{
|
||||
velocity = DashDirection * DashSpeed;
|
||||
}
|
||||
else
|
||||
{
|
||||
velocity += _currentDirection.Normalized() * WaterAccel * deltaF;
|
||||
}
|
||||
}
|
||||
// Walking on the floor
|
||||
else if (IsOnFloor())
|
||||
{
|
||||
|
|
@ -163,11 +185,13 @@ 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;
|
||||
}
|
||||
|
||||
if (!DashTicker.IsRunning)
|
||||
{
|
||||
// Drag in water
|
||||
if (_isInWater)
|
||||
{
|
||||
|
|
@ -178,6 +202,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
|||
{
|
||||
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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue