-
Notifications
You must be signed in to change notification settings - Fork 4
/
UtilsInventory.cs
174 lines (136 loc) · 3.73 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
namespace Inventory;
public static class UtilsInventory
{
public static void HandleInput(Node node, InputEvent @event)
{
InputGame.Handle(@event);
if (Input.IsActionJustPressed("interact"))
{
Inventory.ActiveChest?.Open();
}
if (Input.IsActionJustPressed("inventory"))
{
if (!Player.Inventory.IsHotbar)
{
Player.Inventory.SwitchToHotbarAnimated();
}
else
{
Player.Inventory.SwitchToFullInventoryAnimated();
}
}
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}"))
return;
var cursorItem = ItemCursor.GetItem();
if (cursorItem == null)
HotbarInvSlot(hotbar);
else
HotbarCursor(hotbar, cursorItem);
}
private static void HotbarCursor(int hotbar, Item cursorItem)
{
var playerInv = Player.Inventory;
var playerInvSlots = playerInv.InventorySlots;
var columns = playerInv.Columns;
if (columns <= hotbar)
return;
var hotbarSlot = playerInvSlots[playerInvSlots.Length - columns + hotbar];
ItemPanelDescription.Clear();
if (hotbarSlot.InventoryItem == null)
{
// Just move the item over
hotbarSlot.SetItem(cursorItem);
ItemCursor.RemoveItem();
}
else
{
if (hotbarSlot.InventoryItem.Item.Type == cursorItem.Type)
{
// Same type of item
// Add the item counts together
cursorItem.Count += hotbarSlot.InventoryItem.Item.Count;
// Just move the item over
hotbarSlot.SetItem(cursorItem);
ItemCursor.RemoveItem();
}
else
{
// Different type of item
// Swap the items
var hotbarItem = hotbarSlot.InventoryItem.Item;
hotbarSlot.SetItem(cursorItem);
ItemCursor.SetItem(hotbarItem);
}
}
}
private static void HotbarInvSlot(int hotbar)
{
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);
}
}
}
}