diff --git a/autoloads/inventory_manager.gd b/autoloads/inventory_manager.gd index 610c180..cf15668 100644 --- a/autoloads/inventory_manager.gd +++ b/autoloads/inventory_manager.gd @@ -19,7 +19,7 @@ var quick_slot_item_id: String = "001" var max_inventory_items: int = 40 # 4 rows of 10 var inventory: Array[DBItemResource] = [] - +var _inventory_cache: Dictionary[String, Dictionary] = {} ## Used for caching certain information func _ready() -> void: self.quick_slot_item_changed.connect(_on_quick_slot_item_changed) @@ -38,17 +38,36 @@ func _unhandled_input(event: InputEvent) -> void: remove_from_inventory.emit("003", 1) +func available_space(item_id: String) -> int: + var full_stacks: int = floor(_inventory_cache[item_id].total / DBItems.data[item_id].max_stack_size) + var space_in_stacks: int = ( + DBItems.data[item_id].max_stack_size - abs( + _inventory_cache[item_id].total - (DBItems.data[item_id].max_stack_size * full_stacks) + ) + ) + + # This is a bit of a hack because I'm a math/logic noob + if space_in_stacks == DBItems.data[item_id].max_stack_size: + space_in_stacks = 0 + + var open_stack_amount: int = (max_inventory_items - inventory.size()) * DBItems.data[item_id].max_stack_size + return open_stack_amount + space_in_stacks + + +func _find_stacks_by_id(item_resource: DBItemResource, item_id: String) -> bool: + return item_resource.id == item_id + ## Find the stack where at least one item can be added -func _find_available_stack(item_resource: DBItemResource, item_id: String) -> bool: +func _find_stacks_with_space(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 _update_cache_total(item_id: String, amount: int) -> void: + if not _inventory_cache.get(item_id): + _inventory_cache[item_id] = {"total": 0} + _inventory_cache[item_id].total += amount func _on_add_to_inventory(item_id: String, amount: int = 1) -> void: @@ -65,27 +84,30 @@ 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_available_stack.bind(item_id)) + var first_stack_index: int = inventory.find_custom(_find_stacks_with_space.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: # No existing stack and empty slots available var stack_resource: DBItemResource = item_resource.duplicate() inventory.append(stack_resource) if amount_remaining <= stack_resource.max_stack_size: + _update_cache_total(item_id, amount_remaining) inventory[-1].amount += amount_remaining amount_remaining = 0 else: + _update_cache_total(item_id, stack_resource.max_stack_size) inventory[-1].amount = stack_resource.max_stack_size amount_remaining -= stack_resource.max_stack_size - elif first_stack_index > -1: var stack_resource: DBItemResource = inventory[first_stack_index] var current_amount: int = stack_resource.amount var total_amount: int = current_amount + amount_remaining - if total_amount < stack_resource.max_stack_size: # Stack not full and can hold + if total_amount < stack_resource.max_stack_size: # Stack not full and has space + _update_cache_total(item_id, amount_remaining) inventory[first_stack_index].amount = total_amount amount_remaining = 0 else: + _update_cache_total(item_id, stack_resource.max_stack_size - current_amount) inventory[first_stack_index].amount = stack_resource.max_stack_size amount_remaining -= stack_resource.max_stack_size @@ -94,7 +116,7 @@ func _on_add_to_inventory(item_id: String, amount: int = 1) -> void: func _on_remove_from_inventory(item_id: String, amount: int = 1) -> void: var amount_remaining: int = amount while amount_remaining > 0: - var last_stack_index: int = inventory.rfind_custom(_find_removable_stack.bind(item_id)) + var last_stack_index: int = inventory.rfind_custom(_find_stacks_by_id.bind(item_id)) if last_stack_index > -1: var stack_resource: DBItemResource = inventory[last_stack_index] @@ -102,12 +124,14 @@ func _on_remove_from_inventory(item_id: String, amount: int = 1) -> void: var total_amount: int = current_stack_amount - amount_remaining if total_amount > 0: # Stack will not be empty after removing + _update_cache_total(item_id, -amount_remaining) inventory[last_stack_index].amount -= amount_remaining amount_remaining = 0 else: - var remaining: int = stack_resource.max_stack_size - current_stack_amount + var to_remove: int = stack_resource.max_stack_size - current_stack_amount + _update_cache_total(item_id, -amount_remaining) inventory.remove_at(last_stack_index) - amount_remaining -= remaining + amount_remaining -= to_remove else: # Received more to remove than we had stacks of amount_remaining = 0