summaryrefslogtreecommitdiff
path: root/scripts/Entities
diff options
context:
space:
mode:
authorMatheus <matheus.guedes.mg.m@gmail.com>2025-10-26 20:02:15 -0300
committerMatheus <matheus.guedes.mg.m@gmail.com>2025-10-26 20:02:15 -0300
commitf4ed469fc9eaeebf39093fbf6601581cc10c6e2f (patch)
treed8f29ae3288e950b5fb1f5251845d57949ca2ac0 /scripts/Entities
parent639cd8cef403e9e66bf31e7888e786effac2b292 (diff)
feat:save AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
O vazio dentro de mim é como uma xícara de café esquecida no canto da mesa.
Diffstat (limited to 'scripts/Entities')
-rw-r--r--scripts/Entities/Actions/EscapeAction.cs1
-rw-r--r--scripts/Entities/Actors/Actor.cs49
-rw-r--r--scripts/Entities/Actors/Enemy.cs32
-rw-r--r--scripts/Entities/Actors/Inventory.cs34
-rw-r--r--scripts/Entities/Actors/Player.cs32
-rw-r--r--scripts/Entities/Entity.cs25
-rw-r--r--scripts/Entities/Items/Item.cs24
-rw-r--r--scripts/Entities/Items/ItemEntity.cs40
8 files changed, 229 insertions, 8 deletions
diff --git a/scripts/Entities/Actions/EscapeAction.cs b/scripts/Entities/Actions/EscapeAction.cs
index cc5d289..f0b1ed5 100644
--- a/scripts/Entities/Actions/EscapeAction.cs
+++ b/scripts/Entities/Actions/EscapeAction.cs
@@ -7,6 +7,7 @@ public partial class EscapeAction(Actor actor) : Action(actor)
{
public override bool Perform()
{
+ Actor.MapData.SaveGame();
SignalBus.Instance.EmitSignal(SignalBus.SignalName.EscapeRequested);
return false;
}
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);
diff --git a/scripts/Entities/Entity.cs b/scripts/Entities/Entity.cs
index b7dbcc2..76002b1 100644
--- a/scripts/Entities/Entity.cs
+++ b/scripts/Entities/Entity.cs
@@ -1,4 +1,5 @@
using Godot;
+using Godot.Collections;
using TheLegendOfGustav.Map;
using TheLegendOfGustav.Utils;
@@ -18,7 +19,7 @@ public enum EntityType
/// <summary>
/// Classe para elementos móveis que o jogador pode interagir.
/// </summary>
-public abstract partial class Entity : Sprite2D
+public abstract partial class Entity : Sprite2D, ISaveable
{
private Vector2I gridPosition = Vector2I.Zero;
@@ -138,4 +139,26 @@ public abstract partial class Entity : Sprite2D
Type = definition.Type;
Texture = definition.texture;
}
+
+ public Dictionary<string, Variant> GetSaveData()
+ {
+ return new()
+ {
+ {"position_x", GridPosition.X},
+ {"position_y", GridPosition.Y},
+ {"blocks_movement", BlocksMovement},
+ {"name", DisplayName},
+ {"layer", (int)Type},
+
+ };
+ }
+
+ public bool LoadSaveData(Dictionary<string, Variant> saveData)
+ {
+ GridPosition = new((int)saveData["position_x"], (int)saveData["position_y"]);
+ BlocksMovement = (bool)saveData["blocks_movement"];
+ DisplayName = (string)saveData["name"];
+ Type = (EntityType)(int)saveData["layer"];
+ return true;
+ }
} \ No newline at end of file
diff --git a/scripts/Entities/Items/Item.cs b/scripts/Entities/Items/Item.cs
index 0eeffd8..17c318c 100644
--- a/scripts/Entities/Items/Item.cs
+++ b/scripts/Entities/Items/Item.cs
@@ -1,11 +1,12 @@
using System.Reflection.Metadata;
using Godot;
+using Godot.Collections;
using TheLegendOfGustav.Entities.Actions;
using TheLegendOfGustav.Entities.Actors;
namespace TheLegendOfGustav.Entities.Items;
-public partial class Item : RefCounted
+public partial class Item : RefCounted, ISaveable
{
public Item(ItemResource definition)
@@ -14,6 +15,10 @@ public partial class Item : RefCounted
Uses = Definition.MaxUses;
}
+ public Item()
+ {
+ }
+
public ItemResource Definition { get; private set; }
public int Uses { get; set; }
@@ -58,4 +63,21 @@ public partial class Item : RefCounted
Inventory inventory = consumer.Inventory;
inventory.RemoveItem(this);
}
+
+ public Dictionary<string, Variant> GetSaveData()
+ {
+ return new()
+ {
+ {"definition", Definition.ResourcePath},
+ {"uses", Uses}
+ };
+ }
+
+ public bool LoadSaveData(Dictionary<string, Variant> saveData)
+ {
+ Definition = GD.Load<ItemResource>((string)saveData["definition"]);
+ Uses = (int)saveData["uses"];
+
+ return true;
+ }
} \ No newline at end of file
diff --git a/scripts/Entities/Items/ItemEntity.cs b/scripts/Entities/Items/ItemEntity.cs
index e646e40..66944ce 100644
--- a/scripts/Entities/Items/ItemEntity.cs
+++ b/scripts/Entities/Items/ItemEntity.cs
@@ -1,10 +1,11 @@
using Godot;
+using Godot.Collections;
using TheLegendOfGustav.Entities;
using TheLegendOfGustav.Map;
namespace TheLegendOfGustav.Entities.Items;
-public partial class ItemEntity : Entity
+public partial class ItemEntity : Entity, ISaveable
{
public Item Item { get; private set; }
@@ -14,7 +15,7 @@ public partial class ItemEntity : Entity
// Eu quero muito reescrever o jogo do zero, mas não tenho tempo :(
EntityDefinition sad = new()
{
- blocksMovement = true,
+ blocksMovement = false,
name = item.Definition.DisplayName,
texture = item.Definition.Icon,
Type = EntityType.ITEM,
@@ -22,4 +23,39 @@ public partial class ItemEntity : Entity
SetDefinition(sad);
}
+ public ItemEntity(Vector2I initialPosition, MapData map) : base(initialPosition, map)
+ {
+ }
+
+ public new Dictionary<string, Variant> GetSaveData()
+ {
+ Dictionary<string, Variant> baseData = base.GetSaveData();
+ baseData.Add("item", Item.GetSaveData());
+
+ return baseData;
+ }
+
+ public new bool LoadSaveData(Dictionary<string, Variant> saveData)
+ {
+ Item = new();
+
+ if (!Item.LoadSaveData((Dictionary<string, Variant>)saveData["item"]))
+ {
+ return false;
+ }
+ // Eu quero muito reescrever o jogo do zero, mas não tenho tempo :(
+ EntityDefinition sad = new()
+ {
+ blocksMovement = false,
+ name = Item.Definition.DisplayName,
+ texture = Item.Definition.Icon,
+ Type = EntityType.ITEM,
+ };
+
+ SetDefinition(sad);
+
+ base.LoadSaveData(saveData);
+
+ return true;
+ }
} \ No newline at end of file