🐛 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();
|
||||
}
|
||||
|
||||
|
@ -32,7 +32,12 @@ public partial class Player : CharacterBody2D
|
||||
|
||||
[Export] public float TileSize;
|
||||
|
||||
[Export] public string PlayerName;
|
||||
[Export]
|
||||
public string PlayerName
|
||||
{
|
||||
get => GetParent<World>()?.Scoreboard.GetData(PlayerId)?.Name;
|
||||
set => GetParent<World>()?.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<Label>("Overlay/NameTag").Text = PlayerName;
|
||||
|
||||
|
||||
GetNode<ProgressBar>("Overlay/HealthBar").Value = Health / MaxHealth * 100;
|
||||
|
||||
GetNode<Timer>("ReloadTimer").Timeout += () =>
|
||||
@ -81,6 +83,8 @@ public partial class Player : CharacterBody2D
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
GetNode<Label>("Overlay/NameTag").Text = PlayerName;
|
||||
|
||||
if (PlayerInput.IsShooting)
|
||||
{
|
||||
PlayerInput.IsShooting = false;
|
||||
|
@ -22,9 +22,7 @@ public partial class HUD : Control
|
||||
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;
|
||||
@ -48,5 +46,8 @@ public partial class HUD : Control
|
||||
|
||||
var positionLabel = GetNode<Label>("BottomBox/HBox/PositionLabel");
|
||||
positionLabel.Text = $"({player.Position.X:F2}, {player.Position.Y:F2})";
|
||||
|
||||
var scoreLabel = GetNode<Label>("BottomBox/HBox/ScoreLabel");
|
||||
scoreLabel.Text = $"Score {World.Scoreboard.GetCurrentData()?.Score.ToString() ?? "-"}";
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user