🐛 Bug fixes
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user