AceField/Scripts/Player.cs
2024-08-05 16:30:50 +08:00

62 lines
1.4 KiB
C#

using System;
using Godot;
namespace CodingLand.Scripts;
public partial class Player : CharacterBody2D
{
[Export] public float MaxSpeed = 400f;
[Export] public float Acceleration = 500f;
[Export] public float Deceleration = 500f;
[Export] public float RotationSpeed = 5f;
[Export] public float CameraSmoothness = 0.05f;
[Export] public Camera2D PlayerCamera;
[Export] public Timer PlayerDashCountdown;
[Export] public float PlayerDashAcceleration = 2f;
public override void _PhysicsProcess(double delta)
{
var input = Vector2.Zero;
if (Input.IsActionPressed("move_right"))
input.X += 1;
if (Input.IsActionPressed("move_left"))
input.X -= 1;
if (Input.IsActionPressed("move_down"))
input.Y += 1;
if (Input.IsActionPressed("move_up"))
input.Y -= 1;
input = input.Normalized();
if (input != Vector2.Zero)
{
Velocity = Velocity.MoveToward(input * MaxSpeed, Acceleration * (float)delta);
var finalRotation = input.Angle() + Mathf.Pi / 2;
Rotation = Mathf.LerpAngle(Rotation, finalRotation, RotationSpeed * (float)delta);
}
else
{
Velocity = Velocity.MoveToward(Vector2.Zero, Deceleration * (float)delta);
}
if (Input.IsActionJustPressed("move_dash") && PlayerDashCountdown.IsStopped())
{
Velocity *= PlayerDashAcceleration;
PlayerDashCountdown.Start();
}
Position += Velocity * (float)delta;
MoveAndSlide();
if (PlayerCamera != null)
{
PlayerCamera.Position = GlobalPosition;
}
}
}