Browse Source

Fixing adding of items to inventory

pull/24/head
Ryan Reed 3 weeks ago
parent
commit
7460b8a5b2
2 changed files with 27 additions and 14 deletions
  1. +22
    -12
      autoloads/inventory_manager.gd
  2. +5
    -2
      scenes/ui/inventory/inventory.gd

+ 22
- 12
autoloads/inventory_manager.gd View File

@ -37,7 +37,7 @@ func _unhandled_input(event: InputEvent) -> void:
add_to_inventory.emit("003", 1) add_to_inventory.emit("003", 1)
func _find_first_stack(item_resource: DBItemResource, item_id: String) -> bool:
func _find_first_available_stack(item_resource: DBItemResource, item_id: String) -> bool:
return ( return (
item_resource.id == item_id and item_resource.id == item_id and
item_resource.amount < item_resource.max_stack_size item_resource.amount < item_resource.max_stack_size
@ -45,33 +45,43 @@ func _find_first_stack(item_resource: DBItemResource, item_id: String) -> bool:
func _on_add_to_inventory(item_id: String, amount: int = 1) -> void: func _on_add_to_inventory(item_id: String, amount: int = 1) -> void:
if not DBItems.data.get(item_id):
printerr("Cannot add item, ", item_id, ", to inventory. Could not find item within DBItems.data.")
return
if amount < 1:
printerr("Cannot add item, ", item_id, ", to inventory. Amount to add cannot be less then 1.")
return
var item_resource: DBItemResource = DBItems.data[item_id] var item_resource: DBItemResource = DBItems.data[item_id]
# The logic below is a mess and needs to be reworked
# This logic seems overly complicated and is a mess.
# Should look into fixing/simplifying this in the future
var amount_remaining: int = amount var amount_remaining: int = amount
while amount_remaining > 0: while amount_remaining > 0:
var first_stack_index: int = inventory.find_custom(_find_first_stack.bind(item_id))
var first_stack_index: int = inventory.find_custom(_find_first_available_stack.bind(item_id))
if first_stack_index == -1 and inventory.size() < max_inventory_items: if first_stack_index == -1 and inventory.size() < max_inventory_items:
inventory.append(item_resource)
var stack_resource: DBItemResource = item_resource.duplicate()
inventory.append(stack_resource)
if amount_remaining <= item_resource.max_stack_size:
if amount_remaining <= stack_resource.max_stack_size:
inventory[-1].amount += amount_remaining inventory[-1].amount += amount_remaining
amount_remaining = 0 amount_remaining = 0
else: else:
inventory[-1].amount = item_resource.max_stack_size
amount_remaining -= item_resource.max_stack_size
inventory[-1].amount = stack_resource.max_stack_size
amount_remaining -= stack_resource.max_stack_size
item_added.emit(item_id, amount - amount_remaining) item_added.emit(item_id, amount - amount_remaining)
elif first_stack_index > -1: elif first_stack_index > -1:
var current_amount: int = inventory[first_stack_index].amount
var stack_resource: DBItemResource = inventory[first_stack_index]
var current_amount: int = stack_resource.amount
var total_amount: int = current_amount + amount_remaining var total_amount: int = current_amount + amount_remaining
if item_resource.max_stack_size - total_amount < 0:
inventory[first_stack_index].amount += amount_remaining
if total_amount < stack_resource.max_stack_size: # Stack not full and can hold
inventory[first_stack_index].amount = total_amount
amount_remaining = 0 amount_remaining = 0
else: else:
inventory[first_stack_index].amount = item_resource.max_stack_size
amount_remaining -= item_resource.max_stack_size
inventory[first_stack_index].amount = stack_resource.max_stack_size
amount_remaining -= stack_resource.max_stack_size
item_added.emit(item_id, amount - amount_remaining) item_added.emit(item_id, amount - amount_remaining)


+ 5
- 2
scenes/ui/inventory/inventory.gd View File

@ -47,7 +47,7 @@ func refresh_inventory_grid() -> void:
for item: DBItemResource in InventoryManager.inventory: for item: DBItemResource in InventoryManager.inventory:
create_item_cell(item) create_item_cell(item)
var empty_cells: int = InventoryManager.max_inventory_items - inventory_resource.inventory.size()
var empty_cells: int = InventoryManager.max_inventory_items - InventoryManager.inventory.size()
for _i: int in range(empty_cells): for _i: int in range(empty_cells):
create_item_cell(null) create_item_cell(null)
@ -63,8 +63,11 @@ func toggle_inventory() -> void:
InventoryManager.inventory_closed.emit() InventoryManager.inventory_closed.emit()
get_tree().paused = false get_tree().paused = false
## Add any items from the existing inventory resource
func update_inventory_with_resource() -> void: func update_inventory_with_resource() -> void:
# Add any items from the existing inventory resource
if inventory_resource == null:
return
for item_resource: DBItemResource in inventory_resource.inventory: for item_resource: DBItemResource in inventory_resource.inventory:
InventoryManager.add_to_inventory.emit(item_resource.id, item_resource.amount) InventoryManager.add_to_inventory.emit(item_resource.id, item_resource.amount)


Loading…
Cancel
Save