Leaderboard

This commit is contained in:
2024-08-09 14:55:45 +08:00
parent 40b0f2e176
commit 8390fb2794
9 changed files with 191 additions and 3 deletions

View File

@ -8,6 +8,7 @@ public partial class World : Node2D
[Export] public PackedScene PlayerScene;
[Export] public int RoundCount = 1;
[Export] public int RoundTotalCount = 20;
[Export] public double RoundDuration = 60;
[Export] public double RoundProgress;
[Export] public double RoundTimeLeft;
@ -106,6 +107,12 @@ public partial class World : Node2D
private void NewRound()
{
if (RoundCount >= RoundTotalCount)
{
// TODO End this game
return;
}
RoundCount++;
foreach (var child in GetChildren())
{

View File

@ -33,7 +33,7 @@ public partial class BootScreen : Control
EmitSignal(SignalName.StartGame);
var name = string.IsNullOrEmpty(PlayerNameInput.Text) ? null : PlayerNameInput.Text;
// TODO Fix this I don't know why the first round player's name won't fully apply
World.Scoreboard.SetName(Multiplayer.GetUniqueId(), name);
World.Scoreboard.SetName(Multiplayer.GetUniqueId(), name ?? $"Player#{Multiplayer.GetUniqueId()}");
World.StartGame(currentPlayerName: name);
Hide();
}

63
Scripts/UI/Leaderboard.cs Normal file
View File

@ -0,0 +1,63 @@
using AceField.Scripts.Logic;
using Godot;
namespace AceField.Scripts.UI;
public partial class Leaderboard : Control
{
[Export] public Scoreboard Scoreboard;
public override void _Process(double delta)
{
Visible = Input.IsActionPressed("ui_leaderboard");
if (!Visible) return;
if (GetNode<Control>("List").GetChildCount() != Scoreboard.Players.Count)
{
CleanNodes();
CreateNodes();
return;
}
var place = 1;
foreach (var node in GetNode<Control>("List").GetChildren())
{
if (node is not LeaderboardRecord record) continue;
var data = Scoreboard.GetData(record.PlayerId);
if (data == null) continue;
record.Place = place;
record.Score = data.Score;
record.PlayerName = data.Name;
place++;
}
}
private void CleanNodes()
{
foreach (var child in GetNode<Control>("List").GetChildren())
{
child.QueueFree();
}
}
private void CreateNodes()
{
var blueprint = GD.Load<PackedScene>("res://Scenes/UI/LeaderboardRecord.tscn");
var place = 1;
foreach (var record in Scoreboard.Players)
{
var instance = blueprint.Instantiate<LeaderboardRecord>();
instance.Place = place;
instance.PlayerName = record.Value.Name;
instance.Score = record.Value.Score;
instance.PlayerId = record.Key;
instance.CustomMinimumSize = new Vector2(0, 80);
GetNode("List").AddChild(instance);
place++;
}
}
}

View File

@ -0,0 +1,19 @@
using Godot;
namespace AceField.Scripts.UI;
public partial class LeaderboardRecord : Control
{
[Export] public int Place;
[Export] public string PlayerName;
[Export] public int Score;
[Export] public int PlayerId;
public override void _Process(double delta)
{
GetNode<Label>("Panel/VBoxContainer/PlaceTag").Text = Place.ToString();
GetNode<Label>("Panel/VBoxContainer/NameTag").Text = PlayerName;
GetNode<Label>("Panel/VBoxContainer/ScoreTag").Text = Score.ToString();
}
}