AceField/Scripts/UI/Leaderboard.cs

64 lines
1.3 KiB
C#
Raw Normal View History

2024-08-09 06:55:45 +00:00
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++;
}
}
}