From 9077c66a943e05a0919bb3bb2437b8ebe7c79547 Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Sun, 6 Apr 2025 12:41:49 -0400 Subject: [PATCH] wip: Add support for removing an item from inventory --- autoloads/inventory_manager.gd | 41 +++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/autoloads/inventory_manager.gd b/autoloads/inventory_manager.gd index a18029b..610c180 100644 --- a/autoloads/inventory_manager.gd +++ b/autoloads/inventory_manager.gd @@ -30,19 +30,26 @@ func _ready() -> void: self.remove_from_inventory.connect(_on_remove_from_inventory) - # TODO: REMOVE ME func _unhandled_input(event: InputEvent) -> void: - if event.is_action_pressed("ui_cancel"): + if event.is_action_pressed("ui_up"): add_to_inventory.emit("003", 1) + elif event.is_action_pressed("ui_down"): + remove_from_inventory.emit("003", 1) -func _find_first_available_stack(item_resource: DBItemResource, item_id: String) -> bool: +## Find the stack where at least one item can be added +func _find_available_stack(item_resource: DBItemResource, item_id: String) -> bool: return ( item_resource.id == item_id and item_resource.amount < item_resource.max_stack_size ) +## Find the stack in which we can remove a specific item +## The inventory has a stack, then it should have at least 1 item. Empty stacks are not kept in the inventory. +func _find_removable_stack(item_resource: DBItemResource, item_id: String) -> bool: + return item_resource.id == item_id + func _on_add_to_inventory(item_id: String, amount: int = 1) -> void: if not DBItems.data.get(item_id): @@ -58,7 +65,7 @@ func _on_add_to_inventory(item_id: String, amount: int = 1) -> void: # 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_available_stack.bind(item_id)) + var first_stack_index: int = inventory.find_custom(_find_available_stack.bind(item_id)) if first_stack_index == -1 and inventory.size() < max_inventory_items: var stack_resource: DBItemResource = item_resource.duplicate() @@ -71,7 +78,6 @@ func _on_add_to_inventory(item_id: String, amount: int = 1) -> void: 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 stack_resource: DBItemResource = inventory[first_stack_index] var current_amount: int = stack_resource.amount @@ -83,14 +89,29 @@ func _on_add_to_inventory(item_id: String, amount: int = 1) -> void: 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) func _on_remove_from_inventory(item_id: String, amount: int = 1) -> void: - if not inventory.has(item_id): return + var amount_remaining: int = amount + while amount_remaining > 0: + var last_stack_index: int = inventory.rfind_custom(_find_removable_stack.bind(item_id)) + + if last_stack_index > -1: + var stack_resource: DBItemResource = inventory[last_stack_index] + var current_stack_amount: int = stack_resource.amount + var total_amount: int = current_stack_amount - amount_remaining + + if total_amount > 0: # Stack will not be empty after removing + inventory[last_stack_index].amount -= amount_remaining + amount_remaining = 0 + else: + var remaining: int = stack_resource.max_stack_size - current_stack_amount + inventory.remove_at(last_stack_index) + amount_remaining -= remaining + else: # Received more to remove than we had stacks of + amount_remaining = 0 - #if inventory[item_id].amount > 0: - #inventory[item_id].amount -= amount - #item_removed.emit(item_id, max(0, amount)) + item_removed.emit(item_id, amount - amount_remaining) func _on_item_dropped(item_id: String) -> void: _on_remove_from_inventory(item_id, 1)