added C#
This commit is contained in:
parent
ab79ace226
commit
00b76b6b25
63 changed files with 1172 additions and 1548 deletions
300
src/player/Player.cs
Normal file
300
src/player/Player.cs
Normal file
|
|
@ -0,0 +1,300 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Dancer;
|
||||
|
||||
using Godot;
|
||||
using Chickensoft.AutoInject;
|
||||
using Chickensoft.Introspection;
|
||||
|
||||
[Meta(typeof(IAutoNode))]
|
||||
public sealed partial class Player : CharacterBody2D
|
||||
{
|
||||
public override void _Notification(int what) => this.Notify(what);
|
||||
|
||||
[Export(PropertyHint.None, "suffix:s")]
|
||||
public float MaxInputBufferTime { get; set; } = 0.15f;
|
||||
|
||||
[Export(PropertyHint.None, "suffix:s⁻¹")]
|
||||
public float GroundedSpeedDecay { get; set; } = 0.1f;
|
||||
|
||||
[Export(PropertyHint.None, "suffix:s⁻¹")]
|
||||
public float GroundedSpeedStopDecay { get; set; } = 0.005f;
|
||||
|
||||
[Export(PropertyHint.None, "suffix:px/s")]
|
||||
public float WalkMaxSpeed { get; set; } = 130.0f;
|
||||
|
||||
[Export(PropertyHint.None, "suffix:px/s²")]
|
||||
public float WalkAccel { get; set; } = 600.0f;
|
||||
|
||||
[Export(PropertyHint.None, "suffix:px/s²")]
|
||||
public float AirAccel { get; set; } = 200.0f;
|
||||
|
||||
[Export(PropertyHint.None, "suffix:s")]
|
||||
public float JumpAscentMaxTime { get; set; } = 0.2f;
|
||||
|
||||
[Export(PropertyHint.None, "suffix:px/s")]
|
||||
public float JumpAscentSpeed { get; set; } = 200.0f;
|
||||
|
||||
[Export(PropertyHint.None, "suffix:s")]
|
||||
public float LateJumpTimeLimit { get; set; } = 0.15f;
|
||||
|
||||
[Export(PropertyHint.None, "suffix:s")]
|
||||
public float SlashSpeedBoost { get; set; } = 90.0f;
|
||||
|
||||
/// <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;
|
||||
|
||||
/// <summary>The angle difference from perfect down that still counts.</summary>
|
||||
[Export(PropertyHint.Range, "0,360,0.1,radians_as_degrees")]
|
||||
public float DownInputAngle { get; set; } = Mathf.Pi / 4;
|
||||
|
||||
private ulong MaxInputBufferTimeMicroseconds => (ulong)(MaxInputBufferTime * 1_000_000);
|
||||
|
||||
private float _jumpAscentRemainingTime;
|
||||
private bool _isJumping;
|
||||
private float _jumpAllowedTime;
|
||||
private Vector2 _currentDirection = Vector2.Zero;
|
||||
private bool _isFacingRight = true;
|
||||
|
||||
private AnimationNodeStateMachinePlayback _playback = null!;
|
||||
private List<BufferedInput> _inputBuffer = [];
|
||||
|
||||
[Node] private AnimationTree AnimationTree { get; set; } = null!;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_playback = AnimationTree.Get("parameters/playback").As<AnimationNodeStateMachinePlayback>();
|
||||
}
|
||||
|
||||
public override void _UnhandledInput(InputEvent @event)
|
||||
{
|
||||
if (@event.IsEcho())
|
||||
return;
|
||||
|
||||
if (
|
||||
@event.IsAction("move_left")
|
||||
|| @event.IsAction("move_right")
|
||||
|| @event.IsAction("move_up")
|
||||
|| @event.IsAction("move_down"))
|
||||
{
|
||||
_currentDirection = Input.GetVector(
|
||||
"move_left",
|
||||
"move_right",
|
||||
"move_up",
|
||||
"move_down");
|
||||
}
|
||||
|
||||
if (@event.IsAction("jump") || @event.IsAction("attack"))
|
||||
{
|
||||
_inputBuffer.Add(new()
|
||||
{
|
||||
Direction = _currentDirection,
|
||||
Event = @event,
|
||||
Time = Time.GetTicksUsec()
|
||||
});
|
||||
HandleBufferedInputs();
|
||||
}
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
var deltaF = (float)delta;
|
||||
|
||||
if (_currentDirection.X < 0)
|
||||
_isFacingRight = false;
|
||||
else if (_currentDirection.X > 0)
|
||||
_isFacingRight = true;
|
||||
|
||||
// Walking on the floor
|
||||
if (IsOnFloor())
|
||||
{
|
||||
if (Mathf.Sign(Velocity.X) != Mathf.Sign(_currentDirection.X))
|
||||
Velocity = Velocity with { X = Velocity.X * Mathf.Pow(GroundedSpeedStopDecay, deltaF) };
|
||||
else
|
||||
Velocity = Velocity with { X = Velocity.X * Mathf.Pow(GroundedSpeedDecay, deltaF) };
|
||||
|
||||
var targetVelX = Mathf.Sign(_currentDirection.X) * WalkMaxSpeed;
|
||||
var isWalking = targetVelX != 0;
|
||||
|
||||
if (!isWalking || Mathf.Abs(Velocity.X) < WalkMaxSpeed)
|
||||
{
|
||||
Velocity = Velocity with
|
||||
{
|
||||
X = Mathf.MoveToward(Velocity.X, targetVelX, WalkAccel * deltaF)
|
||||
};
|
||||
}
|
||||
}
|
||||
// Air control
|
||||
else
|
||||
{
|
||||
Velocity = Velocity with { X = Velocity.X + _currentDirection.X * AirAccel * deltaF };
|
||||
}
|
||||
|
||||
// Move up while holding jump
|
||||
if (_jumpAscentRemainingTime > 0)
|
||||
{
|
||||
Velocity = Velocity with { Y = -JumpAscentSpeed };
|
||||
_jumpAscentRemainingTime -= deltaF;
|
||||
if (_jumpAscentRemainingTime < 0)
|
||||
Velocity += GetGravity() * -_jumpAscentRemainingTime;
|
||||
}
|
||||
else if (!IsOnFloor())
|
||||
{
|
||||
Velocity += GetGravity() * deltaF;
|
||||
}
|
||||
|
||||
// Drag while in air
|
||||
if (!IsOnFloor() && _jumpAscentRemainingTime <= 0)
|
||||
{
|
||||
var dragForce = -0.01f * Velocity * Velocity.Abs();
|
||||
Velocity += dragForce * deltaF;
|
||||
}
|
||||
|
||||
MoveAndSlide();
|
||||
|
||||
_jumpAllowedTime = Mathf.Max(0, _jumpAllowedTime - deltaF);
|
||||
if (IsOnFloor())
|
||||
_jumpAllowedTime = LateJumpTimeLimit;
|
||||
|
||||
if (IsOnCeiling())
|
||||
{
|
||||
_isJumping = false;
|
||||
_jumpAscentRemainingTime = 0.0f;
|
||||
}
|
||||
|
||||
if (IsHoldingWall())
|
||||
{
|
||||
Velocity = Velocity with { Y = Mathf.Min(5.0f, Velocity.Y) };
|
||||
_isFacingRight = Mathf.Sign(GetWallNormal().X) > 0;
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsHoldingWall()
|
||||
{
|
||||
return
|
||||
!IsOnFloor()
|
||||
&& IsOnWall()
|
||||
&& Mathf.Sign(GetWallNormal().X) != Mathf.Sign(_currentDirection.X)
|
||||
&& _currentDirection.X != 0;
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
HandleBufferedInputs();
|
||||
GetNode<Node2D>("%Body").Scale = new(_isFacingRight ? 1 : -1, 1);
|
||||
}
|
||||
|
||||
private void HandleBufferedInputs()
|
||||
{
|
||||
var inputFound = false;
|
||||
var currentTime = Time.GetTicksUsec();
|
||||
|
||||
// Clear expired input buffer items
|
||||
while (
|
||||
_inputBuffer.Count > 0
|
||||
&& _inputBuffer[0].Time + MaxInputBufferTimeMicroseconds < currentTime)
|
||||
{
|
||||
_inputBuffer.RemoveAt(0);
|
||||
}
|
||||
|
||||
// Check new commands
|
||||
for (var i = 0; i < _inputBuffer.Count; i++)
|
||||
{
|
||||
var input = _inputBuffer[i];
|
||||
|
||||
if (input.Event.IsAction("jump"))
|
||||
{
|
||||
if (
|
||||
input.Event.IsPressed()
|
||||
&& _jumpAllowedTime > 0
|
||||
&& (!IsOnFloor() || !IsInputVecDown(input.Direction)))
|
||||
{
|
||||
inputFound = true;
|
||||
GroundJump();
|
||||
}
|
||||
else if (input.Event.IsPressed() && IsOnWall())
|
||||
{
|
||||
inputFound = true;
|
||||
WallJump();
|
||||
}
|
||||
else if (input.Event.IsReleased() && _isJumping)
|
||||
{
|
||||
JumpCut();
|
||||
}
|
||||
}
|
||||
else if (input.Event.IsActionPressed("attack"))
|
||||
{
|
||||
inputFound = true;
|
||||
Slash();
|
||||
}
|
||||
|
||||
if (inputFound)
|
||||
{
|
||||
_inputBuffer = _inputBuffer.GetRange(i + 1, _inputBuffer.Count - i - 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void GroundJump()
|
||||
{
|
||||
_jumpAllowedTime = 0.0f;
|
||||
_jumpAscentRemainingTime = JumpAscentMaxTime;
|
||||
_isJumping = true;
|
||||
}
|
||||
|
||||
private void WallJump()
|
||||
{
|
||||
_jumpAllowedTime = 0.0f;
|
||||
Velocity += GetWallNormal() * 200.0f;
|
||||
Velocity = Velocity with { Y = Mathf.Min(0, Velocity.Y) };
|
||||
}
|
||||
|
||||
private void JumpCut()
|
||||
{
|
||||
_isJumping = false;
|
||||
_jumpAscentRemainingTime = 0.0f;
|
||||
// Jump cut only once, if we're still moving up
|
||||
if (Velocity.Y < 0)
|
||||
Velocity = Velocity with { Y = Velocity.Y * 0.1f };
|
||||
}
|
||||
|
||||
private void Slash()
|
||||
{
|
||||
Velocity = Velocity with
|
||||
{
|
||||
X = Velocity.X + (_isFacingRight ? 1 : -1) * SlashSpeedBoost
|
||||
};
|
||||
_playback.Travel("attack");
|
||||
|
||||
var instance = GD.Load<PackedScene>("res://src/player/slash_projectile.tscn")
|
||||
.Instantiate<SlashProjectile>();
|
||||
AddChild(instance);
|
||||
instance.GlobalTransform = GetNode<Node2D>("%HandAttachment").GlobalTransform;
|
||||
instance.HitboxEntered += OnEnemyHit;
|
||||
}
|
||||
|
||||
private bool IsInputVecDown(Vector2 vec)
|
||||
{
|
||||
return Mathf.Abs(vec.AngleTo(Vector2.Down)) < DownInputAngle && vec.Length() > 0.1f;
|
||||
}
|
||||
|
||||
private void OnEnemyHit(Node2D enemy)
|
||||
{
|
||||
if (enemy is not IAttackable attackable) return;
|
||||
attackable.GetAttacked(new()
|
||||
{
|
||||
Attacker = this,
|
||||
Damage = 1.0f,
|
||||
Impulse = Vector2.Down * 5000.0f + (enemy.Position - Position).Normalized() * 5000.0f
|
||||
});
|
||||
}
|
||||
|
||||
public class BufferedInput
|
||||
{
|
||||
public Vector2 Direction { get; init; }
|
||||
public required InputEvent Event { get; init; }
|
||||
public ulong Time { get; init; }
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue