-
Notifications
You must be signed in to change notification settings - Fork 4
/
UtilsInventory.cs
123 lines (95 loc) · 2.7 KB
/
UtilsInventory.cs
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
namespace Inventory;
public static class UtilsInventory
{
private static bool PlayerInventoryVisible { get; set; }
public static void HandleInput(InputEvent @event)
{
InputGame.Handle(@event);
if (Input.IsActionJustPressed("interact"))
{
Inventory.ActiveChest?.Open();
}
if (Input.IsActionJustPressed("inventory"))
{
if (PlayerInventoryVisible)
{
Player.Inventory.SwitchToHotbar();
}
else
{
Player.Inventory.SwitchToFullInventory();
}
PlayerInventoryVisible = !PlayerInventoryVisible;
}
if (Input.IsActionJustPressed("inventory_take_all"))
{
ItemPanelDescription.Clear();
Inventory.OtherInventory?.TakeAll();
}
if (Input.IsActionJustPressed("inventory_sort"))
{
ItemPanelDescription.Clear();
Inventory.OtherInventory?.Sort();
Player.Inventory.Sort();
}
for (int i = 0; i < Player.Inventory.Columns; i++)
InputHotbar(i);
// DEBUG
var debugInv = Inventory.OtherInventory;
if (debugInv == null)
return;
if (Input.IsKeyPressed(Key.F1))
for (int i = 0; i < debugInv.InventorySlots.Length; i++)
debugInv.InventorySlots[i].SetDebugLabel(debugInv.InventorySlots[i].InventoryItem == null ? "null" : "item" + "");
if (Input.IsKeyPressed(Key.F2))
for (int i = 0; i < debugInv.InventorySlots.Length; i++)
debugInv.InventorySlots[i].SetDebugLabel("");
}
private static void InputHotbar(int hotbar)
{
if (Input.IsActionJustPressed($"inventory_hotbar_{hotbar + 1}"))
{
var activeInvSlot = Inventory.ActiveInventorySlot;
if (activeInvSlot == null)
return;
var activeInvSlotItem = activeInvSlot.InventoryItem;
if (activeInvSlotItem == null)
return;
var playerInv = Player.Inventory;
var columns = playerInv.Columns;
var playerInvSlots = playerInv.InventorySlots;
if (columns <= hotbar)
return;
var hotbarSlot = playerInvSlots[playerInvSlots.Length - columns + hotbar];
if (activeInvSlot == hotbarSlot)
return;
ItemPanelDescription.Clear();
if (hotbarSlot.InventoryItem == null)
{
// Just move the item over
hotbarSlot.SetItem(activeInvSlotItem.Item);
activeInvSlot.RemoveItem();
}
else
{
if (hotbarSlot.InventoryItem.Item.Type == activeInvSlotItem.Item.Type)
{
// Same type of item
// Add the item counts together
activeInvSlotItem.Item.Count += hotbarSlot.InventoryItem.Item.Count;
// Just move the item over
hotbarSlot.SetItem(activeInvSlotItem.Item);
activeInvSlot.RemoveItem();
}
else
{
// Different type of item
// Swap the items
var hotbarItem = hotbarSlot.InventoryItem.Item;
hotbarSlot.SetItem(activeInvSlotItem.Item);
activeInvSlot.SetItem(hotbarItem);
}
}
}
}
}