Browse Source

feat: Use null to indicate empty slots to keep inventory order

pull/24/head
Ryan Reed 3 weeks ago
parent
commit
ac92c7501f
1 changed files with 16 additions and 7 deletions
  1. +16
    -7
      autoloads/inventory_manager.gd

+ 16
- 7
autoloads/inventory_manager.gd View File

@ -14,12 +14,14 @@ signal add_to_inventory(item_id: String, amount: int)
signal remove_from_inventory(item_id: String, amount: int)
signal item_added(item_id: String, amount: int)
signal item_removed(item_id: String, amount: int)
signal inventory_slot_cleared(slot_index: int)
var quick_slot_item_id: String = "001"
var max_inventory_items: int = 40 # 4 rows of 10
var inventory: Array[DBItemResource] = []
var inventory: Array[DBItemResource] = [] ## To ensure inventory is automatically sorted, "empty" inventory cells will be replaced with null to keep positions
var _inventory_cache: Dictionary[String, Dictionary] = {} ## Used for caching certain information
@ -50,13 +52,15 @@ func available_space(item_id: String) -> int:
func _find_stacks_by_id(item_resource: DBItemResource, item_id: String) -> bool:
return item_resource.id == item_id
return item_resource != null and item_resource.id == item_id
## Find the stack where at least one item can be added
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
item_resource == null or (
item_resource.id == item_id and
item_resource.amount < item_resource.max_stack_size
)
)
func _update_cache_total(item_id: String, amount: int) -> void:
@ -81,8 +85,9 @@ func _on_add_to_inventory(item_id: String, amount: int = 1) -> void:
while amount_remaining > 0:
var first_stack_index: int = inventory.find_custom(_find_stacks_with_space.bind(item_id))
var stack_resource: DBItemResource
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()
stack_resource = item_resource.duplicate()
inventory.append(stack_resource)
if amount_remaining <= stack_resource.max_stack_size:
@ -94,7 +99,10 @@ 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
elif first_stack_index > -1:
var stack_resource: DBItemResource = inventory[first_stack_index]
if inventory[first_stack_index] == null: # Empty slot
inventory[first_stack_index] = item_resource.duplicate()
stack_resource = 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 has space
@ -125,8 +133,9 @@ func _on_remove_from_inventory(item_id: String, amount: int = 1) -> void:
else:
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)
inventory[last_stack_index] = null
amount_remaining -= to_remove
inventory_slot_cleared.emit(last_stack_index)
else: # Received more to remove than we had stacks of
amount_remaining = 0


Loading…
Cancel
Save