Skip to content

Commit 1d64a6f

Browse files
committed
Final touches
1 parent 513e2a4 commit 1d64a6f

File tree

5 files changed

+76
-60
lines changed

5 files changed

+76
-60
lines changed

scenes/player/inventory/Inventory.gd

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ func get_panel_at_pos(pos: Vector2) -> InventoryPanel:
7070

7171
func get_panels(occupied_only: bool) -> Array[InventoryPanel]:
7272
var output: Array[InventoryPanel] = []
73-
var temp_arr: Array = []
74-
73+
var temp_arr: Array = []
74+
7575
if occupied_only:
7676
for row: Array in panels:
7777
for panel: InventoryPanel in row:
@@ -80,7 +80,7 @@ func get_panels(occupied_only: bool) -> Array[InventoryPanel]:
8080
else:
8181
for row: Array in panels:
8282
temp_arr += row
83-
83+
8484
output.assign(temp_arr)
8585
return output
8686

@@ -96,41 +96,39 @@ func update_inventory():
9696
var occupied_panels: Array[InventoryPanel] = get_panels(true)
9797
var panel_item_uuid_dictionary: Dictionary = {}
9898
var panels_with_invalid_items: Dictionary = {}
99-
99+
100100
#Ensure all panels have an item
101101
assert(
102-
occupied_panels.all(
103-
func(panel_occupied: InventoryPanel):
104-
return panel_occupied.item is Item
105-
)
102+
occupied_panels.all(
103+
func(panel_occupied: InventoryPanel): return panel_occupied.item is Item
106104
)
107-
105+
)
106+
108107
#Store the uuid of the item that each panel has to accelerate the rest of the update.
109108
for panel: InventoryPanel in occupied_panels:
110109
panel_item_uuid_dictionary[panel.item.uuid] = panel
111-
110+
112111
#All panels are assumed invalid until proven otherwise.
113112
panels_with_invalid_items = panel_item_uuid_dictionary.duplicate()
114-
113+
115114
#Check every inventory item
116115
for inv_item: Item in player.inventory.items:
117-
118116
#Unmark as invalid those who have been found in the inventory
119117
panels_with_invalid_items.erase(inv_item.uuid)
120-
118+
121119
#No item with this uuid has been found in the displayed inventory, add it.
122120
if not inv_item.uuid in panel_item_uuid_dictionary.keys():
123121
place_item_at_free_slot(inv_item)
124-
122+
125123
else:
126124
#Item found, update it.
127125
panel_item_uuid_dictionary[inv_item.uuid].item = inv_item
128126
panel_item_uuid_dictionary[inv_item.uuid].queue_redraw()
129-
127+
130128
#Clear any panels with items that are NOT in the inventory
131129
for item_uuid: String in panels_with_invalid_items:
132130
panels_with_invalid_items[item_uuid].item = null
133-
131+
134132

135133
func place_item_at_free_slot(item: Item) -> bool:
136134
for y in range(SIZE.y):

scenes/player/inventory/InventoryPanel.gd

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@ const DEFAULT_FONT: Font = preload("res://addons/gut/fonts/LobsterTwo-Regular.tt
77
@export var item: Item:
88
set(new_item):
99
if item is Item and item.amount_changed.is_connected(on_item_amount_changed):
10-
item.amount_changed.disconnect(on_item_amount_changed)
11-
10+
item.amount_changed.disconnect(on_item_amount_changed)
11+
1212
item = new_item
13-
13+
1414
if item is Item:
1515
if not item.amount_changed.is_connected(on_item_amount_changed):
16-
item.amount_changed.connect(on_item_amount_changed)
16+
item.amount_changed.connect(on_item_amount_changed)
1717
$TextureRect.texture = item.get_node("Icon").texture
1818
else:
19-
2019
$TextureRect.texture = null
21-
20+
2221
queue_redraw()
2322

2423
@onready var inventory: Inventory = $"../.."
@@ -34,14 +33,18 @@ func _ready():
3433
mouse_exited.connect(_on_mouse_exited)
3534

3635

37-
38-
39-
4036
func _draw():
41-
if item is Item:
37+
if item is Item and item.amount != 1:
4238
var font_height: int = 14
43-
draw_string(DEFAULT_FONT, Vector2(0, size.y - font_height), str(item.amount),HORIZONTAL_ALIGNMENT_CENTER, -1, font_height)
44-
39+
draw_string(
40+
DEFAULT_FONT,
41+
Vector2(0, size.y - font_height),
42+
str(item.amount),
43+
HORIZONTAL_ALIGNMENT_CENTER,
44+
-1,
45+
font_height
46+
)
47+
4548

4649
func _gui_input(event: InputEvent):
4750
if event.is_action_pressed("j_left_click"):
@@ -69,7 +72,7 @@ func _gui_input(event: InputEvent):
6972
drag_panel.hide()
7073

7174

72-
func _physics_process(_delta):
75+
func _physics_process(_delta: float):
7376
if selected:
7477
drag_panel.position = get_local_mouse_position() + drag_panel_offset
7578

@@ -82,5 +85,5 @@ func _on_mouse_exited():
8285
inventory.mouse_above_this_panel = null
8386

8487

85-
func on_item_amount_changed(item: Item, amount: int):
88+
func on_item_amount_changed(_amount: int):
8689
queue_redraw()

scripts/classes/Item.gd

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ extends StaticBody2D
22

33
class_name Item
44

5-
signal amount_changed(this: Item, new_amount: int)
5+
signal amount_changed(new_amount: int)
66

77
enum MODE { LOOT, ITEMSLOT }
88

@@ -28,8 +28,8 @@ var amount_max: int = 1
2828
var amount: int = 1:
2929
set(val):
3030
amount = val
31+
amount_changed.emit(amount)
3132

32-
amount_changed.emit(self, amount)
3333
var price: int = 0
3434
var value: int = 0
3535

@@ -96,10 +96,18 @@ func use(player: Player) -> bool:
9696
ITEM_TYPE.CONSUMABLE:
9797
if boost.hp > 0:
9898
player.stats.heal(self.name, boost.hp)
99+
100+
if amount <= 1:
101+
player.inventory.remove_item(uuid)
102+
else:
103+
player.inventory.set_item_amount(uuid, amount - 1)
99104
return true
105+
100106
ITEM_TYPE.EQUIPMENT:
101107
if player.equipment and player.equipment.equip_item(self):
108+
player.inventory.remove_item(uuid)
102109
return true
110+
103111
else:
104112
GodotLogger.info("%s could not equip item %s" % [player.name, item_class])
105113
return false

scripts/components/player/equipmentsynchronizercomponent/EquipmentSynchronizerComponent.gd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ signal item_removed(item_uuid: String)
1010

1111
var target_node: Node
1212

13-
var items = {
13+
var items: Dictionary = {
1414
"Head": null,
1515
"Body": null,
1616
"Legs": null,
@@ -99,7 +99,7 @@ func _unequip_item(item_uuid: String) -> Item:
9999

100100

101101
func get_item(item_uuid: String) -> Item:
102-
for equipment_slot in items:
102+
for equipment_slot: String in items:
103103
var item: Item = items[equipment_slot]
104104
if item != null and item.uuid == item_uuid:
105105
return item
@@ -111,7 +111,7 @@ func get_boost() -> Boost:
111111
var boost: Boost = Boost.new()
112112
boost.identifier = "equipment"
113113

114-
for equipment_slot in items:
114+
for equipment_slot: String in items:
115115
var item: Item = items[equipment_slot]
116116
if item != null:
117117
boost.combine_boost(item.boost)
@@ -122,7 +122,7 @@ func get_boost() -> Boost:
122122
func to_json() -> Dictionary:
123123
var output: Dictionary = {}
124124

125-
for slot in items:
125+
for slot: String in items:
126126
if items[slot] != null:
127127
var item: Item = items[slot]
128128
output[slot] = item.to_json()
@@ -131,7 +131,7 @@ func to_json() -> Dictionary:
131131

132132

133133
func from_json(data: Dictionary) -> bool:
134-
for slot in data:
134+
for slot: String in data:
135135
if not slot in items:
136136
GodotLogger.warn("Slot=[%s] does not exist in equipment items" % slot)
137137
return false

scripts/components/player/inventorysynchronizercomponent/InventorySynchronizerComponent.gd

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,37 +34,37 @@ func _ready():
3434
func add_item(item: Item) -> bool:
3535
if not G.is_server():
3636
return false
37-
37+
3838
#Currency is directly added to gold
3939
if item.item_type == Item.ITEM_TYPE.CURRENCY:
4040
add_gold(item.amount)
4141
return true
4242

4343
item.collision_layer = 0
44-
44+
4545
#Inventory full
4646
if items.size() >= size:
4747
return false
48-
48+
4949
#Try to stack
5050
var existing_item: Item = get_item_by_class(item.item_class)
5151
if existing_item is Item and existing_item.amount < existing_item.amount_max:
5252
var remaining_space: int = existing_item.amount_max - existing_item.amount
5353
var amount_to_add: int = min(item.amount, remaining_space)
54-
54+
5555
#If there's space remaining, add some of this item's to the stack.
56-
if remaining_space > 0:
56+
if remaining_space > 0:
5757
existing_item.amount += amount_to_add
58-
58+
5959
item.amount -= amount_to_add
60-
60+
6161
if item.amount > 0:
6262
add_item(item)
63-
63+
6464
sync_item_amount.rpc_id(target_node.peer_id, item.uuid, item.amount)
65-
65+
6666
return true
67-
67+
6868
#Adding the item from scratch
6969
else:
7070
items.append(item)
@@ -80,27 +80,33 @@ func get_item_by_class(item_class: String) -> Item:
8080
if item.item_class == item_class:
8181
return item
8282

83-
8483
GodotLogger.warn("Could not find item of class '{0}'.".format([item_class]))
8584
return null
8685

8786

88-
func remove_item(item_uuid: String, amount: int = 1):
87+
func remove_item(item_uuid: String):
8988
if not G.is_server():
9089
return false
9190

9291
var item: Item = get_item(item_uuid)
93-
92+
9493
if item != null:
95-
item.amount -= amount
96-
if item.amount <= 0:
97-
items.erase(item)
98-
sync_remove_item.rpc_id(target_node.peer_id, item_uuid)
99-
else:
100-
sync_item_amount.rpc_id(target_node.peer_id, item.uuid, item.amount)
94+
items.erase(item)
95+
sync_remove_item.rpc_id(target_node.peer_id, item_uuid)
10196
return item
10297

10398

99+
func set_item_amount(item_uuid: String, new_amount: int):
100+
if not G.is_server():
101+
return false
102+
103+
var item: Item = get_item(item_uuid)
104+
105+
if item != null:
106+
item.amount = new_amount
107+
sync_item_amount.rpc_id(target_node.peer_id, item_uuid, new_amount)
108+
109+
104110
func get_item(item_uuid: String) -> Item:
105111
for item in items:
106112
if item.uuid == item_uuid:
@@ -109,13 +115,12 @@ func get_item(item_uuid: String) -> Item:
109115
return null
110116

111117

112-
func use_item(item_uuid: String):
118+
func use_item(item_uuid: String) -> bool:
113119
if not G.is_server():
114120
return false
115121

116122
var item: Item = get_item(item_uuid)
117123
if item and item.use(target_node):
118-
remove_item(item_uuid)
119124
return true
120125

121126
return false
@@ -211,7 +216,7 @@ func sync_response(inventory: Dictionary):
211216

212217
@rpc("call_remote", "authority", "reliable")
213218
func sync_add_item(item_uuid: String, item_class: String, amount: int):
214-
var item = J.item_scenes[item_class].instantiate()
219+
var item: Item = J.item_scenes[item_class].instantiate()
215220
item.uuid = item_uuid
216221
item.item_class = item_class
217222
item.amount = amount
@@ -235,7 +240,9 @@ func sync_remove_item(item_uuid: String):
235240
@rpc("call_remote", "authority", "reliable")
236241
func sync_item_amount(item_uuid: String, new_amount: int):
237242
var item: Item = get_item(item_uuid)
238-
item.amount = new_amount
243+
244+
if item is Item:
245+
item.amount = new_amount
239246

240247

241248
@rpc("call_remote", "authority", "reliable")

0 commit comments

Comments
 (0)