diff options
| author | Matheus <matheus.guedes.mg.m@gmail.com> | 2025-11-10 12:56:00 -0300 |
|---|---|---|
| committer | Matheus <matheus.guedes.mg.m@gmail.com> | 2025-11-10 12:56:00 -0300 |
| commit | 0f3c49b7743f7c7fabf41fdf4cb3ffe1a4ac3bf4 (patch) | |
| tree | df940efb7502899441295acd5039311b9e6e4927 /scenes/GUI/Leaderboard.cs | |
| parent | c4f5404211ef654944d5615e9055c714441f8234 (diff) | |
Placar.
Diffstat (limited to 'scenes/GUI/Leaderboard.cs')
| -rw-r--r-- | scenes/GUI/Leaderboard.cs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/scenes/GUI/Leaderboard.cs b/scenes/GUI/Leaderboard.cs new file mode 100644 index 0000000..cf126c9 --- /dev/null +++ b/scenes/GUI/Leaderboard.cs @@ -0,0 +1,62 @@ +using System; +using Godot; +using Godot.Collections; +using TheLegendOfGustav.Utils; + +namespace TheLegendOfGustav.GUI; +public partial class Leaderboard : Control +{ + [Signal] + public delegate void MenuRequestedEventHandler(); + private static readonly PackedScene LeaderboardItemScene = GD.Load<PackedScene>("res://scenes/GUI/leaderboard_item.tscn"); + + private VBoxContainer leaderboard; + private Button BackButton; + // Called when the node enters the scene tree for the first time. + public override void _Ready() + { + leaderboard = GetNode<VBoxContainer>("VBoxContainer/VBoxContainer"); + BackButton = GetNode<Button>("VBoxContainer/exit"); + + BackButton.Pressed += () => EmitSignal(SignalName.MenuRequested); + + bool hasLeaderboardFile = FileAccess.FileExists("user://placar.json"); + if (hasLeaderboardFile) + { + using var leaderboardFile = FileAccess.Open("user://placar.json", FileAccess.ModeFlags.Read); + string boardString = leaderboardFile.GetLine(); + + Dictionary<string, Variant> leaderBoardData; + + try + { + var parseResult = Json.ParseString(boardString); + if (parseResult.VariantType == Variant.Type.Nil) + { + throw new Exception(); + } + leaderBoardData = (Dictionary<string, Variant>)parseResult; + } + catch (Exception) + { + // Arquivo inválido. + return; + } + + Array<Dictionary<string, Variant>> players = (Array<Dictionary<string, Variant>>)leaderBoardData["placar"]; + + foreach (Dictionary<string, Variant> player in players) + { + LeaderboardItem item = LeaderboardItemScene.Instantiate<LeaderboardItem>(); + + item.PlayerName = (string)player["jogador"]; + item.Floor = (string)player["andar_mais_fundo"]; + item.Kills = (string)player["inimigos_mortos"]; + item.Damage = (string)player["dano_tomado"]; + + leaderboard.AddChild(item); + } + + } + } +} |
