AceField/Scripts/Player.cs

62 lines
1.4 KiB
C#
Raw Normal View History

2024-08-05 08:30:50 +00:00
using System;
2024-08-04 15:00:26 +00:00
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;
2024-08-05 08:30:50 +00:00
[Export] public Timer PlayerDashCountdown;
[Export] public float PlayerDashAcceleration = 2f;
2024-08-04 15:00:26 +00:00
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);
}
2024-08-05 08:30:50 +00:00
if (Input.IsActionJustPressed("move_dash") && PlayerDashCountdown.IsStopped())
{
Velocity *= PlayerDashAcceleration;
PlayerDashCountdown.Start();
}
2024-08-04 15:00:26 +00:00
Position += Velocity * (float)delta;
MoveAndSlide();
if (PlayerCamera != null)
{
PlayerCamera.Position = GlobalPosition;
}
}
}