@ -3,7 +3,7 @@ extends Node
signal next_quick_slot
signal previous_quick_slot
signal select_quick_slot ( slot_index : int )
signal quick_slot_item_changed ( item_id : String )
signal quick_slot_selected ( slot_index : int )
signal item_picked_up ( item : DBItemResource )
signal item_dropped ( item : DBItemResource )
signal inventory_opened
@ -14,23 +14,26 @@ 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_updated ( slot_index : int )
var quick_slot_item_id : String = " 001 "
var quick_slot_count : int = 10
var selected_quick_slot : int = 0
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
func _ready ( ) - > void :
self . quick_slot_item_changed . connect ( _on_quick_slot_item_changed )
self . item_picked_up . connect ( _on_item_picked_up )
self . item_dropped . connect ( _on_item_dropped )
self . add_to_inventory . connect ( _on_add_to_inventory )
self . remove_from_inventory . connect ( _on_remove_from_inventory )
self . clear_inventory . connect ( _on_clear_inventory )
self . quick_slot_selected . connect ( _on_quick_slot_selected )
func available_space ( item_id : String ) - > int :
@ -48,15 +51,24 @@ func available_space(item_id: String) -> int:
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 get_inventory_item ( item_slot : int = selected_quick_slot ) - > DBItemResource :
if item_slot > = inventory . size ( ) or item_slot < 0 :
return null
return inventory . get ( item_slot )
func get_quick_slot_item_id ( item_slot : int = selected_quick_slot ) - > String :
return inventory [ item_slot ] . id
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 +93,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 : DBItemR esource = item_resource . duplicate ( )
stack_resource = item_resource . duplicate ( )
inventory . append ( stack_resource )
if amount_remaining < = stack_resource . max_stack_size :
@ -93,8 +106,13 @@ func _on_add_to_inventory(item_id: String, amount: int = 1) -> void:
_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
inventory_slot_updated . emit ( inventory . size ( ) - 1 )
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
@ -106,6 +124,8 @@ 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
inventory_slot_updated . emit ( first_stack_index )
item_added . emit ( item_id , amount - amount_remaining )
func _on_remove_from_inventory ( item_id : String , amount : int = 1 ) - > void :
@ -125,9 +145,11 @@ 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
else : # Received more to remove than we had stacks of
inventory_slot_updated . emit ( last_stack_index )
else : # Received more to remove than we have stacks for
amount_remaining = 0
item_removed . emit ( item_id , amount - amount_remaining )
@ -136,11 +158,11 @@ func _on_clear_inventory() -> void:
inventory . clear ( )
_inventory_cache . clear ( )
func _on_item_dropped ( item_id : String ) - > void :
_on_remove_from_inventory ( item_ id , 1 )
func _on_item_dropped ( item : DBItemResource ) - > void :
_on_remove_from_inventory ( item . id , 1 )
func _on_item_picked_up ( item_id : String ) - > void :
_on_add_to_inventory ( item_ id , 1 )
func _on_item_picked_up ( item : DBItemResource ) - > void :
_on_add_to_inventory ( item . id , 1 )
func _on_quick_slot_item_changed ( item_id : String ) - > void :
quick_slot_item_id = item_id
func _on_quick_slot_selected ( slot_index : int ) - > void :
selected_quick_slot = slot_index