summaryrefslogtreecommitdiff
path: root/scripts/Entities/Actors/Inventory.cs
blob: ebc60e4c1ca909f52c7ac277625f9457b8b0f143 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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, ISaveable
{
	private Player player;
	public int Capacity { get; private set; } = capacity;
	public Godot.Collections.Array<Item> Items { get; private set; } = [];

	public override void _Ready()
	{
		base._Ready();
		player = GetParent<Player>();
	}

	public void Drop(Item item)
	{
		Items.Remove(item);

		MapData data = player.MapData;

		ItemEntity itemEnt = new(player.GridPosition, data, item);

		data.InsertEntity(itemEnt);
		data.EmitSignal(MapData.SignalName.EntityPlaced, itemEnt);
		
		MessageLogData.Instance.AddMessage($"Você descarta {item.Definition.DisplayName}.");
	}

	public void Add(Item item)
	{
		if (Items.Count >= Capacity) return;

		Items.Add(item);
	}

	public void RemoveItem(Item item)
	{
		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;
	}
}