diff --git a/autoloads/inventory_manager.gd b/autoloads/inventory_manager.gd index c539b00..a18029b 100644 --- a/autoloads/inventory_manager.gd +++ b/autoloads/inventory_manager.gd @@ -37,7 +37,7 @@ func _unhandled_input(event: InputEvent) -> void: 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 ( item_resource.id == item_id and 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: + 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] - # 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 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: - 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 amount_remaining = 0 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) 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 - 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 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) diff --git a/scenes/ui/inventory/inventory.gd b/scenes/ui/inventory/inventory.gd index fa7d08e..12c5390 100644 --- a/scenes/ui/inventory/inventory.gd +++ b/scenes/ui/inventory/inventory.gd @@ -47,7 +47,7 @@ func refresh_inventory_grid() -> void: for item: DBItemResource in InventoryManager.inventory: 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): create_item_cell(null) @@ -63,8 +63,11 @@ func toggle_inventory() -> void: InventoryManager.inventory_closed.emit() get_tree().paused = false +## Add any items from the existing inventory resource 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: InventoryManager.add_to_inventory.emit(item_resource.id, item_resource.amount)