refactored player input handling
This commit is contained in:
parent
2088037606
commit
761389116a
9 changed files with 216 additions and 235 deletions
|
|
@ -52,3 +52,11 @@ end_of_line = crlf
|
||||||
# Makefiles
|
# Makefiles
|
||||||
[Makefile]
|
[Makefile]
|
||||||
indent_style = tab
|
indent_style = tab
|
||||||
|
|
||||||
|
|
||||||
|
###
|
||||||
|
# C# Compiler Settings
|
||||||
|
|
||||||
|
[*.cs]
|
||||||
|
# Enums in switch expressions are not expected to handle rogue (MyEnum)123 values.
|
||||||
|
dotnet_diagnostic.CS8524.severity = none
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
<Project Sdk="Godot.NET.Sdk/4.7.0">
|
<Project Sdk="Godot.NET.Sdk/4.7.1">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net10.0</TargetFramework>
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
<EnableDynamicLoading>true</EnableDynamicLoading>
|
<EnableDynamicLoading>true</EnableDynamicLoading>
|
||||||
|
|
@ -9,15 +9,15 @@
|
||||||
<PublishAot>true</PublishAot>
|
<PublishAot>true</PublishAot>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Chickensoft.AutoInject" Version="2.13.19" PrivateAssets="all" />
|
<PackageReference Include="Chickensoft.AutoInject" Version="2.13.27" PrivateAssets="all" />
|
||||||
<PackageReference Include="Chickensoft.AutoInject.Analyzers" Version="2.13.19" PrivateAssets="all" OutputItemType="analyzer" />
|
<PackageReference Include="Chickensoft.AutoInject.Analyzers" Version="2.13.27" PrivateAssets="all" OutputItemType="analyzer" />
|
||||||
<PackageReference Include="Chickensoft.Collections" Version="3.1.4" />
|
<PackageReference Include="Chickensoft.Collections" Version="3.1.4" />
|
||||||
<PackageReference Include="Chickensoft.GodotNodeInterfaces" Version="3.0.23" />
|
<PackageReference Include="Chickensoft.GodotNodeInterfaces" Version="3.0.29" />
|
||||||
<PackageReference Include="Chickensoft.Introspection" Version="3.0.3" />
|
<PackageReference Include="Chickensoft.Introspection" Version="3.0.3" />
|
||||||
<PackageReference Include="Chickensoft.Introspection.Generator" Version="3.0.3" PrivateAssets="all" OutputItemType="analyzer" />
|
<PackageReference Include="Chickensoft.Introspection.Generator" Version="3.0.3" PrivateAssets="all" OutputItemType="analyzer" />
|
||||||
<PackageReference Include="Chickensoft.LogicBlocks" Version="6.1.1" />
|
<PackageReference Include="Chickensoft.LogicBlocks" Version="6.2.0" />
|
||||||
<PackageReference Include="Chickensoft.LogicBlocks.Auto" Version="6.1.1" />
|
<PackageReference Include="Chickensoft.LogicBlocks.Auto" Version="6.2.0" />
|
||||||
<PackageReference Include="Chickensoft.LogicBlocks.DiagramGenerator" Version="6.1.1" PrivateAssets="all" OutputItemType="analyzer" />
|
<PackageReference Include="Chickensoft.LogicBlocks.DiagramGenerator" Version="6.2.0" PrivateAssets="all" OutputItemType="analyzer" />
|
||||||
<PackageReference Include="Chickensoft.LogicBlocks.Generator" Version="4.2.2" />
|
<PackageReference Include="Chickensoft.LogicBlocks.Generator" Version="4.2.2" />
|
||||||
<PackageReference Include="Chickensoft.Serialization" Version="3.1.0" />
|
<PackageReference Include="Chickensoft.Serialization" Version="3.1.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
#if TOOLS
|
#if TOOLS
|
||||||
|
|
||||||
using Dancer;
|
|
||||||
using Godot.Collections;
|
using Godot.Collections;
|
||||||
|
|
||||||
[Tool]
|
[Tool]
|
||||||
|
|
|
||||||
12
src/ActionName.cs
Normal file
12
src/ActionName.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
namespace Dancer;
|
||||||
|
|
||||||
|
public static class ActionName
|
||||||
|
{
|
||||||
|
public static readonly StringName MoveLeft = "move_left";
|
||||||
|
public static readonly StringName MoveRight = "move_right";
|
||||||
|
public static readonly StringName MoveUp = "move_up";
|
||||||
|
public static readonly StringName MoveDown = "move_down";
|
||||||
|
public static readonly StringName Jump = "jump";
|
||||||
|
public static readonly StringName Attack = "attack";
|
||||||
|
public static readonly StringName Dash = "dash";
|
||||||
|
}
|
||||||
1
src/ActionName.cs.uid
Normal file
1
src/ActionName.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
uid://v5ipnpwr14we
|
||||||
|
|
@ -2,24 +2,48 @@ namespace Dancer;
|
||||||
|
|
||||||
public sealed class Ticker
|
public sealed class Ticker
|
||||||
{
|
{
|
||||||
[Export]
|
[Export] public float Duration { get; set; }
|
||||||
public float Duration { get; set; }
|
|
||||||
public float RemainingTime { get; private set; }
|
public float RemainingTime { get; private set; }
|
||||||
public bool IsRunning => RemainingTime > 0f;
|
public bool IsRunning => RemainingTime > 0f;
|
||||||
public bool HasFinishedLastTick { get; private set; }
|
public bool HasFinishedLastTick { get; private set; }
|
||||||
|
public float LastTickOverflow { get; private set; }
|
||||||
|
|
||||||
|
|
||||||
|
public void Start() => RemainingTime = Duration;
|
||||||
|
|
||||||
public Result Tick(float delta)
|
public Result Tick(float delta)
|
||||||
{
|
{
|
||||||
HasFinishedLastTick = false;
|
TickInternal(delta);
|
||||||
if (RemainingTime == 0f) return Result.NotRunning;
|
return
|
||||||
RemainingTime -= delta;
|
RemainingTime > 0f ? Result.Running :
|
||||||
if (RemainingTime > 0f) return Result.Running;
|
HasFinishedLastTick ? Result.Finished :
|
||||||
RemainingTime = 0f;
|
Result.NotRunning;
|
||||||
HasFinishedLastTick = true;
|
|
||||||
return Result.Finished;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Start() => RemainingTime = Duration;
|
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
|
public enum Result
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Chickensoft.Collections;
|
using Chickensoft.Collections;
|
||||||
|
|
||||||
|
|
@ -45,10 +44,10 @@ public enum Result
|
||||||
|
|
||||||
public enum NodeState
|
public enum NodeState
|
||||||
{
|
{
|
||||||
Idle,
|
|
||||||
Running,
|
Running,
|
||||||
Success,
|
Success,
|
||||||
Failure,
|
Failure,
|
||||||
|
Idle,
|
||||||
}
|
}
|
||||||
|
|
||||||
[Tool]
|
[Tool]
|
||||||
|
|
@ -133,7 +132,6 @@ public static class BehaviorTreeExtensions
|
||||||
Result.Running => NodeState.Running,
|
Result.Running => NodeState.Running,
|
||||||
Result.Success => NodeState.Success,
|
Result.Success => NodeState.Success,
|
||||||
Result.Failure => NodeState.Failure,
|
Result.Failure => NodeState.Failure,
|
||||||
_ => throw new UnreachableException(),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -145,7 +143,6 @@ public static class BehaviorTreeExtensions
|
||||||
NodeState.Running => Result.Running,
|
NodeState.Running => Result.Running,
|
||||||
NodeState.Success => Result.Success,
|
NodeState.Success => Result.Success,
|
||||||
NodeState.Failure => Result.Failure,
|
NodeState.Failure => Result.Failure,
|
||||||
_ => throw new UnreachableException(),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -171,16 +168,11 @@ public sealed class Sequence(Node[] children) : ListNode(children)
|
||||||
var child = Children[childIdx];
|
var child = Children[childIdx];
|
||||||
switch (child.Tick(context))
|
switch (child.Tick(context))
|
||||||
{
|
{
|
||||||
case Result.Running:
|
case (Result.Running or Result.Failure) and var r:
|
||||||
result = Result.Running;
|
result = r;
|
||||||
goto LoopDone;
|
goto LoopDone;
|
||||||
case Result.Success:
|
case Result.Success:
|
||||||
break;
|
break;
|
||||||
case Result.Failure:
|
|
||||||
result = Result.Failure;
|
|
||||||
goto LoopDone;
|
|
||||||
default:
|
|
||||||
throw new UnreachableException();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -213,16 +205,11 @@ public sealed class MemoSequence(Node[] children) : ListNode(children)
|
||||||
var child = Children[childIdx];
|
var child = Children[childIdx];
|
||||||
switch (child.Tick(context))
|
switch (child.Tick(context))
|
||||||
{
|
{
|
||||||
case Result.Running:
|
case (Result.Running or Result.Failure) and var r:
|
||||||
result = Result.Running;
|
result = r;
|
||||||
goto LoopDone;
|
goto LoopDone;
|
||||||
case Result.Success:
|
case Result.Success:
|
||||||
break;
|
break;
|
||||||
case Result.Failure:
|
|
||||||
result = Result.Failure;
|
|
||||||
goto LoopDone;
|
|
||||||
default:
|
|
||||||
throw new UnreachableException();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -248,16 +235,11 @@ public sealed class Selector(Node[] children) : ListNode(children)
|
||||||
var child = Children[childIdx];
|
var child = Children[childIdx];
|
||||||
switch (child.Tick(context))
|
switch (child.Tick(context))
|
||||||
{
|
{
|
||||||
case Result.Running:
|
case (Result.Running or Result.Success) and var r:
|
||||||
result = Result.Running;
|
result = r;
|
||||||
goto LoopDone;
|
|
||||||
case Result.Success:
|
|
||||||
result = Result.Success;
|
|
||||||
goto LoopDone;
|
goto LoopDone;
|
||||||
case Result.Failure:
|
case Result.Failure:
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
throw new UnreachableException();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -295,7 +277,6 @@ public sealed class Invert(Node child) : DecoratorNode(child)
|
||||||
Result.Running => Result.Running,
|
Result.Running => Result.Running,
|
||||||
Result.Success => Result.Failure,
|
Result.Success => Result.Failure,
|
||||||
Result.Failure => Result.Success,
|
Result.Failure => Result.Success,
|
||||||
_ => throw new UnreachableException(),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -306,7 +287,6 @@ public sealed class AlwaysSucceed(Node child) : DecoratorNode(child)
|
||||||
{
|
{
|
||||||
Result.Running => Result.Running,
|
Result.Running => Result.Running,
|
||||||
Result.Success or Result.Failure => Result.Success,
|
Result.Success or Result.Failure => Result.Success,
|
||||||
_ => throw new UnreachableException(),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -317,7 +297,6 @@ public sealed class AlwaysFail(Node child) : DecoratorNode(child)
|
||||||
{
|
{
|
||||||
Result.Running => Result.Running,
|
Result.Running => Result.Running,
|
||||||
Result.Success or Result.Failure => Result.Failure,
|
Result.Success or Result.Failure => Result.Failure,
|
||||||
_ => throw new UnreachableException(),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace Dancer;
|
namespace Dancer;
|
||||||
|
|
||||||
public static class Physics
|
public static class Physics
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,4 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
|
||||||
using Chickensoft.LogicBlocks;
|
|
||||||
using Chickensoft.LogicBlocks.Auto;
|
|
||||||
|
|
||||||
namespace Dancer;
|
namespace Dancer;
|
||||||
|
|
||||||
|
|
@ -37,15 +33,15 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
||||||
public float WaterAccel { get; set; } = 1200.0f;
|
public float WaterAccel { get; set; } = 1200.0f;
|
||||||
|
|
||||||
[Export(PropertyHint.None, "suffix:s")]
|
[Export(PropertyHint.None, "suffix:s")]
|
||||||
public float JumpAscentMaxTime { get; set; } = 0.25f;
|
public float JumpAscentMaxDuration { get; set; } = 0.25f;
|
||||||
|
|
||||||
[Export(PropertyHint.None, "suffix:px/s")]
|
[Export(PropertyHint.None, "suffix:px/s")]
|
||||||
public float JumpAscentSpeed { get; set; } = 200.0f;
|
public float JumpAscentSpeed { get; set; } = 200.0f;
|
||||||
|
|
||||||
[Export(PropertyHint.None, "suffix:s")]
|
[Export(PropertyHint.None, "suffix:s")]
|
||||||
public float LateJumpTimeLimit { get; set; } = 0.1f;
|
public float LateJumpLimitDuration { get; set; } = 0.1f;
|
||||||
|
|
||||||
[Export(PropertyHint.None, "suffix:s")]
|
[Export(PropertyHint.None, "suffix:px/s")]
|
||||||
public float SlashSpeedBoost { get; set; } = 90.0f;
|
public float SlashSpeedBoost { get; set; } = 90.0f;
|
||||||
|
|
||||||
[Export(PropertyHint.None, "suffix:s")]
|
[Export(PropertyHint.None, "suffix:s")]
|
||||||
|
|
@ -72,22 +68,22 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
||||||
[Export] public TileMapLayer WaterTileMap { get; set; } = null!;
|
[Export] public TileMapLayer WaterTileMap { get; set; } = null!;
|
||||||
|
|
||||||
private ulong MaxInputBufferTimeMicroseconds => (ulong)(MaxInputBufferTime * 1_000_000);
|
private ulong MaxInputBufferTimeMicroseconds => (ulong)(MaxInputBufferTime * 1_000_000);
|
||||||
public float JumpAscentRemainingTime { get; private set; }
|
public bool IsJumping => JumpTicker.IsRunning;
|
||||||
public bool IsJumping { get; private set; }
|
|
||||||
public float JumpAllowedTime { get; private set; }
|
|
||||||
|
|
||||||
/// The direction the player is currently holding.
|
/// The direction the player is currently holding.
|
||||||
/// This vectors components should always be -1/0/1.
|
/// This vectors components should always be -1/0/1.
|
||||||
private Vector2 _currentDirection = Vector2.Zero;
|
private Vector2 _currentDirection = Vector2.Zero;
|
||||||
|
|
||||||
private bool _isFacingRight = true;
|
public bool IsFacingRight { get; private set; } = true;
|
||||||
private AnimationNodeStateMachinePlayback _playback = null!;
|
private AnimationNodeStateMachinePlayback _playback = null!;
|
||||||
private List<BufferedInput> _inputBuffer = [];
|
private List<BufferedInput> _inputBuffer = [];
|
||||||
private bool _isInWater;
|
public bool IsInWater { get; private set; }
|
||||||
private float PlatformIgnoreTime { get; set; }
|
private float PlatformIgnoreTime { get; set; }
|
||||||
private float WaterJumpAllowedTime { get; set; }
|
public Ticker JumpAllowedTicker { get; } = new();
|
||||||
private Ticker DashTicker { get; set; } = new();
|
private Ticker WaterJumpAllowedTicker { get; } = new();
|
||||||
private Ticker DashCooldown { get; set; } = new();
|
private Ticker DashTicker { get; } = new();
|
||||||
|
private Ticker DashCooldown { get; } = new();
|
||||||
|
private Ticker JumpTicker { get; } = new();
|
||||||
private Vector2 DashDirection { get; set; }
|
private Vector2 DashDirection { get; set; }
|
||||||
|
|
||||||
[Node] private AnimationTree AnimationTree { get; set; } = null!;
|
[Node] private AnimationTree AnimationTree { get; set; } = null!;
|
||||||
|
|
@ -101,6 +97,8 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
||||||
_playback = AnimationTree.Get("parameters/playback").As<AnimationNodeStateMachinePlayback>();
|
_playback = AnimationTree.Get("parameters/playback").As<AnimationNodeStateMachinePlayback>();
|
||||||
DashTicker.Duration = DashDuration;
|
DashTicker.Duration = DashDuration;
|
||||||
DashCooldown.Duration = DashCooldownDuration;
|
DashCooldown.Duration = DashCooldownDuration;
|
||||||
|
JumpAllowedTicker.Duration = LateJumpLimitDuration;
|
||||||
|
WaterJumpAllowedTicker.Duration = LateJumpLimitDuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void _UnhandledInput(InputEvent @event)
|
public override void _UnhandledInput(InputEvent @event)
|
||||||
|
|
@ -109,27 +107,27 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@event.IsAction("move_left")
|
@event.IsAction(ActionName.MoveLeft)
|
||||||
|| @event.IsAction("move_right")
|
|| @event.IsAction(ActionName.MoveRight)
|
||||||
|| @event.IsAction("move_up")
|
|| @event.IsAction(ActionName.MoveUp)
|
||||||
|| @event.IsAction("move_down"))
|
|| @event.IsAction(ActionName.MoveDown))
|
||||||
{
|
{
|
||||||
_currentDirection = To8DirectionVector(Input.GetVector(
|
_currentDirection = To8DirectionVector(Input.GetVector(
|
||||||
"move_left",
|
ActionName.MoveLeft,
|
||||||
"move_right",
|
ActionName.MoveRight,
|
||||||
"move_up",
|
ActionName.MoveUp,
|
||||||
"move_down"));
|
ActionName.MoveDown));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (@event.IsAction("jump") || @event.IsAction("attack") || @event.IsAction("dash"))
|
if (Action.TryFromEvent(@event, out var action))
|
||||||
{
|
{
|
||||||
_inputBuffer.Add(new()
|
_inputBuffer.Add(new()
|
||||||
{
|
{
|
||||||
Direction = _currentDirection,
|
Direction = _currentDirection,
|
||||||
Event = @event,
|
Action = action,
|
||||||
|
IsPressed = @event.IsPressed(),
|
||||||
Time = Time.GetTicksUsec(),
|
Time = Time.GetTicksUsec(),
|
||||||
});
|
});
|
||||||
HandleBufferedInputs();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -143,7 +141,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
||||||
if (CanTurn) AdjustFacing();
|
if (CanTurn) AdjustFacing();
|
||||||
|
|
||||||
// Water movement
|
// Water movement
|
||||||
if (_isInWater)
|
if (IsInWater)
|
||||||
{
|
{
|
||||||
if (DashTicker.IsRunning)
|
if (DashTicker.IsRunning)
|
||||||
{
|
{
|
||||||
|
|
@ -178,14 +176,13 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move up while holding jump
|
// Move up while holding jump
|
||||||
if (JumpAscentRemainingTime > 0)
|
if (JumpTicker.IsRunning)
|
||||||
{
|
{
|
||||||
velocity.Y = -JumpAscentSpeed;
|
velocity.Y = -JumpAscentSpeed;
|
||||||
JumpAscentRemainingTime -= deltaF;
|
var overflow = JumpTicker.TickWithOverflow(deltaF);
|
||||||
if (JumpAscentRemainingTime < 0)
|
velocity += GetGravity() * overflow;
|
||||||
velocity += GetGravity() * -JumpAscentRemainingTime;
|
|
||||||
}
|
}
|
||||||
else if (!IsOnFloor() && !_isInWater && !DashTicker.IsRunning)
|
else if (!IsOnFloor() && !IsInWater && !DashTicker.IsRunning)
|
||||||
{
|
{
|
||||||
velocity += GetGravity() * deltaF;
|
velocity += GetGravity() * deltaF;
|
||||||
}
|
}
|
||||||
|
|
@ -193,12 +190,12 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
||||||
if (!DashTicker.IsRunning)
|
if (!DashTicker.IsRunning)
|
||||||
{
|
{
|
||||||
// Drag in water
|
// Drag in water
|
||||||
if (_isInWater)
|
if (IsInWater)
|
||||||
{
|
{
|
||||||
velocity += Physics.DragForce(-velocity, 0.02f) * deltaF;
|
velocity += Physics.DragForce(-velocity, 0.02f) * deltaF;
|
||||||
}
|
}
|
||||||
// Drag while in air
|
// Drag while in air
|
||||||
else if (!IsOnFloor() && JumpAscentRemainingTime <= 0)
|
else if (!IsOnFloor() && !JumpTicker.IsRunning)
|
||||||
{
|
{
|
||||||
velocity += Physics.DragForce(-velocity, 0.009f) * deltaF;
|
velocity += Physics.DragForce(-velocity, 0.009f) * deltaF;
|
||||||
}
|
}
|
||||||
|
|
@ -207,7 +204,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
||||||
if (IsHoldingWall())
|
if (IsHoldingWall())
|
||||||
{
|
{
|
||||||
velocity.Y = Mathf.Min(velocity.Y, 5.0f);
|
velocity.Y = Mathf.Min(velocity.Y, 5.0f);
|
||||||
_isFacingRight = Mathf.Sign(GetWallNormal().X) > 0;
|
IsFacingRight = Mathf.Sign(GetWallNormal().X) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Velocity = velocity;
|
Velocity = velocity;
|
||||||
|
|
@ -223,39 +220,34 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
||||||
|
|
||||||
MoveAndSlide();
|
MoveAndSlide();
|
||||||
|
|
||||||
JumpAllowedTime = Mathf.Max(0, JumpAllowedTime - deltaF);
|
JumpAllowedTicker.Tick(deltaF);
|
||||||
if (IsOnFloor())
|
if (IsOnFloor())
|
||||||
JumpAllowedTime = LateJumpTimeLimit;
|
JumpAllowedTicker.Start();
|
||||||
|
WaterJumpAllowedTicker.Tick(deltaF);
|
||||||
WaterJumpAllowedTime = Mathf.Max(0, WaterJumpAllowedTime - deltaF);
|
|
||||||
DashTicker.Tick(deltaF);
|
DashTicker.Tick(deltaF);
|
||||||
DashCooldown.Tick(deltaF);
|
DashCooldown.Tick(deltaF);
|
||||||
|
|
||||||
if (IsOnCeiling())
|
if (IsOnCeiling()) JumpTicker.Reset();
|
||||||
{
|
|
||||||
IsJumping = false;
|
|
||||||
JumpAscentRemainingTime = 0.0f;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void EnteredWater(Node2D body) => _isInWater = true;
|
public void EnteredWater(Node2D body) => IsInWater = true;
|
||||||
|
|
||||||
public void LeftWater(Node2D body)
|
public void LeftWater(Node2D body)
|
||||||
{
|
{
|
||||||
if (!_isInWater || SubmersedSensor.HasOverlappingBodies()) return;
|
if (!IsInWater || SubmersedSensor.HasOverlappingBodies()) return;
|
||||||
_isInWater = false;
|
IsInWater = false;
|
||||||
WaterJumpAllowedTime = LateJumpTimeLimit;
|
WaterJumpAllowedTicker.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AdjustFacing()
|
private void AdjustFacing()
|
||||||
{
|
{
|
||||||
if (_currentDirection.X < 0)
|
if (_currentDirection.X < 0)
|
||||||
_isFacingRight = false;
|
IsFacingRight = false;
|
||||||
else if (_currentDirection.X > 0)
|
else if (_currentDirection.X > 0)
|
||||||
_isFacingRight = true;
|
IsFacingRight = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Vector2 FacingVector => _isFacingRight ? Vector2.Right : Vector2.Left;
|
private Vector2 FacingVector => IsFacingRight ? Vector2.Right : Vector2.Left;
|
||||||
|
|
||||||
private bool IsHoldingWall()
|
private bool IsHoldingWall()
|
||||||
{
|
{
|
||||||
|
|
@ -268,12 +260,26 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
||||||
|
|
||||||
public override void _Process(double delta)
|
public override void _Process(double delta)
|
||||||
{
|
{
|
||||||
GetNode<Node2D>("%Body").Scale = new(_isFacingRight ? 1 : -1, 1);
|
GetNode<Node2D>("%Body").Scale = new(IsFacingRight ? 1 : -1, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly struct BufferedInput
|
||||||
|
{
|
||||||
|
public Vector2 Direction { get; init; }
|
||||||
|
public required Action Action { get; init; }
|
||||||
|
public required bool IsPressed { get; init; }
|
||||||
|
public ulong Time { get; init; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum Action
|
||||||
|
{
|
||||||
|
Jump,
|
||||||
|
Attack,
|
||||||
|
Dash,
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandleBufferedInputs()
|
private void HandleBufferedInputs()
|
||||||
{
|
{
|
||||||
var inputFound = false;
|
|
||||||
var currentTime = Time.GetTicksUsec();
|
var currentTime = Time.GetTicksUsec();
|
||||||
|
|
||||||
// Clear expired input buffer items
|
// Clear expired input buffer items
|
||||||
|
|
@ -287,54 +293,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
||||||
// Check new commands
|
// Check new commands
|
||||||
for (var i = 0; i < _inputBuffer.Count; i++)
|
for (var i = 0; i < _inputBuffer.Count; i++)
|
||||||
{
|
{
|
||||||
var input = _inputBuffer[i];
|
if (MatchInput(_inputBuffer[i]))
|
||||||
|
|
||||||
if (input.Event.IsAction("jump"))
|
|
||||||
{
|
|
||||||
if (input.Event.IsPressed() && input.Direction.Y > 0 && IsOnFloor())
|
|
||||||
{
|
|
||||||
inputFound = true;
|
|
||||||
DropDown();
|
|
||||||
}
|
|
||||||
else if (
|
|
||||||
input.Event.IsPressed()
|
|
||||||
&& JumpAllowedTime > 0
|
|
||||||
&& (!IsOnFloor() || input.Direction.Y <= 0)
|
|
||||||
&& !_isInWater)
|
|
||||||
{
|
|
||||||
inputFound = true;
|
|
||||||
GroundJump();
|
|
||||||
}
|
|
||||||
else if (input.Event.IsPressed() && IsOnWall())
|
|
||||||
{
|
|
||||||
inputFound = true;
|
|
||||||
WallJump();
|
|
||||||
}
|
|
||||||
else if (input.Event.IsPressed() && WaterJumpAllowedTime > 0)
|
|
||||||
{
|
|
||||||
inputFound = true;
|
|
||||||
WaterExitJump();
|
|
||||||
}
|
|
||||||
else if (input.Event.IsReleased() && IsJumping)
|
|
||||||
{
|
|
||||||
JumpCut();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (input.Event.IsActionPressed("attack"))
|
|
||||||
{
|
|
||||||
inputFound = true;
|
|
||||||
Attack();
|
|
||||||
}
|
|
||||||
else if (input.Event.IsActionPressed("dash"))
|
|
||||||
{
|
|
||||||
if (_isInWater && !DashCooldown.IsRunning)
|
|
||||||
{
|
|
||||||
inputFound = true;
|
|
||||||
Dash();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inputFound)
|
|
||||||
{
|
{
|
||||||
_inputBuffer = _inputBuffer.GetRange(i + 1, _inputBuffer.Count - i - 1);
|
_inputBuffer = _inputBuffer.GetRange(i + 1, _inputBuffer.Count - i - 1);
|
||||||
break;
|
break;
|
||||||
|
|
@ -342,6 +301,56 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Select an action based on a buffered input.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="input"></param>
|
||||||
|
/// <returns>true if inputs up to this one should be consumed.</returns>
|
||||||
|
private bool MatchInput(BufferedInput input)
|
||||||
|
{
|
||||||
|
switch (input)
|
||||||
|
{
|
||||||
|
case { Action: Action.Jump, IsPressed: true, Direction.Y: > 0f }
|
||||||
|
when IsOnFloor():
|
||||||
|
DropDown();
|
||||||
|
return true;
|
||||||
|
|
||||||
|
case { Action: Action.Jump, IsPressed: true }
|
||||||
|
when JumpAllowedTicker.IsRunning
|
||||||
|
&& (!IsOnFloor() || input.Direction.Y <= 0)
|
||||||
|
&& !IsInWater:
|
||||||
|
GroundJump();
|
||||||
|
return true;
|
||||||
|
|
||||||
|
case { Action: Action.Jump, IsPressed: true }
|
||||||
|
when IsOnWall():
|
||||||
|
WallJump();
|
||||||
|
return true;
|
||||||
|
|
||||||
|
case { Action: Action.Jump, IsPressed: true }
|
||||||
|
when WaterJumpAllowedTicker.IsRunning:
|
||||||
|
WaterExitJump();
|
||||||
|
return true;
|
||||||
|
|
||||||
|
case { Action: Action.Jump, IsPressed: false }
|
||||||
|
when IsJumping:
|
||||||
|
JumpCut();
|
||||||
|
return false;
|
||||||
|
|
||||||
|
case { Action: Action.Attack, IsPressed: true }:
|
||||||
|
Attack();
|
||||||
|
return true;
|
||||||
|
|
||||||
|
case { Action: Action.Dash, IsPressed: true }
|
||||||
|
when (IsInWater && !DashCooldown.IsRunning):
|
||||||
|
Dash();
|
||||||
|
return true;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void DropDown()
|
private void DropDown()
|
||||||
{
|
{
|
||||||
PlatformIgnoreTime = 0.1f;
|
PlatformIgnoreTime = 0.1f;
|
||||||
|
|
@ -350,23 +359,22 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
||||||
private void GroundJump()
|
private void GroundJump()
|
||||||
{
|
{
|
||||||
AdjustFacing();
|
AdjustFacing();
|
||||||
JumpAllowedTime = 0.0f;
|
JumpAllowedTicker.Reset();
|
||||||
JumpAscentRemainingTime = JumpAscentMaxTime;
|
JumpTicker.Duration = JumpAscentMaxDuration;
|
||||||
IsJumping = true;
|
JumpTicker.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WallJump()
|
private void WallJump()
|
||||||
{
|
{
|
||||||
AdjustFacing();
|
AdjustFacing();
|
||||||
JumpAllowedTime = 0.0f;
|
JumpAllowedTicker.Reset();
|
||||||
Velocity += GetWallNormal() * 200.0f;
|
Velocity += GetWallNormal() * 200.0f;
|
||||||
Velocity = Velocity with { Y = Mathf.Min(0, Velocity.Y) };
|
Velocity = Velocity with { Y = Mathf.Min(0, Velocity.Y) };
|
||||||
}
|
}
|
||||||
|
|
||||||
private void JumpCut()
|
private void JumpCut()
|
||||||
{
|
{
|
||||||
IsJumping = false;
|
JumpTicker.Reset();
|
||||||
JumpAscentRemainingTime = 0.0f;
|
|
||||||
// Jump cut only once, if we're still moving up
|
// Jump cut only once, if we're still moving up
|
||||||
if (Velocity.Y < 0)
|
if (Velocity.Y < 0)
|
||||||
Velocity = Velocity with { Y = Velocity.Y * 0.1f };
|
Velocity = Velocity with { Y = Velocity.Y * 0.1f };
|
||||||
|
|
@ -375,16 +383,16 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
||||||
private void Attack()
|
private void Attack()
|
||||||
{
|
{
|
||||||
AdjustFacing();
|
AdjustFacing();
|
||||||
Velocity += (_isFacingRight ? Vector2.Right : Vector2.Left) * SlashSpeedBoost;
|
Velocity += (IsFacingRight ? Vector2.Right : Vector2.Left) * SlashSpeedBoost;
|
||||||
_playback.Travel("front_kick");
|
_playback.Travel("front_kick");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WaterExitJump()
|
private void WaterExitJump()
|
||||||
{
|
{
|
||||||
AdjustFacing();
|
AdjustFacing();
|
||||||
JumpAllowedTime = 0.0f;
|
WaterJumpAllowedTicker.Reset();
|
||||||
JumpAscentRemainingTime = JumpAscentMaxTime * 1.5f;
|
JumpTicker.Duration = JumpAscentMaxDuration * 1.5f;
|
||||||
IsJumping = true;
|
JumpTicker.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Dash()
|
private void Dash()
|
||||||
|
|
@ -401,35 +409,15 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
||||||
private static Vector2 To8DirectionVector(Vector2 input)
|
private static Vector2 To8DirectionVector(Vector2 input)
|
||||||
{
|
{
|
||||||
if (input.IsZeroApprox()) 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;
|
|
||||||
var sector = Mathf.FloorToInt((angle + sixteenthCircle) / eighthCircle) % 8;
|
|
||||||
if (sector < 0) sector += 8;
|
|
||||||
return sector switch
|
|
||||||
{
|
|
||||||
0 => Vector2.Right,
|
|
||||||
1 => Vector2.Right + Vector2.Down,
|
|
||||||
2 => Vector2.Down,
|
|
||||||
3 => Vector2.Down + Vector2.Left,
|
|
||||||
4 => Vector2.Left,
|
|
||||||
5 => Vector2.Left + Vector2.Up,
|
|
||||||
6 => Vector2.Up,
|
|
||||||
7 => Vector2.Up + Vector2.Right,
|
|
||||||
_ => throw new UnreachableException(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool IsInputVecDown(Vector2 vec)
|
const float tan225 = 0.4142135623730950488f; // tan(22.5°) = √2 - 1
|
||||||
{
|
float ax = Mathf.Sign(input.X), ay = Mathf.Abs(input.Y);
|
||||||
return Mathf.Abs(vec.AngleTo(Vector2.Down)) < DownInputAngle && vec.Length() > 0.01f;
|
float sx = Mathf.Sign(input.X), sy = Mathf.Sign(input.Y);
|
||||||
}
|
|
||||||
|
|
||||||
private bool IsInputVecSide(Vector2 vec)
|
return
|
||||||
{
|
ay < ax * tan225 ? new(sx, 0) :
|
||||||
return vec.Length() > 0.01f
|
ax < ay * tan225 ? new(0, sy) :
|
||||||
&& (Mathf.Abs(vec.AngleTo(Vector2.Left)) < SideInputAngle
|
new(sx, sy);
|
||||||
|| Mathf.Abs(vec.AngleTo(Vector2.Right)) < SideInputAngle);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnEnemyHit(Hitbox hitbox, Node2D enemy)
|
private void OnEnemyHit(Hitbox hitbox, Node2D enemy)
|
||||||
|
|
@ -448,65 +436,37 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|
||||||
Health -= attack.Damage;
|
Health -= attack.Damage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class BufferedInput
|
|
||||||
{
|
|
||||||
public Vector2 Direction { get; init; }
|
|
||||||
public required InputEvent Event { get; init; }
|
|
||||||
public ulong Time { get; init; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnHitboxAdded(Hitbox hitbox) => hitbox.BodyHit += OnEnemyHit;
|
public void OnHitboxAdded(Hitbox hitbox) => hitbox.BodyHit += OnEnemyHit;
|
||||||
public void OnHitboxRemoved(Hitbox hitbox) => hitbox.BodyHit -= OnEnemyHit;
|
public void OnHitboxRemoved(Hitbox hitbox) => hitbox.BodyHit -= OnEnemyHit;
|
||||||
IHitboxOwner IProvide<IHitboxOwner>.Value() => this;
|
IHitboxOwner IProvide<IHitboxOwner>.Value() => this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class GameActionExtensions
|
||||||
|
{
|
||||||
|
private static readonly List<(StringName actionName, Player.Action action)>
|
||||||
|
Actions =
|
||||||
|
[
|
||||||
|
(ActionName.Attack, Player.Action.Attack),
|
||||||
|
(ActionName.Dash, Player.Action.Dash),
|
||||||
|
(ActionName.Jump, Player.Action.Jump),
|
||||||
|
];
|
||||||
|
|
||||||
[Meta]
|
extension(Player.Action)
|
||||||
public sealed partial class Logic : AutoBlock
|
|
||||||
{
|
{
|
||||||
public sealed record Data
|
public static bool TryFromEvent(
|
||||||
|
InputEvent @event,
|
||||||
|
out Player.Action result
|
||||||
|
)
|
||||||
{
|
{
|
||||||
|
foreach (var (actionName, action) in Actions)
|
||||||
|
if (@event.IsAction(actionName))
|
||||||
|
{
|
||||||
|
result = action;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Logic()
|
result = default;
|
||||||
{
|
return false;
|
||||||
Set(new Data());
|
|
||||||
Preallocate<Root>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Input
|
|
||||||
{
|
|
||||||
public readonly record struct OnPhysicsProcess(double Delta);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Meta, StateDiagram]
|
|
||||||
public abstract partial record Root : LogicBlockState
|
|
||||||
{
|
|
||||||
[Meta]
|
|
||||||
public sealed partial record Grounded : Root, IGet<Input.OnPhysicsProcess>
|
|
||||||
{
|
|
||||||
public Type On(in Input.OnPhysicsProcess input)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Meta]
|
|
||||||
public sealed partial record Airborne : Root, IGet<Input.OnPhysicsProcess>
|
|
||||||
{
|
|
||||||
public Type On(in Input.OnPhysicsProcess input)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Meta]
|
|
||||||
public sealed partial record Diving : Root, IGet<Input.OnPhysicsProcess>
|
|
||||||
{
|
|
||||||
public Type On(in Input.OnPhysicsProcess input)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue