-
Notifications
You must be signed in to change notification settings - Fork 11
/
Inventory.gd
210 lines (191 loc) · 6.06 KB
/
Inventory.gd
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
extends Panel;
const ItemClass = preload("res://Item.gd");
const ItemSlotClass = preload("res://ItemSlot.gd");
const TooltipClass = preload("res://Tooltip.gd");
const MAX_SLOTS = 45;
const itemImages = [
preload("res://images/Ac_Ring05.png"),
preload("res://images/A_Armor05.png"),
preload("res://images/A_Armour02.png"),
preload("res://images/A_Shoes03.png"),
preload("res://images/C_Elm03.png"),
preload("res://images/E_Wood02.png"),
preload("res://images/W_Sword001.png"),
preload("res://images/Ac_Necklace03.png"),
];
const itemDictionary = {
0: {
"itemName": "Ring",
"itemValue": 456,
"itemIcon": itemImages[0],
"slotType": Global.SlotType.SLOT_RING
},
1: {
"itemName": "Sword",
"itemValue": 832,
"itemIcon": itemImages[6],
"slotType": Global.SlotType.SLOT_LHAND
},
2: {
"itemName": "Armor",
"itemValue": 623,
"itemIcon": itemImages[2],
"slotType": Global.SlotType.SLOT_ARMOR
},
3: {
"itemName": "Helmet",
"itemValue": 12,
"itemIcon": itemImages[4],
"slotType": Global.SlotType.SLOT_HELMET
},
4: {
"itemName": "Boots",
"itemValue": 654,
"itemIcon": itemImages[3],
"slotType": Global.SlotType.SLOT_FEET
},
5: {
"itemName": "Shield",
"itemValue": 23,
"itemIcon": itemImages[5],
"slotType": Global.SlotType.SLOT_RHAND
},
6: {
"itemName": "Necklace",
"itemValue": 756,
"itemIcon": itemImages[7],
"slotType": Global.SlotType.SLOT_NECK
}
};
var slotList = Array();
var holdingItem = null;
var itemOffset = Vector2(0, 0);
onready var tooltip = get_node("../Tooltip");
onready var characterPanel = get_node("../CharacterPanel");
func _ready():
var slots = get_node("SlotsContainer/Slots");
for _i in range(MAX_SLOTS):
var slot = ItemSlotClass.new();
slot.connect("mouse_entered", self, "mouse_enter_slot", [slot]);
slot.connect("mouse_exited", self, "mouse_exit_slot", [slot]);
slot.connect("gui_input", self, "slot_gui_input", [slot]);
slotList.append(slot);
slots.add_child(slot);
for i in range(10):
if i == 0:
continue;
var panelSlot = characterPanel.slots[i];
if panelSlot:
panelSlot.connect("mouse_entered", self, "mouse_enter_slot", [panelSlot]);
panelSlot.connect("mouse_exited", self, "mouse_exit_slot", [panelSlot]);
panelSlot.connect("gui_input", self, "slot_gui_input", [panelSlot]);
func mouse_enter_slot(_slot : ItemSlotClass):
if _slot.item:
tooltip.display(_slot.item, get_global_mouse_position());
func mouse_exit_slot(_slot : ItemSlotClass):
if tooltip.visible:
tooltip.hide();
func slot_gui_input(event : InputEvent, slot : ItemSlotClass):
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT && event.pressed:
if holdingItem:
if slot.slotType != Global.SlotType.SLOT_DEFAULT:
if canEquip(holdingItem, slot):
if !slot.item:
slot.equipItem(holdingItem, false);
holdingItem = null;
else:
var tempItem = slot.item;
slot.pickItem();
tempItem.rect_global_position = event.global_position - itemOffset;
slot.equipItem(holdingItem, false);
holdingItem = tempItem;
elif slot.item:
var tempItem = slot.item;
slot.pickItem();
tempItem.rect_global_position = event.global_position - itemOffset;
slot.putItem(holdingItem);
holdingItem = tempItem;
else:
slot.putItem(holdingItem);
holdingItem = null;
elif slot.item:
holdingItem = slot.item;
itemOffset = event.global_position - holdingItem.rect_global_position;
slot.pickItem();
holdingItem.rect_global_position = event.global_position - itemOffset;
elif event.button_index == BUTTON_RIGHT && !event.pressed:
if slot.slotType != Global.SlotType.SLOT_DEFAULT:
if slot.item:
var freeSlot = getFreeSlot();
if freeSlot:
var item = slot.item;
slot.removeItem();
freeSlot.setItem(item);
else:
if slot.item:
var itemSlotType = slot.item.slotType;
var panelSlot = characterPanel.getSlotByType(slot.item.slotType);
if itemSlotType == Global.SlotType.SLOT_RING:
if panelSlot[0].item && panelSlot[1].item:
var panelItem = panelSlot[0].item;
panelSlot[0].removeItem();
var slotItem = slot.item;
slot.removeItem();
slot.setItem(panelItem);
panelSlot[0].setItem(slotItem);
pass
elif !panelSlot[0].item && panelSlot[1].item || !panelSlot[0].item && !panelSlot[1].item:
var tempItem = slot.item;
slot.removeItem();
panelSlot[0].equipItem(tempItem);
elif panelSlot[0].item && !panelSlot[1].item:
var tempItem = slot.item;
slot.removeItem();
panelSlot[1].equipItem(tempItem);
pass
else:
if panelSlot.item:
var panelItem = panelSlot.item;
panelSlot.removeItem();
var slotItem = slot.item;
slot.removeItem();
slot.setItem(panelItem);
panelSlot.setItem(slotItem);
else:
var tempItem = slot.item;
slot.removeItem();
panelSlot.equipItem(tempItem);
func _input(event : InputEvent):
if holdingItem && holdingItem.picked:
holdingItem.rect_global_position = event.global_position - itemOffset;
func getFreeSlot():
for slot in slotList:
if !slot.item:
return slot;
func canEquip(item, slot):
var ring = Global.SlotType.SLOT_RING;
var ring2 = Global.SlotType.SLOT_RING2;
return item.slotType == slot.slotType || item.slotType == ring && (slot.slotType == ring || slot.slotType == ring2);
func _on_SortRarityButton_pressed():
var items = Array();
for slot in slotList:
if slot.item:
items.append(slot.item);
slot.removeItem();
items.sort_custom(self, "sortItemsByRarity");
for i in range(items.size()):
var item = items[i];
var slot = slotList[i];
slot.setItem(item);
func sortItemsByRarity(itemA : ItemClass, itemB : ItemClass):
return itemA.rarity > itemB.rarity;
func _on_AddItemButton_pressed():
var slot = getFreeSlot();
if slot:
var item = itemDictionary[randi() % itemDictionary.size()];
var itemName = item.itemName;
var itemIcon = item.itemIcon;
var itemValue = item.itemValue;
var slotType = item.slotType;
slot.setItem(ItemClass.new(itemName, itemIcon, null, itemValue, slotType));