summaryrefslogtreecommitdiff
path: root/scripts/Entities/Actors/Player.cs
blob: 7304e0abed5db04c41586c09c74d762fe3c7e8cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using Godot;
using Godot.Collections;
using TheLegendOfGustav.Map;

namespace TheLegendOfGustav.Entities.Actors;

/// <summary>
/// Classe do jogador. Por enquanto não é diferente do Ator, mas isso pode mudar.
/// </summary>
[GlobalClass]
public partial class Player : Actor, ISaveable
{
	private PlayerDefinition definition;

	public Player(Vector2I initialPosition, MapData map, PlayerDefinition definition) : base(initialPosition, map, definition)
	{
		this.definition = definition;
		SetDefinition(definition);
	}

	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);
	}
}