AceField/Scripts/UI/Leaderboard.cs

68 lines
1.5 KiB
C#
Raw Permalink Normal View History

2024-08-09 07:45:40 +00:00
using System.Linq;
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;
2024-08-09 07:45:40 +00:00
if (Scoreboard.Players.Count == 1) return;
2024-08-09 06:55:45 +00:00
if (GetNode<Control>("List").GetChildCount() != Scoreboard.Players.Count)
{
CleanNodes();
CreateNodes();
return;
}
var place = 1;
2024-08-09 07:45:40 +00:00
var data = Scoreboard.Players.ToList();
data.Sort((a, b) => b.Value.Score.CompareTo(a.Value.Score));
foreach (var item in data)
2024-08-09 06:55:45 +00:00
{
2024-08-09 07:45:40 +00:00
var record = GetNode<Control>("List").GetNode<LeaderboardRecord>($"Record_{place}");
2024-08-09 06:55:45 +00:00
2024-08-09 07:45:40 +00:00
record!.PlayerId = item.Key;
2024-08-09 06:55:45 +00:00
record.Place = place;
2024-08-09 07:45:40 +00:00
record.Score = item.Value.Score;
record.PlayerName = item.Value.Name;
2024-08-09 06:55:45 +00:00
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);
2024-08-09 07:45:40 +00:00
instance.Name = $"Record_{place}";
2024-08-09 06:55:45 +00:00
GetNode("List").AddChild(instance);
place++;
}
}
}