summaryrefslogtreecommitdiff
path: root/scripts/InputHandling/SpellMenuInputHandler.cs
blob: ba9c1284424630fdb7204304600842717cacbcf4 (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
using Godot;
using TheLegendOfGustav.Entities.Actions;
using TheLegendOfGustav.Entities.Actors;
using TheLegendOfGustav.GUI;
using TheLegendOfGustav.Magic;
using TheLegendOfGustav.Utils;

namespace TheLegendOfGustav.InputHandling;

public partial class SpellMenuInputHandler : BaseInputHandler
{
	private static readonly PackedScene spellMenuScene = GD.Load<PackedScene>("res://scenes/GUI/spellbook_menu.tscn");

	[Export]
	private Map.Map map;

	private SpellBookMenu spellBookMenu;
	private SpellResource spellCast = null;

	public override void Enter()
	{
		spellBookMenu = spellMenuScene.Instantiate<SpellBookMenu>();
		map.MapData.Player.AddChild(spellBookMenu);
		spellBookMenu.Initialize(map.MapData.Player.SpellBook);
		spellBookMenu.SpellSelected += OnSpellCast;
	}

	public override void Exit()
	{
		spellCast = null;
		spellBookMenu.QueueFree();
	}

	public override Action GetAction(Player player)
	{
		Action action = null;

		if (spellCast != null)
		{
			if (spellCast.Type == SpellType.Ranged)
			{
				SignalBus.Instance.EmitSignal(SignalBus.SignalName.PlayerSpellChooseLocation, spellCast);
				GetParent<InputHandler>().SetInputHandler(InputHandlers.CastSpell);
				return action;
			}
			action = new SpellAction(player, Vector2I.Zero, spellCast);
			Close();
			return action;
		}

		if (Input.IsActionJustPressed("quit"))
		{
			Close();
		}

		return action;
	}

	private void Close()
	{
		GetParent<InputHandler>().SetInputHandler(InputHandlers.MainGame);
	}

	private void OnSpellCast(SpellResource spell)
	{
		spellCast = spell;
	}
}