-
Notifications
You must be signed in to change notification settings - Fork 5
/
inventory.gd
337 lines (274 loc) · 9.69 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# EgoVenture Inventory system
extends Control
# Emitted, when another inventory item was triggered
signal triggered_inventory_item(first_item, second_item)
# Emitted when the player released an item
signal released_inventory_item(item)
# The currently selected inventory item or null
var selected_item: InventoryItemNode = null
# Wether the inventory is currently activated
var activated: bool = false
# Wether the inventory item was just released (to prevent other
# actions to be carried out)
var just_released: bool = false
# Wether to ignore a game pause
var ignore_pause: bool = false setget _set_ignore_pause
# The list of inventory items
var _inventory_items: Array
# The size to scroll around. Defaults to the inventory height
var _scroll_size: int
# The current width of all inventory items
var _items_width: int = 0
# Hide the activate and menu button on touch devices
func _ready():
if EgoVenture.is_touch:
$Canvas/InventoryAnchor/Panel/InventoryPanel/Reveal.show()
$Canvas/InventoryAnchor/Panel/InventoryPanel/Menu.show()
else:
$Canvas/InventoryAnchor/Panel/InventoryPanel/Reveal.hide()
$Canvas/InventoryAnchor/Panel/InventoryPanel/Menu.hide()
$Canvas/InventoryAnchor/Panel/InventoryPanel/ScrollContainer\
.get_h_scrollbar().rect_scale.x = 0
$Canvas/InventoryAnchor/Panel/InventoryPanel/ArrowLeft.hide()
$Canvas/InventoryAnchor/Panel/InventoryPanel/ArrowRight.hide()
# Reset just_released
func _process(_delta):
just_released = false
$Canvas/InventoryAnchor/Panel/InventoryPanel/ArrowLeft.visible = \
$Canvas/InventoryAnchor/Panel/InventoryPanel/ScrollContainer\
.scroll_horizontal > 0
var _scroll = $Canvas/InventoryAnchor/Panel/InventoryPanel/ScrollContainer
$Canvas/InventoryAnchor/Panel/InventoryPanel/ArrowRight.visible = \
_items_width > _scroll.get_rect().size.x
$Canvas/InventoryAnchor/Panel/InventoryPanel/ArrowRight.visible = \
_scroll.get_rect().size.x + _scroll.scroll_horizontal < \
_items_width
# Handle inventory drop events and border trigger for mouse
#
# ** Parameters **
#
# - event: Event received
func _input(event: InputEvent):
if not DetailView.is_visible:
# Drop the inventory item on RMB and two finger touch
if Inventory.selected_item != null and \
event is InputEventMouseButton and \
(event as InputEventMouseButton).button_index == BUTTON_RIGHT \
and not (event as InputEventMouseButton).pressed:
release_item()
just_released = true
elif Inventory.selected_item != null and \
event is InputEventScreenTouch and \
(event as InputEventScreenTouch).index == 2 and \
not (event as InputEventScreenTouch).pressed:
release_item()
just_released = true
elif ! EgoVenture.is_touch and event is InputEventMouseMotion and \
$Timer.is_stopped():
# Activate the inventory when reaching the upper screen border
if ! activated and get_viewport().get_mouse_position().y <= 10:
toggle_inventory()
# Deactivate the inventory when the mouse is below it
elif activated and \
get_viewport().get_mouse_position().y \
> $Canvas/InventoryAnchor/Panel.rect_size.y:
toggle_inventory()
# Configure the inventory. Should be call by a game core singleton
#
# ** Parameters **
#
# - configuration: The game configuration
func configure(configuration: GameConfiguration):
$Canvas/InventoryAnchor/Panel/InventoryPanel/Menu.texture_normal = \
configuration.inventory_texture_menu
$Canvas/InventoryAnchor/Panel/InventoryPanel/Notepad.texture_normal = \
configuration.inventory_texture_notepad
$Canvas/InventoryAnchor/Panel/InventoryPanel/ArrowLeft.texture_normal = \
configuration.inventory_texture_left_arrow
$Canvas/InventoryAnchor/Panel/InventoryPanel/ArrowRight.texture_normal = \
configuration.inventory_texture_right_arrow
$Canvas/InventoryAnchor.theme = configuration.design_theme
$Canvas/InventoryAnchor/Panel.rect_min_size.y = configuration.inventory_size
_scroll_size = configuration.inventory_size
if EgoVenture.is_touch:
$Canvas/InventoryAnchor/Panel.add_stylebox_override(
"panel",
$Canvas/InventoryAnchor/Panel.get_stylebox("inventory_panel_touch", "Panel")
)
else:
$Canvas/InventoryAnchor/Panel.add_stylebox_override(
"panel",
$Canvas/InventoryAnchor/Panel.get_stylebox("inventory_panel", "Panel")
)
$Canvas/InventoryAnchor/Panel/InventoryPanel/Reveal.texture_normal = \
configuration.inventory_texture_reveal
$Canvas/InventoryAnchor.margin_top = configuration.inventory_size * -1
var animation: Animation = $Animations.get_animation("Activate")
animation.track_set_key_value(
0,
0,
configuration.inventory_size * -1
)
if OS.has_touchscreen_ui_hint():
$Animations.play("Activate")
DetailView.get_node("Panel").theme = configuration.design_theme
# Disable the inventory system
func disable():
$Canvas/InventoryAnchor/Panel.hide()
# Enable the inventory system
func enable():
$Canvas/InventoryAnchor/Panel.show()
# Add an item to the inventory
#
# ** Parameters **
#
# - item: Item to add to the inventory
# - skip_show: Skip the reveal animation of the inventory bar
# - allow_duplicate: Allow to add an inventory item already in the inventory
func add_item(item: InventoryItem, skip_show: bool = false, allow_duplicate: bool = false):
if not allow_duplicate and has_item(item):
print(
"Item %s already is in the inventory. Rerufsing to add it twice" % \
item.title
)
return
var inventory_item_node = InventoryItemNode.new()
inventory_item_node.configure(item)
inventory_item_node.connect(
"triggered_inventory_item",
self,
"_on_triggered_inventory_item"
)
_inventory_items.append(inventory_item_node)
_update()
if not EgoVenture.is_touch and not activated and not skip_show:
# Briefly show the inventory when it is not activated
toggle_inventory()
$Timer.start()
yield($Timer,"timeout")
$Timer.stop()
toggle_inventory()
_items_width += inventory_item_node.get_rect().size.x
# Remove item from the inventory
#
# ** Parameters **
#
# - item: Item to remove from the inventory
func remove_item(item: InventoryItem):
var found_index = -1
for index in range(_inventory_items.size()):
if (_inventory_items[index] as InventoryItemNode).item == item:
found_index = index
if found_index != -1:
if selected_item == _inventory_items[found_index]:
release_item()
_items_width -= _inventory_items[found_index].get_rect().size.x
_inventory_items.remove(found_index)
_update()
# Release the currently selected item
func release_item():
emit_signal("released_inventory_item", selected_item.item)
selected_item.texture_normal = \
(selected_item.item as InventoryItem).image_normal
selected_item.modulate.a = 1
selected_item = null
if not EgoVenture.is_touch:
Cursors.reset(Cursors.Type.DEFAULT)
Speedy.keep_shape = false
# Returns the current list of inventory items
func get_items() -> Array:
var items = []
for item in _inventory_items:
items.append(item.item)
return items
# Check, wether the player carries a specific item
#
# ** Parameters **
#
# - needle: item searched for
#
# - returns: true if the player is carrying the item, false if not.
func has_item(needle: InventoryItem) -> bool:
for item in _inventory_items:
if item.item == needle:
return true
return false
# Show or hide the inventory
func toggle_inventory():
if activated:
$Animations.play_backwards("Activate")
activated = false
else:
$Animations.play("Activate")
activated = true
# Emit signal, that the notepad was pressed
func _on_Notepad_pressed():
if selected_item == null:
Notepad.show()
# Emit signal, that the menu was pressed
func _on_Menu_pressed():
MainMenu.toggle()
# Emit a signal, that one item was triggered on another item
#
# ** Parameters **
#
# - first_item: First item that is used
# - second_item: Second item that is used
func _on_triggered_inventory_item(
first_item: InventoryItem,
second_item: InventoryItem
):
emit_signal("triggered_inventory_item", first_item, second_item)
# Update the inventory item view by simply removing all items and re-adding them
func _update():
var inventory_panel = \
$Canvas/InventoryAnchor/Panel/InventoryPanel\
/ScrollContainer/Inventory
for child in inventory_panel.get_children():
inventory_panel.remove_child(child)
for item in _inventory_items:
inventory_panel.add_child(item)
# React to touches on the reveal button
#
# ** Parameters **
#
# - event: event that was triggered
func _on_Reveal_gui_input(event):
if event is InputEventScreenTouch:
if Inventory.selected_item == null:
if (event as InputEventScreenTouch).pressed:
var push_event = InputEventAction.new()
push_event.pressed = true
push_event.action = "hotspot_indicator"
Input.parse_input_event(push_event)
else:
var release_event = InputEventAction.new()
release_event.pressed = false
release_event.action = "hotspot_indicator"
Input.parse_input_event(release_event)
elif not (event as InputEventScreenTouch).pressed:
if DetailView.is_visible:
DetailView.hide()
else:
DetailView.show_with_item(Inventory.selected_item.item)
# Wether to ignore game pauses
#
# ** Parameters **
#
# - value: Wether to ignore game pauses or not
func _set_ignore_pause(value: bool):
ignore_pause = value
if ignore_pause:
$Canvas/InventoryAnchor.pause_mode = Node.PAUSE_MODE_PROCESS
pause_mode = Node.PAUSE_MODE_PROCESS
else:
$Canvas/InventoryAnchor.pause_mode = Node.PAUSE_MODE_STOP
pause_mode = Node.PAUSE_MODE_STOP
# Handle moving the inventory to the right
func _on_ArrowRight_pressed() -> void:
var _scroll = $Canvas/InventoryAnchor/Panel/InventoryPanel/ScrollContainer
_scroll.scroll_horizontal += _scroll_size
# Handle moving the inventory to the left
func _on_ArrowLeft_pressed() -> void:
$Canvas/InventoryAnchor/Panel/InventoryPanel/ScrollContainer\
.scroll_horizontal -= _scroll_size