diff options
Diffstat (limited to 'scripts/Entities/Actors')
| -rw-r--r-- | scripts/Entities/Actors/Actor.cs | 49 | ||||
| -rw-r--r-- | scripts/Entities/Actors/Enemy.cs | 32 | ||||
| -rw-r--r-- | scripts/Entities/Actors/Inventory.cs | 34 | ||||
| -rw-r--r-- | scripts/Entities/Actors/Player.cs | 32 |
4 files changed, 143 insertions, 4 deletions
diff --git a/scripts/Entities/Actors/Actor.cs b/scripts/Entities/Actors/Actor.cs index ec00e30..71a32a5 100644 --- a/scripts/Entities/Actors/Actor.cs +++ b/scripts/Entities/Actors/Actor.cs @@ -1,4 +1,5 @@ using Godot; +using Godot.Collections; using TheLegendOfGustav.Magic; using TheLegendOfGustav.Map; using TheLegendOfGustav.Utils; @@ -9,7 +10,7 @@ namespace TheLegendOfGustav.Entities.Actors; /// A classe de ator define um personagem no jogo. /// </summary> [GlobalClass] -public partial class Actor : Entity +public partial class Actor : Entity, ISaveable { #region Fields private int mp; @@ -29,6 +30,9 @@ public partial class Actor : Entity { SetDefinition(definition); } + public Actor(Vector2I initialPosition, MapData map) : base(initialPosition, map) + { + } #endregion #region Signals @@ -278,5 +282,48 @@ public partial class Actor : Entity MapData.UnregisterBlockingEntity(this); EmitSignal(SignalName.Died); } + + public new Dictionary<string, Variant> GetSaveData() + { + Dictionary<string, Variant> baseData = base.GetSaveData(); + baseData.Add("energy", Energy); + baseData.Add("max_hp", MaxHp); + baseData.Add("hp", Hp); + baseData.Add("max_mp", MaxMp); + baseData.Add("mp", MaxMp); + baseData.Add("atk", Atk); + baseData.Add("def", Def); + baseData.Add("men", Men); + baseData.Add("mp_regen_rate", MpRegenRate); + baseData.Add("mp_regen_per_turn", MpRegenPerTurn); + baseData.Add("spell_book", SpellBook.GetSaveData()); + + return baseData; + } + + public new bool LoadSaveData(Dictionary<string, Variant> saveData) + { + if (!base.LoadSaveData(saveData)) + { + return false; + } + + if (!SpellBook.LoadSaveData((Dictionary<string, Variant>)saveData["spell_book"])) + { + return false; + } + + Energy = (int)saveData["energy"]; + MaxHp = (int)saveData["max_hp"]; + Hp = (int)saveData["hp"]; + MaxMp = (int)saveData["max_mp"]; + Mp = (int)saveData["max_mp"]; + Atk = (int)saveData["atk"]; + Def = (int)saveData["def"]; + Men = (int)saveData["men"]; + MpRegenRate = (int)saveData["mp_regen_rate"]; + MpRegenPerTurn = (int)saveData["mp_regen_per_turn"]; + return true; + } #endregion }
\ No newline at end of file diff --git a/scripts/Entities/Actors/Enemy.cs b/scripts/Entities/Actors/Enemy.cs index d398176..4994534 100644 --- a/scripts/Entities/Actors/Enemy.cs +++ b/scripts/Entities/Actors/Enemy.cs @@ -1,4 +1,5 @@ using Godot; +using Godot.Collections; using TheLegendOfGustav.Entities.Actors.AI; using TheLegendOfGustav.Map; @@ -8,7 +9,7 @@ namespace TheLegendOfGustav.Entities.Actors; /// Um inimigo é uma espécie de ator que é /// hostil ao jogador. Inimigos são controlados por IA. /// </summary> -public partial class Enemy : Actor +public partial class Enemy : Actor, ISaveable { private EnemyDefinition definition; @@ -17,6 +18,9 @@ public partial class Enemy : Actor this.definition = definition; SetDefinition(definition); } + public Enemy(Vector2I initialPosition, MapData map) : base(initialPosition, map) + { + } /// <summary> /// A alma do ator. Gera ações que são executadas todo turno. @@ -30,9 +34,12 @@ public partial class Enemy : Actor /// <param name="definition">Definição do inimigo.</param> public void SetDefinition(EnemyDefinition definition) { + this.definition = definition; // Definimos as características do ator. base.SetDefinition(this.definition); + Soul?.QueueFree(); + // Definimos qual IA utilizar. switch (definition.AI) { @@ -51,4 +58,27 @@ public partial class Enemy : Actor Soul = null; base.Die(); } + + public new Dictionary<string, Variant> GetSaveData() + { + Dictionary<string, Variant> baseData = base.GetSaveData(); + baseData.Add("definition", definition.ResourcePath); + + return baseData; + } + + public new bool LoadSaveData(Dictionary<string, Variant> saveData) + { + string definitionPath = (string)saveData["definition"]; + EnemyDefinition definition = GD.Load<EnemyDefinition>(definitionPath); + + SetDefinition(definition); + + if (!base.LoadSaveData(saveData)) + { + return false; + } + + return true; + } } diff --git a/scripts/Entities/Actors/Inventory.cs b/scripts/Entities/Actors/Inventory.cs index 5ae61b4..ebc60e4 100644 --- a/scripts/Entities/Actors/Inventory.cs +++ b/scripts/Entities/Actors/Inventory.cs @@ -1,11 +1,12 @@ using Godot; +using Godot.Collections; using TheLegendOfGustav.Entities.Items; using TheLegendOfGustav.Map; using TheLegendOfGustav.Utils; namespace TheLegendOfGustav.Entities.Actors; -public partial class Inventory(int capacity) : Node +public partial class Inventory(int capacity) : Node, ISaveable { private Player player; public int Capacity { get; private set; } = capacity; @@ -42,4 +43,35 @@ public partial class Inventory(int capacity) : Node { Items.Remove(item); } + + public Dictionary<string, Variant> GetSaveData() + { + Godot.Collections.Array<Dictionary<string, Variant>> itemsData = []; + foreach (Item item in Items) { + itemsData.Add(item.GetSaveData()); + } + + return new() + { + {"items", itemsData} + }; + } + + public bool LoadSaveData(Dictionary<string, Variant> saveData) + { + Array<Dictionary<string, Variant>> itemRess = (Array<Dictionary<string, Variant>>)saveData["items"]; + + foreach(Dictionary<string, Variant> item in itemRess) + { + Item it = new(); + if(!it.LoadSaveData(item)) + { + return false; + } + + Items.Add(it); + } + + return true; + } }
\ No newline at end of file diff --git a/scripts/Entities/Actors/Player.cs b/scripts/Entities/Actors/Player.cs index d6fedb9..7304e0a 100644 --- a/scripts/Entities/Actors/Player.cs +++ b/scripts/Entities/Actors/Player.cs @@ -1,4 +1,5 @@ using Godot; +using Godot.Collections; using TheLegendOfGustav.Map; namespace TheLegendOfGustav.Entities.Actors; @@ -7,7 +8,7 @@ namespace TheLegendOfGustav.Entities.Actors; /// Classe do jogador. Por enquanto não é diferente do Ator, mas isso pode mudar. /// </summary> [GlobalClass] -public partial class Player : Actor +public partial class Player : Actor, ISaveable { private PlayerDefinition definition; @@ -19,8 +20,37 @@ public partial class Player : Actor public Inventory Inventory { get; private set; } + public new Dictionary<string, Variant> GetSaveData() + { + Dictionary<string, Variant> baseData = base.GetSaveData(); + baseData.Add("inventory", Inventory.GetSaveData()); + baseData.Add("definition", definition.ResourcePath); + + return baseData; + } + + public new bool LoadSaveData(Dictionary<string, Variant> saveData) + { + PlayerDefinition definition = GD.Load<PlayerDefinition>((string)saveData["definition"]); + + SetDefinition(definition); + + if (!base.LoadSaveData(saveData)) + { + return false; + } + + if(!Inventory.LoadSaveData((Dictionary<string, Variant>)saveData["inventory"])) + { + return false; + } + + return true; + } + public void SetDefinition(PlayerDefinition definition) { + Inventory?.QueueFree(); Inventory = new(definition.InventoryCapacity); AddChild(Inventory); |
