AceField/Scripts/Player.cs

230 lines
5.5 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-08 18:20:56 +00:00
private int _currentPlayerId = 1;
[Export] public float MaxSpeed = 400f;
[Export] public float Acceleration = 500f;
[Export] public float Deceleration = 500f;
[Export] public float RotationSpeed = 5f;
[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;
[Export] public int AmmoAmount = 30;
[Export] public int MaxAmmoAmount = 30;
[Export] public Camera2D PlayerCamera;
[Export] public PlayerInput PlayerInput;
[Export] public float PlayerDashAcceleration = 2f;
[Export] public PackedScene BulletScene;
[Export] public PackedScene TileScene;
[Export] public float TileSize;
[Export] public string PlayerName;
[Export]
public int PlayerId
{
get => _currentPlayerId;
set
{
_currentPlayerId = value;
PlayerInput.SetMultiplayerAuthority(value);
}
}
[Signal]
public delegate void PlayerDiedEventHandler(int killerId);
2024-08-08 18:20:56 +00:00
public bool IsCurrentPlayer => _currentPlayerId == Multiplayer.GetUniqueId();
public bool IsReloading
=> !GetNode<Timer>("ReloadTimer").IsStopped();
public double TimeRemainingOfReload
=> GetNode<Timer>("ReloadTimer").TimeLeft;
public override void _Ready()
{
Health = MaxHealth;
ActionPoints = MaxActionPoints;
if (PlayerId == Multiplayer.GetUniqueId())
PlayerCamera.Enabled = true;
PlayerName ??= $"Player#{PlayerId}";
GetNode<Label>("Overlay/NameTag").Text = PlayerName;
GetNode<ProgressBar>("Overlay/HealthBar").Value = Health / MaxHealth * 100;
GetNode<Timer>("ReloadTimer").Timeout += () =>
{
AmmoAmount = MaxAmmoAmount;
PlayerInput.IsReloading = false;
};
GetParent<World>().GetParent().GetNode<Control>("OverlayLayer/Hud").Show();
GetParent<World>().GetParent().GetNode<Control>("OverlayLayer/PlayerDiedScreen").Hide();
2024-08-08 18:20:56 +00:00
}
public override void _Process(double delta)
{
if (PlayerInput.IsShooting)
{
PlayerInput.IsShooting = false;
var timer = GetNode<Timer>("WeaponCountdown");
if (timer.IsStopped())
{
var name = GD.Randi();
Shoot(name.ToString());
timer.Start();
}
}
if (PlayerInput.IsReloading)
{
if (AmmoAmount == MaxAmmoAmount)
{
PlayerInput.IsReloading = false;
}
else
{
var timer = GetNode<Timer>("ReloadTimer");
if (timer.IsStopped())
{
AmmoAmount = 0;
timer.Start();
}
}
}
if (PlayerInput.IsBuilding)
{
PlayerInput.IsBuilding = false;
var target = GetGlobalMousePosition();
var distance = Position.DistanceTo(target);
if (distance <= Reach * TileSize)
{
var name = GD.Randi();
if (GetParent<World>().GetTileByPosition<Node2D>(target) == null)
Rpc(nameof(AddTile), target, PlayerId, name.ToString());
}
}
}
public override void _PhysicsProcess(double delta)
{
var input = PlayerInput.MovementDirection;
if (input != Vector2.Zero)
{
input = input.Normalized();
Velocity = Velocity.MoveToward(input * MaxSpeed, Acceleration * (float)delta);
var centre = GetNode<Node2D>("RotationCentre");
var finalRotation = input.Angle() + Mathf.Pi / 2;
centre.Rotation = Mathf.LerpAngle(centre.Rotation, finalRotation, RotationSpeed * (float)delta);
}
else
{
Velocity = Velocity.MoveToward(Vector2.Zero, Deceleration * (float)delta);
}
var dashCountdown = GetNode<Timer>("DashCountdown");
if (PlayerInput.IsDashing && dashCountdown.IsStopped() && ActionPoints > 0)
{
PlayerInput.IsDashing = false;
Velocity *= PlayerDashAcceleration;
ActionPoints--;
dashCountdown.Start();
}
if (IsReloading)
{
Velocity *= 0.8f;
}
Position += Velocity * (float)delta;
MoveAndSlide();
}
public void TakeDamage(double damage, int attackerId = 0)
2024-08-08 18:20:56 +00:00
{
Rpc(nameof(GotDamage), damage, attackerId);
2024-08-08 18:20:56 +00:00
}
private void Shoot(string name)
{
if (AmmoAmount <= 0) return;
var marker = GetNode<Marker2D>("RotationCentre/Muzzle");
var projectile = BulletScene.Instantiate<Bullet>();
projectile.Name = $"Bullet@{name}";
projectile.Transform = marker.GlobalTransform;
projectile.PlayerId = PlayerId;
GetParent().AddChild(projectile);
AmmoAmount--;
}
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
public void AddTile(Vector2 pos, int playerId, string name)
{
var tiles = GetParent<World>();
var tileVec = new Vector2(50, 50);
var instance = TileScene.Instantiate<Brick>();
instance.Name = $"Brick@{name}";
instance.Position = pos.Snapped(tileVec);
instance.PlayerId = playerId;
tiles.AddChild(instance);
}
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
private void GotDamage(double damage, int attackerId)
2024-08-08 18:20:56 +00:00
{
var protection = GetNode<Timer>("ProtectionCountdown");
if (protection.IsStopped())
{
Health -= damage;
var tween = CreateTween();
var bar = GetNode<ProgressBar>("Overlay/HealthBar");
tween.TweenProperty(bar, "value", Health / MaxHealth * 100, 0.3);
protection.Start();
}
var shakableCamera = GetNode<CameraShake>("Camera2D");
shakableCamera.AddTrauma(0.5f);
if (!(Health <= 0)) return;
Rpc(nameof(Die), attackerId);
if (!IsCurrentPlayer) return;
GetParent<World>().GetParent().GetNode<Control>("OverlayLayer/Hud").Hide();
GetParent<World>().GetParent().GetNode<Control>("OverlayLayer/PlayerDiedScreen").Show();
}
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
private void Die(int killerId = 0)
{
EmitSignal(SignalName.PlayerDied, killerId);
QueueFree();
2024-08-08 18:20:56 +00:00
}
}