diff --git a/Scenes/Player.tscn b/Scenes/Player.tscn index b6220dc..5f0140f 100644 --- a/Scenes/Player.tscn +++ b/Scenes/Player.tscn @@ -22,27 +22,24 @@ properties/1/replication_mode = 1 properties/2/path = NodePath("RotationCentre:rotation") properties/2/spawn = true properties/2/replication_mode = 1 -properties/3/path = NodePath(".:PlayerName") +properties/3/path = NodePath(".:Health") properties/3/spawn = true properties/3/replication_mode = 1 -properties/4/path = NodePath(".:Health") +properties/4/path = NodePath(".:MaxHealth") properties/4/spawn = true properties/4/replication_mode = 1 -properties/5/path = NodePath(".:MaxHealth") +properties/5/path = NodePath(".:ActionPoints") properties/5/spawn = true properties/5/replication_mode = 1 -properties/6/path = NodePath(".:ActionPoints") +properties/6/path = NodePath(".:MaxActionPoints") properties/6/spawn = true properties/6/replication_mode = 1 -properties/7/path = NodePath(".:MaxActionPoints") +properties/7/path = NodePath(".:AmmoAmount") properties/7/spawn = true properties/7/replication_mode = 1 -properties/8/path = NodePath(".:AmmoAmount") +properties/8/path = NodePath(".:MaxAmmoAmount") properties/8/spawn = true properties/8/replication_mode = 1 -properties/9/path = NodePath(".:MaxAmmoAmount") -properties/9/spawn = true -properties/9/replication_mode = 1 [sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_hojn2"] properties/0/path = NodePath("InputSynchronizer:IsDashing") diff --git a/Scenes/UI/HUD.tscn b/Scenes/UI/HUD.tscn index 5734eb2..2107f46 100644 --- a/Scenes/UI/HUD.tscn +++ b/Scenes/UI/HUD.tscn @@ -97,10 +97,10 @@ size_flags_horizontal = 3 text = "Round 1" horizontal_alignment = 1 -[node name="RoundSecondLabel" type="Label" parent="BottomBox/HBox"] +[node name="ScoreLabel" type="Label" parent="BottomBox/HBox"] layout_mode = 2 size_flags_horizontal = 3 -text = "60.00" +text = "Score 0" horizontal_alignment = 2 [node name="ProgressBar" type="ProgressBar" parent="BottomBox"] diff --git a/Scripts/Logic/Scoreboard.cs b/Scripts/Logic/Scoreboard.cs index 0ce6079..f296c72 100644 --- a/Scripts/Logic/Scoreboard.cs +++ b/Scripts/Logic/Scoreboard.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using Godot; @@ -16,17 +15,55 @@ public partial class Scoreboard : Node public void AddPlayer(int networkId, string playerName = null) { - Players[networkId] = new PlayerData(playerName ?? $"Player#{networkId}"); + Rpc(nameof(UpdateEntry), networkId, 0, playerName ?? $"Player#{networkId}"); } public void RemovePlayer(int networkId) { - Players.Remove(networkId); + Rpc(nameof(DropEntry), networkId); } public void AddScore(int networkId, int amount = 1) { - if (Players[networkId]?.Score == null) return; - Players[networkId]!.Score += amount; + if (!Players.TryGetValue(networkId, out var player)) return; + player.Score += amount; + Rpc(nameof(UpdateEntry), networkId, player.Score, player.Name); + } + + public void SetName(int networkId, string name) + { + if (!Players.TryGetValue(networkId, out var player)) return; + player.Name = name; + Rpc(nameof(UpdateEntry), networkId, player.Score, player.Name); + } + + public PlayerData GetData(int networkId) + { + return Players.GetValueOrDefault(networkId); + } + + public PlayerData GetCurrentData() + { + return Players.GetValueOrDefault(Multiplayer.GetUniqueId()); + } + + [Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)] + private void UpdateEntry(int networkId, int score, string name) + { + if (Players.ContainsKey(networkId)) + { + Players[networkId].Score = score; + Players[networkId].Name = name; + } + else + { + Players.Add(networkId, new PlayerData(name, score)); + } + } + + [Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)] + private void DropEntry(int networkId) + { + Players.Remove(networkId); } } diff --git a/Scripts/Logic/World.cs b/Scripts/Logic/World.cs index 9e141e0..303d20f 100644 --- a/Scripts/Logic/World.cs +++ b/Scripts/Logic/World.cs @@ -77,7 +77,6 @@ public partial class World : Node2D 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; @@ -85,6 +84,7 @@ public partial class World : Node2D }; AddChild(player, true); + player.PlayerName = name; } private void PutPlayers(string currentPlayerName = null) @@ -108,7 +108,6 @@ public partial class World : Node2D } PutPlayers(); - _roundTimer.Start(); } diff --git a/Scripts/Player.cs b/Scripts/Player.cs index aa3208e..ad97d13 100644 --- a/Scripts/Player.cs +++ b/Scripts/Player.cs @@ -32,7 +32,12 @@ public partial class Player : CharacterBody2D [Export] public float TileSize; - [Export] public string PlayerName; + [Export] + public string PlayerName + { + get => GetParent()?.Scoreboard.GetData(PlayerId)?.Name; + set => GetParent()?.Scoreboard.SetName(PlayerId, value); + } [Export] public int PlayerId @@ -63,10 +68,7 @@ public partial class Player : CharacterBody2D if (PlayerId == Multiplayer.GetUniqueId()) PlayerCamera.Enabled = true; - - PlayerName ??= $"Player#{PlayerId}"; - GetNode