Reload & HUD

This commit is contained in:
2024-08-08 17:10:48 +08:00
parent 91662a1c9f
commit e5be2bdc7c
12 changed files with 189 additions and 21 deletions

View File

@ -17,10 +17,9 @@ public partial class Bullet : Area2D
BodyEntered += body =>
{
if (body is Player player && player.PlayerId != PlayerId)
{
player.TakeDamage(Damage);
}
if (body is not Player player || player.PlayerId == PlayerId) return;
player.TakeDamage(Damage);
QueueFree();
};
}

View File

@ -6,17 +6,20 @@ namespace AceField.Scripts;
public partial class Launcher : Node
{
[Export] public World World;
[Export] public CanvasLayer Overlay;
private void GameFreeze()
{
GetTree().Paused = true;
World.Hide();
Overlay.Hide();
}
private void GameUnfreeze()
{
GetTree().Paused = false;
World.Show();
Overlay.Show();
}
public override void _Ready()

View File

@ -6,6 +6,7 @@ public partial class PlayerInput : MultiplayerSynchronizer
{
[Export] public bool IsDashing;
[Export] public bool IsShooting;
[Export] public bool IsReloading;
[Export] public Vector2 MovementDirection;
@ -25,6 +26,10 @@ public partial class PlayerInput : MultiplayerSynchronizer
[Rpc(CallLocal = true)]
private void Shoot()
=> IsShooting = true;
[Rpc(CallLocal = true)]
private void Reload()
=> IsReloading = true;
public override void _Process(double delta)
{
@ -32,8 +37,10 @@ public partial class PlayerInput : MultiplayerSynchronizer
if (Input.IsActionJustPressed("move_dash"))
Rpc(nameof(Dash));
if (Input.IsActionJustPressed("shoot"))
if (Input.IsActionJustPressed("weapon_shoot"))
Rpc(nameof(Shoot));
if (Input.IsActionJustPressed("weapon_reload"))
Rpc(nameof(Reload));
}
public override void _Input(InputEvent evt)

View File

@ -33,10 +33,8 @@ public partial class World : Node2D
Multiplayer.PeerConnected -= AddPlayer_Adaptor;
}
private string BuildPlayerName(int id)
{
return $"Player#{id}";
}
private static string BuildPlayerName(int id)
=> $"Player#{id}";
private void AddPlayer_Adaptor(long id)
=> AddPlayer((int)id);

View File

@ -44,6 +44,12 @@ public partial class Player : CharacterBody2D
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;
@ -52,18 +58,31 @@ public partial class Player : CharacterBody2D
if (PlayerId == Multiplayer.GetUniqueId())
PlayerCamera.Enabled = true;
PlayerName ??= $"Player#{PlayerId}";
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;
};
}
public override void _Process(double delta)
{
if (PlayerInput.IsShooting)
{
Rpc(nameof(Shoot));
PlayerInput.IsShooting = false;
Shoot();
}
if (PlayerInput.IsReloading)
{
var timer = GetNode<Timer>("ReloadTimer");
if (timer.IsStopped())
timer.Start();
}
}
@ -86,10 +105,11 @@ public partial class Player : CharacterBody2D
}
var dashCountdown = GetNode<Timer>("DashCountdown");
if (PlayerInput.IsDashing && dashCountdown.IsStopped())
if (PlayerInput.IsDashing && dashCountdown.IsStopped() && ActionPoints > 0)
{
PlayerInput.IsDashing = false;
Velocity *= PlayerDashAcceleration;
ActionPoints--;
dashCountdown.Start();
}
@ -122,14 +142,16 @@ public partial class Player : CharacterBody2D
shakableCamera.AddTrauma(0.5f);
}
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
private void Shoot()
{
if (AmmoAmount <= 0) return;
var marker = GetNode<Marker2D>("RotationCentre/Muzzle");
var projectile = BulletScene.Instantiate<Bullet>();
projectile.Transform = marker.GlobalTransform;
projectile.PlayerId = PlayerId;
GetParent().AddChild(projectile);
AmmoAmount--;
}
}

40
Scripts/UI/HUD.cs Normal file
View File

@ -0,0 +1,40 @@
using AceField.Scripts.Logic;
using Godot;
namespace AceField.Scripts.UI;
public partial class HUD : Control
{
[Export] public World World;
private void ApplySize()
{
var screenSize = GetViewportRect().Size;
Position = new Vector2(0, 0);
Size = screenSize;
GetNode<HBoxContainer>("TopBox").Size = new Vector2(screenSize.X-16*2, 16);
}
public override void _Process(double delta)
{
var player = World.GetCurrentPlayer();
if (player == null) return;
var healthBar = GetNode<ProgressBar>("TopBox/HealthBox/Bar");
var healthLabel = GetNode<Label>("TopBox/HealthBox/Label");
healthBar.Value = player.Health / player.MaxHealth * 100;
healthLabel.Text = $"Health {player.Health}/{player.MaxHealth}";
var actionBar = GetNode<ProgressBar>("TopBox/ActionPointBox/Bar");
var actionLabel = GetNode<Label>("TopBox/ActionPointBox/Label");
actionBar.Value = player.ActionPoints / player.MaxActionPoints * 100;
actionLabel.Text = $"AP {player.ActionPoints}/{player.MaxActionPoints}";
var ammoBar = GetNode<ProgressBar>("TopBox/AmmoBox/Bar");
var ammoLabel = GetNode<Label>("TopBox/AmmoBox/Label");
ammoBar.Value = player.AmmoAmount / player.MaxAmmoAmount * 100;
if (player.IsReloading) ammoLabel.Text = $"Reloading... {player.TimeRemainingOfReload:F2}";
else ammoLabel.Text = $"Ammo {player.AmmoAmount}/{player.MaxAmmoAmount}";
}
}