summaryrefslogtreecommitdiff
path: root/scripts/InputHandling/InputHandler.cs
blob: e230a01095a579b656853523b73f4f9f7a737c73 (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
78
79
80
using Godot;
using TheLegendOfGustav.Entities.Actions;
using TheLegendOfGustav.Entities.Actors;
using TheLegendOfGustav.Utils;
using TheLengendOfGustav.InputHandling;

namespace TheLegendOfGustav.InputHandling;

public enum InputHandlers
{
	MainGame,
	GameOver,
	Inspect,
	Pickup,
	Inventory,
	CastSpell,
	SpellMenu
}

/// <summary>
/// Máquina de estado que obtém ações do usuário conforme o estado atual do jogo.
/// </summary>
public partial class InputHandler : Node
{
	[Export]
	private InputHandlers startingInputHandler;

	private Godot.Collections.Dictionary<InputHandlers, BaseInputHandler> inputHandlerDict = [];

	private BaseInputHandler selectedInputHandler;

	public override void _Ready()
	{
		base._Ready();
		// Controles para quando o jogador está vivo e jogando normalmente.
		inputHandlerDict.Add(InputHandlers.MainGame, GetNode<MainGameInputHandler>("MainGameInputHandler"));
		// Controles para quando o jogador está morto.
		inputHandlerDict.Add(InputHandlers.GameOver, GetNode<GameOverInputHandler>("GameOverInputHandler"));
		// Controles para observar o cenário
		inputHandlerDict.Add(InputHandlers.Inspect, GetNode<InspectInputHandler>("InspectInputHandler"));
		// Controles para pegar um item do chão.
		inputHandlerDict.Add(InputHandlers.Pickup, GetNode<PickupInputHandler>("PickupInputHandler"));
		// Controles para quando o inventário for aberto.
		inputHandlerDict.Add(InputHandlers.Inventory, GetNode<InventoryInputHandler>("InventoryInputHandler"));
		// Controles para quando o jogador precisar escolher um alvo de feitiço.
		inputHandlerDict.Add(InputHandlers.CastSpell, GetNode<CastSpellInputHandler>("CastSpellInputHandler"));
		// Controles para quando o menu de feitiços for aberto.
		inputHandlerDict.Add(InputHandlers.SpellMenu, GetNode<SpellMenuInputHandler>("SpellMenuInputHandler"));

		SetInputHandler(startingInputHandler);

		SignalBus.Instance.CommandInputHandler += SetInputHandler;
	}

	public Action GetAction(Player player)
	{
		return selectedInputHandler.GetAction(player);
	}

	public override void _Notification(int what)
	{
		if (what == NotificationPredelete)
		{
			SignalBus.Instance.CommandInputHandler -= SetInputHandler;
		}
		base._Notification(what);
	}

	/// <summary>
	/// Define o esquema de controle atual do jogo
	/// para o estado informado.
	/// </summary>
	/// <param name="inputhandler">Estado do jogo.</param>
	public void SetInputHandler(InputHandlers inputhandler)
	{
		selectedInputHandler?.Exit();
		selectedInputHandler = inputHandlerDict[inputhandler];
		selectedInputHandler.Enter();
	}
}