AceField/Scripts/Player.cs

52 lines
1.2 KiB
C#

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;
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);
}
Position += Velocity * (float)delta;
MoveAndSlide();
if (PlayerCamera != null)
{
PlayerCamera.Position = GlobalPosition;
}
}
}