summaryrefslogtreecommitdiff
path: root/scripts/InputHandling/InventoryInputHandler.cs
blob: 390e5451c57ffe83b80bbf92bf90a7db6c1befec (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
using Godot;
using TheLegendOfGustav.GUI;
using TheLegendOfGustav.Entities.Actors;
using TheLegendOfGustav.Entities.Items;
using TheLegendOfGustav.Entities.Actions;

namespace TheLegendOfGustav.InputHandling;

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


	[Export]
	private Map.Map Map { get; set; }

	private InventoryMenu InventoryMenu { get; set; }
	private ConsumableItem ActivationItem { get; set; } = null;
	private ConsumableItem DropItem { get; set; } = null;

	public override void Enter()
	{
		InventoryMenu = inventoryScene.Instantiate<InventoryMenu>();
		Map.MapData.Player.AddChild(InventoryMenu);
		InventoryMenu.Initialize(Map.MapData.Player.Inventory);
		InventoryMenu.ItemSelected += OnItemActivate;
		InventoryMenu.ItemDrop += OnItemDrop;
	}

	public override void Exit()
	{
		ActivationItem = null;
		DropItem = null;
		InventoryMenu.QueueFree();
	}

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

		if (ActivationItem != null)
		{
			action = new ItemAction(player, ActivationItem);
			Close();
		}

		if (DropItem != null)
		{
			action = new DropAction(player, DropItem);
			Close();
		}

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

		return action;
	}

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

	private void OnItemActivate(ConsumableItem item)
	{
		ActivationItem = item;
	}

	private void OnItemDrop(ConsumableItem item)
	{
		DropItem = item;
	}
}