✨ Player died & Round & Respawn
This commit is contained in:
@ -20,7 +20,7 @@ public partial class Bullet : Area2D
|
||||
if (body is Player player)
|
||||
{
|
||||
if (player.PlayerId == PlayerId) return;
|
||||
player.TakeDamage(Damage);
|
||||
player.TakeDamage(Damage, PlayerId);
|
||||
}
|
||||
|
||||
if (body is Brick brick)
|
||||
|
32
Scripts/Logic/Scoreboard.cs
Normal file
32
Scripts/Logic/Scoreboard.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
|
||||
namespace AceField.Scripts.Logic;
|
||||
|
||||
public record PlayerData(string Name, int Score = 0)
|
||||
{
|
||||
public string Name { get; set; } = Name;
|
||||
public int Score { get; set; } = Score;
|
||||
};
|
||||
|
||||
public partial class Scoreboard : Node
|
||||
{
|
||||
public Dictionary<int, PlayerData> Players = new();
|
||||
|
||||
public void AddPlayer(int networkId, string playerName = null)
|
||||
{
|
||||
Players[networkId] = new PlayerData(playerName ?? $"Player#{networkId}");
|
||||
}
|
||||
|
||||
public void RemovePlayer(int networkId)
|
||||
{
|
||||
Players.Remove(networkId);
|
||||
}
|
||||
|
||||
public void AddScore(int networkId, int amount = 1)
|
||||
{
|
||||
if (Players[networkId]?.Score == null) return;
|
||||
Players[networkId]!.Score += amount;
|
||||
}
|
||||
}
|
@ -4,86 +4,135 @@ namespace AceField.Scripts.Logic;
|
||||
|
||||
public partial class World : Node2D
|
||||
{
|
||||
[Export] public PackedScene PlayerScene;
|
||||
[Export] public Scoreboard Scoreboard;
|
||||
[Export] public PackedScene PlayerScene;
|
||||
|
||||
public void StartGame(string currentPlayerName = null)
|
||||
{
|
||||
if (!Multiplayer.IsServer())
|
||||
return;
|
||||
[Export] public int RoundCount = 1;
|
||||
[Export] public double RoundDuration = 60;
|
||||
[Export] public double RoundProgress;
|
||||
[Export] public double RoundTimeLeft;
|
||||
|
||||
// Handling player connect / disconnect after this client connected
|
||||
Multiplayer.PeerDisconnected += RemovePlayer_Adaptor;
|
||||
Multiplayer.PeerConnected += AddPlayer_Adaptor;
|
||||
private Timer _roundTimer;
|
||||
|
||||
// Handling player connected before this client
|
||||
foreach (var id in Multiplayer.GetPeers())
|
||||
AddPlayer(id);
|
||||
public void StartGame(string currentPlayerName = null)
|
||||
{
|
||||
if (!Multiplayer.IsServer())
|
||||
return;
|
||||
|
||||
// Add this client as a player if client isn't a dedicated server
|
||||
if (!OS.HasFeature("dedicated_server"))
|
||||
AddPlayer(1, currentPlayerName);
|
||||
}
|
||||
_roundTimer = new Timer();
|
||||
_roundTimer.WaitTime = RoundDuration;
|
||||
_roundTimer.Autostart = true;
|
||||
_roundTimer.Timeout += NewRound;
|
||||
_roundTimer.OneShot = true;
|
||||
AddChild(_roundTimer);
|
||||
_roundTimer.Start();
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
if (!Multiplayer.IsServer())
|
||||
return;
|
||||
// Handling player connect / disconnect after this client connected
|
||||
Multiplayer.PeerDisconnected += RemovePlayer_Adaptor;
|
||||
Multiplayer.PeerConnected += AddPlayer_Adaptor;
|
||||
|
||||
Multiplayer.PeerDisconnected -= RemovePlayer_Adaptor;
|
||||
Multiplayer.PeerConnected -= AddPlayer_Adaptor;
|
||||
}
|
||||
// Handling player connected before this client
|
||||
foreach (var id in Multiplayer.GetPeers())
|
||||
Scoreboard.AddPlayer(id);
|
||||
|
||||
private static string BuildPlayerName(int id)
|
||||
=> $"Player@{id}";
|
||||
// Add this client as a player if client isn't a dedicated server
|
||||
if (!OS.HasFeature("dedicated_server"))
|
||||
Scoreboard.AddPlayer(1, currentPlayerName);
|
||||
|
||||
private void AddPlayer_Adaptor(long id)
|
||||
=> AddPlayer((int)id);
|
||||
// Add players into the game
|
||||
PutPlayers(currentPlayerName);
|
||||
}
|
||||
|
||||
private void RemovePlayer_Adaptor(long id)
|
||||
=> RemovePlayer((int)id);
|
||||
public override void _ExitTree()
|
||||
{
|
||||
if (!Multiplayer.IsServer())
|
||||
return;
|
||||
|
||||
private void AddPlayer(int id, string name = null)
|
||||
{
|
||||
var player = PlayerScene.Instantiate<Player>();
|
||||
player.PlayerId = id;
|
||||
var position = Vector2.FromAngle(GD.Randf() * 2 * Mathf.Pi);
|
||||
player.Position = new Vector2(position.X * 5f * GD.Randf(), position.Y * 5f * GD.Randf());
|
||||
player.Name = BuildPlayerName(id);
|
||||
player.PlayerName = name;
|
||||
Multiplayer.PeerDisconnected -= RemovePlayer_Adaptor;
|
||||
Multiplayer.PeerConnected -= AddPlayer_Adaptor;
|
||||
}
|
||||
|
||||
AddChild(player, true);
|
||||
}
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (Multiplayer.IsServer())
|
||||
{
|
||||
RoundProgress = _roundTimer.TimeLeft / _roundTimer.WaitTime;
|
||||
RoundTimeLeft = _roundTimer.TimeLeft;
|
||||
}
|
||||
}
|
||||
|
||||
private void RemovePlayer(int id)
|
||||
{
|
||||
var name = BuildPlayerName(id);
|
||||
if (!HasNode(name))
|
||||
return;
|
||||
private static string BuildPlayerName(int id)
|
||||
=> $"Player@{id}";
|
||||
|
||||
GetNode(name).QueueFree();
|
||||
}
|
||||
private void AddPlayer_Adaptor(long id)
|
||||
=> Scoreboard.AddPlayer((int)id);
|
||||
|
||||
public Player GetCurrentPlayer()
|
||||
{
|
||||
foreach(var child in GetChildren())
|
||||
{
|
||||
if (child is Player { IsCurrentPlayer: true } player)
|
||||
{
|
||||
return player;
|
||||
}
|
||||
}
|
||||
private void RemovePlayer_Adaptor(long id)
|
||||
=> Scoreboard.RemovePlayer((int)id);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public T GetTileByPosition<T>(Vector2 position) where T : Node2D
|
||||
{
|
||||
foreach (var item in GetChildren())
|
||||
{
|
||||
if (item is T tile && tile.Position == position)
|
||||
return tile;
|
||||
}
|
||||
private void SpawnPlayer(int id, string name = null)
|
||||
{
|
||||
var player = PlayerScene.Instantiate<Player>();
|
||||
player.PlayerId = id;
|
||||
var position = Vector2.FromAngle(GD.Randf() * 2 * Mathf.Pi);
|
||||
player.Position = new Vector2(position.X * 5f * GD.Randf(), position.Y * 5f * GD.Randf());
|
||||
player.Name = BuildPlayerName(id);
|
||||
player.PlayerName = name;
|
||||
player.PlayerDied += (killerId) =>
|
||||
{
|
||||
if (killerId == 0) return;
|
||||
Scoreboard.AddScore(killerId);
|
||||
};
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
AddChild(player, true);
|
||||
}
|
||||
|
||||
private void PutPlayers(string currentPlayerName = null)
|
||||
{
|
||||
// Spawn clients
|
||||
foreach (var id in Multiplayer.GetPeers())
|
||||
SpawnPlayer(id, Scoreboard.Players[id]?.Name);
|
||||
|
||||
// Spawn host
|
||||
if (!OS.HasFeature("dedicated_server"))
|
||||
SpawnPlayer(1, currentPlayerName ?? Scoreboard.Players[1]?.Name);
|
||||
}
|
||||
|
||||
private void NewRound()
|
||||
{
|
||||
RoundCount++;
|
||||
foreach (var child in GetChildren())
|
||||
{
|
||||
if (child is not Timer)
|
||||
child.QueueFree();
|
||||
}
|
||||
|
||||
PutPlayers();
|
||||
|
||||
_roundTimer.Start();
|
||||
}
|
||||
|
||||
public Player GetCurrentPlayer()
|
||||
{
|
||||
foreach (var child in GetChildren())
|
||||
{
|
||||
if (child is Player { IsCurrentPlayer: true } player)
|
||||
{
|
||||
return player;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public T GetTileByPosition<T>(Vector2 position) where T : Node2D
|
||||
{
|
||||
foreach (var item in GetChildren())
|
||||
{
|
||||
if (item is T tile && tile.Position == position)
|
||||
return tile;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -45,6 +45,9 @@ public partial class Player : CharacterBody2D
|
||||
}
|
||||
}
|
||||
|
||||
[Signal]
|
||||
public delegate void PlayerDiedEventHandler(int killerId);
|
||||
|
||||
public bool IsCurrentPlayer => _currentPlayerId == Multiplayer.GetUniqueId();
|
||||
|
||||
public bool IsReloading
|
||||
@ -71,6 +74,9 @@ public partial class Player : CharacterBody2D
|
||||
AmmoAmount = MaxAmmoAmount;
|
||||
PlayerInput.IsReloading = false;
|
||||
};
|
||||
|
||||
GetParent<World>().GetParent().GetNode<Control>("OverlayLayer/Hud").Show();
|
||||
GetParent<World>().GetParent().GetNode<Control>("OverlayLayer/PlayerDiedScreen").Hide();
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
@ -157,9 +163,9 @@ public partial class Player : CharacterBody2D
|
||||
MoveAndSlide();
|
||||
}
|
||||
|
||||
public void TakeDamage(double damage)
|
||||
public void TakeDamage(double damage, int attackerId = 0)
|
||||
{
|
||||
Rpc(nameof(GotDamage), damage);
|
||||
Rpc(nameof(GotDamage), damage, attackerId);
|
||||
}
|
||||
|
||||
private void Shoot(string name)
|
||||
@ -189,7 +195,7 @@ public partial class Player : CharacterBody2D
|
||||
}
|
||||
|
||||
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
|
||||
private void GotDamage(double damage)
|
||||
private void GotDamage(double damage, int attackerId)
|
||||
{
|
||||
var protection = GetNode<Timer>("ProtectionCountdown");
|
||||
|
||||
@ -206,5 +212,18 @@ public partial class Player : CharacterBody2D
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,14 @@ public partial class HUD : Control
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
var roundBar = GetNode<ProgressBar>("BottomBox/ProgressBar");
|
||||
roundBar.Value = World.RoundProgress * 100;
|
||||
|
||||
var roundLabel = GetNode<Label>("BottomBox/HBox/RoundLabel");
|
||||
var roundSecLabel = GetNode<Label>("BottomBox/HBox/RoundSecondLabel");
|
||||
roundLabel.Text = $"Round {World.RoundCount}";
|
||||
roundSecLabel.Text = World.RoundTimeLeft.ToString("F2");
|
||||
|
||||
var player = World.GetCurrentPlayer();
|
||||
if (player == null) return;
|
||||
|
||||
@ -37,5 +45,8 @@ public partial class HUD : Control
|
||||
ammoLabel.Text = player.IsReloading
|
||||
? $"Reloading... {player.TimeRemainingOfReload:F2}"
|
||||
: $"Ammo {player.AmmoAmount}/{player.MaxAmmoAmount}";
|
||||
|
||||
var positionLabel = GetNode<Label>("BottomBox/HBox/PositionLabel");
|
||||
positionLabel.Text = $"({player.Position.X:F2}, {player.Position.Y:F2})";
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user