diff options
| author | Gustavoeklund01 <eklundgu@gmail.com> | 2025-11-10 17:25:59 -0300 |
|---|---|---|
| committer | Gustavoeklund01 <eklundgu@gmail.com> | 2025-11-10 17:25:59 -0300 |
| commit | 1a042d7303081e8bc72e9e3c341db3e528c8d998 (patch) | |
| tree | cb56eafbd73ab8c7f8ba231fd512ae246b4dd64b /scripts/Entities | |
| parent | de99779d19b77d174c561cb3795538412f53bcbc (diff) | |
| parent | f46d058571d453ccf7ddf884ccb5d8694f970d7c (diff) | |
Diffstat (limited to 'scripts/Entities')
| -rw-r--r-- | scripts/Entities/Actions/EscapeAction.cs | 76 | ||||
| -rw-r--r-- | scripts/Entities/Actors/Actor.cs | 6 |
2 files changed, 80 insertions, 2 deletions
diff --git a/scripts/Entities/Actions/EscapeAction.cs b/scripts/Entities/Actions/EscapeAction.cs index f0b1ed5..1b3aae2 100644 --- a/scripts/Entities/Actions/EscapeAction.cs +++ b/scripts/Entities/Actions/EscapeAction.cs @@ -1,13 +1,85 @@ +using System; +using Godot; +using Godot.Collections; +using Microsoft.VisualBasic; using TheLegendOfGustav.Entities.Actors; using TheLegendOfGustav.Utils; namespace TheLegendOfGustav.Entities.Actions; -public partial class EscapeAction(Actor actor) : Action(actor) +public partial class EscapeAction(Actor actor, bool should_save = false) : Action(actor) { + private bool should_save = should_save; public override bool Perform() { - Actor.MapData.SaveGame(); + if (should_save) { + Actor.MapData.SaveGame(); + } else { + // game over + bool hasLeaderboardFile = FileAccess.FileExists("user://placar.json"); + if (hasLeaderboardFile) { + using var leaderboardFile = FileAccess.Open("user://placar.json", FileAccess.ModeFlags.ReadWrite); + 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) + { + leaderboardFile.Resize(0); + leaderboardFile.Seek(0); + + leaderBoardData = new() + { + {"placar", new Array<Dictionary<string, Variant>>() {Stats.Instance.Serialize()}} + }; + boardString = Json.Stringify(leaderBoardData); + + leaderboardFile.StoreLine(boardString); + + SignalBus.Instance.EmitSignal(SignalBus.SignalName.EscapeRequested); + return false; + } + + Array<Dictionary<string, Variant>> players = (Array<Dictionary<string, Variant>>)leaderBoardData["placar"]; + + players.Add(Stats.Instance.Serialize()); + + for (int i = 0; i < players.Count; i++) { + for (int j = 0; j < players.Count - 1 - i; j++) { + if ((int)players[j]["andar_mais_fundo"] < (int)players[j + 1]["andar_mais_fundo"]) { + Dictionary<string, Variant> tmp = players[j]; + players[j] = players[j + 1]; + players[j + 1] = tmp; + } + } + } + + if (players.Count > 10) { + players = players.GetSliceRange(0, 10); + } + + leaderBoardData["placar"] = players; + + leaderboardFile.Resize(0); + leaderboardFile.Seek(0); + leaderboardFile.StoreLine(Json.Stringify(leaderBoardData)); + } else { + using var leaderboardFile = FileAccess.Open("user://placar.json", FileAccess.ModeFlags.Write); + Dictionary<string, Variant> leaderBoardData = new() + { + {"placar", new Array<Dictionary<string, Variant>>() {Stats.Instance.Serialize()}} + }; + string boardString = Json.Stringify(leaderBoardData); + + leaderboardFile.StoreLine(boardString); + } + } SignalBus.Instance.EmitSignal(SignalBus.SignalName.EscapeRequested); return false; } diff --git a/scripts/Entities/Actors/Actor.cs b/scripts/Entities/Actors/Actor.cs index c68cc2b..e6b8867 100644 --- a/scripts/Entities/Actors/Actor.cs +++ b/scripts/Entities/Actors/Actor.cs @@ -101,6 +101,9 @@ public partial class Actor : Entity, ISaveable get => hp; set { + if (MapData != null && MapData.Player == this && hp > value) { + Stats.Instance.DamageTaken += (hp - value); + } // Esta propriedade impede que o HP seja maior que o máximo. hp = int.Clamp(value, 0, MaxHp); EmitSignal(SignalName.HealthChanged, Hp, MaxHp); @@ -280,6 +283,9 @@ public partial class Actor : Entity, ISaveable else { deathMessage = $"{DisplayName} morreu!"; + if (!inLoading) { + Stats.Instance.EnemiesKilled++; + } } |
