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 | |
| parent | de99779d19b77d174c561cb3795538412f53bcbc (diff) | |
| parent | f46d058571d453ccf7ddf884ccb5d8694f970d7c (diff) | |
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/Entities/Actions/EscapeAction.cs | 76 | ||||
| -rw-r--r-- | scripts/Entities/Actors/Actor.cs | 6 | ||||
| -rw-r--r-- | scripts/GUI/LeaderboardItem.cs | 41 | ||||
| -rw-r--r-- | scripts/GUI/LeaderboardItem.cs.uid | 1 | ||||
| -rw-r--r-- | scripts/GUI/MainMenu.cs | 12 | ||||
| -rw-r--r-- | scripts/Game.cs | 2 | ||||
| -rw-r--r-- | scripts/GameManager.cs | 10 | ||||
| -rw-r--r-- | scripts/InputHandling/MainGameInputHandler.cs | 2 | ||||
| -rw-r--r-- | scripts/Map/DungeonGenerator.cs | 68 | ||||
| -rw-r--r-- | scripts/Utils/Stats.cs | 52 | ||||
| -rw-r--r-- | scripts/Utils/Stats.cs.uid | 1 |
11 files changed, 250 insertions, 21 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++; + } } diff --git a/scripts/GUI/LeaderboardItem.cs b/scripts/GUI/LeaderboardItem.cs new file mode 100644 index 0000000..c05bf58 --- /dev/null +++ b/scripts/GUI/LeaderboardItem.cs @@ -0,0 +1,41 @@ +using Godot; + +namespace TheLegendOfGustav.GUI; + +public partial class LeaderboardItem : HBoxContainer +{ + [Export] + public string PlayerName { get; set; } = "Jogador"; + [Export] + public string Floor { get; set; } = "Andar Máximo"; + [Export] + public string Kills { get; set; } = "Inimigos Mortos"; + [Export] + public string Damage { get; set; } = "Dano tomado"; + + private Label nameLabel; + private Label floorLabel; + private Label killsLabel; + private Label damageLabel; + + + // Called when the node enters the scene tree for the first time. + public override void _Ready() + { + nameLabel = GetNode<Label>("hdNome"); + floorLabel = GetNode<Label>("hdAndar"); + killsLabel = GetNode<Label>("hdkills"); + damageLabel = GetNode<Label>("hddamage"); + + + UpdateLabels(); + } + + public void UpdateLabels() + { + nameLabel.Text = PlayerName; + floorLabel.Text = Floor; + killsLabel.Text = Kills; + damageLabel.Text = Damage; + } +} diff --git a/scripts/GUI/LeaderboardItem.cs.uid b/scripts/GUI/LeaderboardItem.cs.uid new file mode 100644 index 0000000..ddac2bd --- /dev/null +++ b/scripts/GUI/LeaderboardItem.cs.uid @@ -0,0 +1 @@ +uid://cbjltiujfsw4v diff --git a/scripts/GUI/MainMenu.cs b/scripts/GUI/MainMenu.cs index fc46cd2..0d6c8c6 100644 --- a/scripts/GUI/MainMenu.cs +++ b/scripts/GUI/MainMenu.cs @@ -7,9 +7,12 @@ public partial class MainMenu : Control private Button newGameButton; private Button loadGameButton; private Button quitButton; + private Button leaderboardButton; [Signal] public delegate void GameRequestEventHandler(bool load); + [Signal] + public delegate void LeaderboardRequestEventHandler(); public override void _Ready() { @@ -18,14 +21,18 @@ public partial class MainMenu : Control newGameButton = GetNode<Button>("VBoxContainer/CenterContainer/VBoxContainer/neogame"); loadGameButton = GetNode<Button>("VBoxContainer/CenterContainer/VBoxContainer/continue"); quitButton = GetNode<Button>("VBoxContainer/CenterContainer/VBoxContainer/quit"); + leaderboardButton = GetNode<Button>("VBoxContainer/CenterContainer/VBoxContainer/leaderboard"); newGameButton.Pressed += OnNewGameButtonPressed; loadGameButton.Pressed += OnLoadGameButtonPressed; quitButton.Pressed += OnQuitButtonPressed; + leaderboardButton.Pressed += OnLeaderBoardRequest; newGameButton.GrabFocus(); bool hasSaveFile = FileAccess.FileExists("user://save_game.json"); + bool hasLeaderboard = FileAccess.FileExists("user://placar.json"); loadGameButton.Disabled = !hasSaveFile; + leaderboardButton.Disabled = !hasLeaderboard; } private void OnNewGameButtonPressed() @@ -38,6 +45,11 @@ public partial class MainMenu : Control EmitSignal(SignalName.GameRequest, true); } + private void OnLeaderBoardRequest() + { + EmitSignal(SignalName.LeaderboardRequest); + } + private void OnQuitButtonPressed() { GetTree().Quit(); diff --git a/scripts/Game.cs b/scripts/Game.cs index e55b937..cc86928 100644 --- a/scripts/Game.cs +++ b/scripts/Game.cs @@ -90,6 +90,8 @@ public partial class Game : Node player.AddChild(camera); + Stats.Instance.PlayerName = player.DisplayName; + if (!map.LoadGame(player)) { return false; diff --git a/scripts/GameManager.cs b/scripts/GameManager.cs index 5898744..4215475 100644 --- a/scripts/GameManager.cs +++ b/scripts/GameManager.cs @@ -10,6 +10,7 @@ public partial class GameManager : Node private PackedScene mainMenuScene = GD.Load<PackedScene>("res://scenes/GUI/main_menu.tscn"); private PackedScene gameScene = GD.Load<PackedScene>("res://scenes/Game.tscn"); private PackedScene nameScene = GD.Load<PackedScene>("res://scenes/name_thyself.tscn"); + private PackedScene leaderboardScene = GD.Load<PackedScene>("res://scenes/GUI/Leaderboard.tscn"); private Node currentScene; @@ -42,7 +43,9 @@ public partial class GameManager : Node private void LoadMainMenu() { MainMenu menu = (MainMenu)SwitchToScene(mainMenuScene); + Stats.Instance.Clear(); menu.GameRequest += OnGameRequest; + menu.LeaderboardRequest += OnLeaderboardRequest; } private void LoadGame() @@ -72,6 +75,7 @@ public partial class GameManager : Node private void OnNameSelect(string name) { + Stats.Instance.PlayerName = name; NewGame(name); } @@ -86,4 +90,10 @@ public partial class GameManager : Node LoadGame(); } } + + private void OnLeaderboardRequest() + { + Leaderboard scene = (Leaderboard)SwitchToScene(leaderboardScene); + scene.MenuRequested += LoadMainMenu; + } } diff --git a/scripts/InputHandling/MainGameInputHandler.cs b/scripts/InputHandling/MainGameInputHandler.cs index a18bd73..d1b5818 100644 --- a/scripts/InputHandling/MainGameInputHandler.cs +++ b/scripts/InputHandling/MainGameInputHandler.cs @@ -60,7 +60,7 @@ public partial class MainGameInputHandler : BaseInputHandler if (Input.IsActionJustPressed("quit")) { - action = new EscapeAction(player); + action = new EscapeAction(player, true); } if (Input.IsActionJustPressed("descend")) diff --git a/scripts/Map/DungeonGenerator.cs b/scripts/Map/DungeonGenerator.cs index 31dcffe..cce693a 100644 --- a/scripts/Map/DungeonGenerator.cs +++ b/scripts/Map/DungeonGenerator.cs @@ -3,6 +3,9 @@ using TheLegendOfGustav.Entities.Actors; using TheLegendOfGustav.Entities; using TheLegendOfGustav.Entities.Items; using System; +using Godot.Collections; +using System.Diagnostics.Metrics; +using System.Numerics; namespace TheLegendOfGustav.Map; @@ -14,18 +17,41 @@ public partial class DungeonGenerator : Node { #region Fields /// <summary> + /// Chave: Andar mínimo + /// Valor: Número de máximo de monstros por sala + /// </summary> + private static readonly Dictionary<int, int> maxMonstersByFloor = new() + { + {1, 2}, + {4, 3}, + {6, 4}, + {10, 8} + }; + /// <summary> + /// Chave: Andar mínimo + /// Valor: Número de máximo de itens por sala + /// </summary> + private static readonly Dictionary<int, int> maxItemsByFloor = new() + { + {1, 1}, + {4, 2}, + {6, 3}, + }; + /// <summary> /// Coleção de todos os inimigos que o gerador tem acesso. /// </summary> private static readonly Godot.Collections.Array<EnemyDefinition> enemies = [ GD.Load<EnemyDefinition>("res://assets/definitions/actor/Skeleton.tres"), GD.Load<EnemyDefinition>("res://assets/definitions/actor/morcegao.tres"), GD.Load<EnemyDefinition>("res://assets/definitions/actor/Shadow.tres"), - GD.Load<EnemyDefinition>("res://assets/definitions/actor/omal.tres") + GD.Load<EnemyDefinition>("res://assets/definitions/actor/omal.tres"), + GD.Load<EnemyDefinition>("res://assets/definitions/actor/toilet.tres") ]; private static readonly Godot.Collections.Array<ItemResource> items = [ GD.Load<ItemResource>("res://assets/definitions/Items/small_healing_potion.tres"), - GD.Load<ItemResource>("res://assets/definitions/Items/mana_bolt_grimoire.tres") + GD.Load<ItemResource>("res://assets/definitions/Items/mana_bolt_grimoire.tres"), + GD.Load<ItemResource>("res://assets/definitions/Items/big_potion_of_heals.tres") ]; /// <summary> @@ -48,24 +74,30 @@ public partial class DungeonGenerator : Node /// </summary> [ExportCategory("RNG")] private RandomNumberGenerator rng = new(); - - /// <summary> - /// Quantidade máxima de inimigos por sala. - /// </summary> - [ExportCategory("Monster RNG")] - [Export] - private int maxMonstersPerRoom = 2; - - /// <summary> - /// Quantidade máxima de itens por sala. - /// </summary> - [ExportCategory("Loot RNG")] - [Export] - private int maxItemsPerRoom = 2; #endregion #region Methods + private int GetMaxIValueForFloor(Dictionary<int, int> valueTable, int currentFloor) { + int currentValue = 0; + + int? key = null; + + foreach (int theKey in valueTable.Keys) { + if (theKey > currentFloor) { + break; + } else { + key = theKey; + } + } + + if (key.HasValue) { + currentValue = valueTable[key.Value]; + } + + return currentValue; + } + /// <summary> /// Gera um andar da masmorra. /// Inimigos são colocados conforme configurações. @@ -230,9 +262,9 @@ public partial class DungeonGenerator : Node private void PlaceEntities(MapData data, Rect2I room) { // Define quantos monstros serão colocados na sala - int monsterAmount = rng.RandiRange(0, maxMonstersPerRoom); + int monsterAmount = rng.RandiRange(0, GetMaxIValueForFloor(maxMonstersByFloor, data.CurrentFloor)); // Define quantos itens serão colocados na sala. - int itemAmount = rng.RandiRange(0, maxItemsPerRoom); + int itemAmount = rng.RandiRange(0, GetMaxIValueForFloor(maxItemsByFloor, data.CurrentFloor)); for (int i = 0; i < monsterAmount; i++) { diff --git a/scripts/Utils/Stats.cs b/scripts/Utils/Stats.cs new file mode 100644 index 0000000..d3c4aa3 --- /dev/null +++ b/scripts/Utils/Stats.cs @@ -0,0 +1,52 @@ +using Godot; +using Godot.Collections; +using TheLegendOfGustav.Utils; + +namespace TheLegendOfGustav.Utils; + +public partial class Stats : Node +{ + public static Stats Instance { get; set; } + + public string PlayerName { get; set; } + public int MaxFloor { get; set; } = 0; + + public int EnemiesKilled { get; set; } = 0; + + public int DamageTaken { get; set; } = 0; + + public override void _Ready() + { + base._Ready(); + Instance = this; + + SignalBus.Instance.DungeonFloorChanged += OnFloorChange; + } + + public void Clear() + { + PlayerName = ""; + MaxFloor = 0; + EnemiesKilled = 0; + DamageTaken = 0; + } + + void OnFloorChange(int floor) + { + if (floor > MaxFloor) + { + MaxFloor = floor; + } + } + + public Dictionary<string, Variant> Serialize() + { + return new() + { + {"jogador", PlayerName}, + {"andar_mais_fundo", MaxFloor}, + {"inimigos_mortos", EnemiesKilled}, + {"dano_tomado", DamageTaken} + }; + } +}
\ No newline at end of file diff --git a/scripts/Utils/Stats.cs.uid b/scripts/Utils/Stats.cs.uid new file mode 100644 index 0000000..a93778d --- /dev/null +++ b/scripts/Utils/Stats.cs.uid @@ -0,0 +1 @@ +uid://eg7t08o6wpxb |
