Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NancokPS2 committed Feb 25, 2024
1 parent 3c7d326 commit 9e0006f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion classes/Item.gd
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func server_use(player: Player) -> bool:
if amount <= 1:
player.inventory.server_remove_item(uuid)
else:
player.inventory.server_change_item_amount(uuid, amount - 1)
player.inventory.server_set_item_amount(uuid, amount - 1)
return true

ITEM_TYPE.EQUIPMENT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,29 @@ func server_add_item(item: Item) -> bool:
return false

#Try to stack
var existing_item: Item = get_item_by_class(item.item_class)
if existing_item is Item and existing_item.amount < existing_item.amount_max:
var existing_item: Item = null

#Look for an item that has available space.
for i_item in get_items_by_class(item.item_class):
if i_item.amount < i_item.amount_max:
existing_item = i_item
break

#If found, start the stacking.
if existing_item is Item:
var remaining_space: int = existing_item.amount_max - existing_item.amount
var amount_to_add: int = min(item.amount, remaining_space)
var surplus: int = max(0, item.amount - remaining_space)

#If there's space remaining, add some of this item's to the stack.
#This is delegated to the server_change_item_amount() function which synchronizes amounts by itself.
#If there's space remaining, add some of this item's amount to the existing one.
#And remove it from the former.
#This is delegated to the server_set_item_amount() function which synchronizes amounts by itself.
if remaining_space > 0:
server_change_item_amount(existing_item.uuid, existing_item.amount + amount_to_add)

server_change_item_amount(item.uuid, item.amount - amount_to_add)
server_set_item_amount(item.uuid, item.amount - amount_to_add)
server_set_item_amount(existing_item.uuid, existing_item.amount + amount_to_add)

#Any remaining amount is added as a separate item
if item.amount > 0:
if surplus > 0:
server_add_item(item)
_inventory_synchronizer_rpc.add_item(
_target_node.peer_id, item.name, item.item_class, item.amount
Expand Down Expand Up @@ -146,14 +155,16 @@ func client_remove_item(item_uuid: String):
item_removed.emit(item_uuid)


func server_change_item_amount(item_uuid: String, amount: int):
func server_set_item_amount(item_uuid: String, amount: int):
if not _target_node.multiplayer_connection.is_server():
return false

var item: Item = get_item(item_uuid)
if item != null:
item.amount = amount
_inventory_synchronizer_rpc.change_item_amount(_target_node.peer_id, item_uuid, amount)
else:
GodotLogger.error("This item does not exist!?")


func client_change_item_amount(item_uuid: String, amount: int):
Expand All @@ -173,13 +184,12 @@ func get_item(item_uuid: String) -> Item:


## Returns the first instance of an item of this class
func get_item_by_class(item_class: String) -> Item:
func get_items_by_class(item_class: String) -> Array[Item]:
var output: Array[Item] = []
for item: Item in items:
if item.item_class == item_class:
return item

GodotLogger.warn("Could not find item of class '{0}'.".format([item_class]))
return null
output.append(item)
return output


func server_use_item(item_uuid: String):
Expand Down
1 change: 0 additions & 1 deletion scenes/player/inventory/Inventory.gd
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func _input(event):
hide()
else:
update_inventory()
inventory_synchronizer.sync_invendtory.rpc_id(1)
show()


Expand Down

0 comments on commit 9e0006f

Please sign in to comment.