AceField/Scripts/Player.cs

119 lines
2.6 KiB
C#
Raw Normal View History

using AceField.Scripts.Effects;
2024-08-07 15:01:57 +00:00
using AceField.Scripts.Logic;
2024-08-04 15:00:26 +00:00
using Godot;
2024-08-07 15:01:57 +00:00
namespace AceField.Scripts;
2024-08-04 15:00:26 +00:00
public partial class Player : CharacterBody2D
{
2024-08-05 11:08:28 +00:00
private int _currentPlayerId = 1;
2024-08-05 15:54:22 +00:00
2024-08-04 15:00:26 +00:00
[Export] public float MaxSpeed = 400f;
[Export] public float Acceleration = 500f;
[Export] public float Deceleration = 500f;
[Export] public float RotationSpeed = 5f;
2024-08-05 15:54:22 +00:00
[Export] public int Reach = 5;
[Export] public double Health = 100;
[Export] public double MaxHealth = 100;
[Export] public double ActionPoints = 20;
[Export] public double MaxActionPoints = 20;
2024-08-05 11:08:28 +00:00
[Export] public Camera2D PlayerCamera;
2024-08-05 11:08:28 +00:00
[Export] public PlayerInput PlayerInput;
2024-08-05 08:30:50 +00:00
[Export] public float PlayerDashAcceleration = 2f;
2024-08-04 15:00:26 +00:00
[Export] public PackedScene BulletScene;
2024-08-05 15:54:22 +00:00
[Export]
public int PlayerId
2024-08-05 11:08:28 +00:00
{
get => _currentPlayerId;
set
{
_currentPlayerId = value;
PlayerInput.SetMultiplayerAuthority(value);
}
}
public void SetPlayerId(int id)
2024-08-04 15:00:26 +00:00
{
2024-08-05 11:08:28 +00:00
PlayerId = id;
PlayerInput.SetMultiplayerAuthority(id);
}
2024-08-05 15:54:22 +00:00
2024-08-05 11:08:28 +00:00
public override void _Ready()
{
Health = MaxHealth;
ActionPoints = MaxActionPoints;
2024-08-05 11:08:28 +00:00
if (PlayerId == Multiplayer.GetUniqueId())
PlayerCamera.Enabled = true;
2024-08-05 15:54:22 +00:00
}
public override void _Process(double delta)
{
if (PlayerInput.IsShooting)
{
Rpc(nameof(Shoot));
PlayerInput.IsShooting = false;
}
2024-08-05 11:08:28 +00:00
}
2024-08-04 15:00:26 +00:00
2024-08-05 11:08:28 +00:00
public override void _PhysicsProcess(double delta)
{
var input = PlayerInput.MovementDirection;
2024-08-04 15:00:26 +00:00
if (input != Vector2.Zero)
{
2024-08-05 11:08:28 +00:00
input = input.Normalized();
2024-08-04 15:00:26 +00:00
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
2024-08-05 15:54:22 +00:00
var dashCountdown = GetNode<Timer>("DashCountdown");
if (PlayerInput.IsDashing && dashCountdown.IsStopped())
2024-08-05 08:30:50 +00:00
{
2024-08-05 11:08:28 +00:00
PlayerInput.IsDashing = false;
2024-08-05 08:30:50 +00:00
Velocity *= PlayerDashAcceleration;
2024-08-05 15:54:22 +00:00
dashCountdown.Start();
2024-08-05 08:30:50 +00:00
}
2024-08-05 11:08:28 +00:00
2024-08-04 15:00:26 +00:00
Position += Velocity * (float)delta;
MoveAndSlide();
}
public void TakeDamage(double damage)
{
Rpc(nameof(GotDamage), damage);
}
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
private void GotDamage(double damage)
{
Health -= damage;
var shakableCamera = GetNode<CameraShake>("Camera2D");
shakableCamera.AddTrauma(0.5f);
}
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
private void Shoot()
{
var marker = GetNode<Marker2D>("Muzzle");
var projectile = BulletScene.Instantiate<Bullet>();
projectile.Transform = marker.GlobalTransform;
projectile.PlayerId = PlayerId;
GetParent().AddChild(projectile);
}
2024-08-04 15:00:26 +00:00
}