summaryrefslogtreecommitdiff
path: root/scripts/Magic/SpellBook.cs
blob: 914add64a6a77064609abbd54b8dbf8ef0b8c50f (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
using Godot;
using Godot.Collections;

namespace TheLegendOfGustav.Magic;

public partial class SpellBook : Node, ISaveable
{
	public Godot.Collections.Array<SpellResource> KnownSpells { get; private set; } = [];
	
	public bool KnowsSpell(SpellResource spell) => KnownSpells.Contains(spell);

	public void LearnSpell(SpellResource spell) {
		if (!KnownSpells.Contains(spell)) {
			KnownSpells.Add(spell);
		}
	}

	public void ForgetSpell(SpellResource spell) {
		KnownSpells.Remove(spell);
	}

	public Dictionary<string, Variant> GetSaveData()
	{
		Array<string> spellPaths = [];
		foreach(SpellResource spell in KnownSpells)
		{
			spellPaths.Add(spell.ResourcePath);
		}

		return new()
		{
			{"spells", spellPaths}
		};
	}

	public bool LoadSaveData(Dictionary<string, Variant> saveData)
	{
		Array<string> paths = (Array<string>)saveData["spells"];

		foreach(string path in paths)
		{
			SpellResource spell = GD.Load<SpellResource>(path);
			KnownSpells.Add(spell);
		}

		return true;
	}
}