@ -4,6 +4,10 @@ extends Node
signal item_dropped ( item : DBItemResource )
signal item_dropped ( item : DBItemResource )
signal item_picked_up ( item : DBItemResource )
signal item_picked_up ( item : DBItemResource )
signal creative_mode_disabled
signal creative_mode_enabled
signal toggle_creative_mode ( enabled : bool )
#region Inventory Specific
#region Inventory Specific
signal add_to_inventory ( item_id : String , amount : int )
signal add_to_inventory ( item_id : String , amount : int )
signal clear_inventory ## Remove all items in inventory
signal clear_inventory ## Remove all items in inventory
@ -28,6 +32,7 @@ signal select_quick_slot(slot_index: int)
var max_inventory_items : int = 40 # 4 rows of 10
var max_inventory_items : int = 40 # 4 rows of 10
var quick_slot_count : int = 10
var quick_slot_count : int = 10
var selected_quick_slot : int = 0
var selected_quick_slot : int = 0
var creative_mode : bool = false
var inventory : Array [ DBItemResource ] = [ ] ## To ensure inventory is automatically sorted, "empty" inventory cells will be replaced with null to keep positions
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
var _inventory_cache : Dictionary [ String , Dictionary ] = { } ## Used for caching certain information
@ -43,6 +48,7 @@ func _ready() -> void:
self . remove_from_inventory . connect ( _on_remove_from_inventory )
self . remove_from_inventory . connect ( _on_remove_from_inventory )
self . remove_from_quickslot . connect ( _on_remove_from_quickslot )
self . remove_from_quickslot . connect ( _on_remove_from_quickslot )
self . remove_from_slot . connect ( _on_remove_from_slot )
self . remove_from_slot . connect ( _on_remove_from_slot )
self . toggle_creative_mode . connect ( _on_toggle_creative_mode )
func available_space ( item_id : String ) - > int :
func available_space ( item_id : String ) - > int :
@ -66,6 +72,8 @@ func get_inventory_item(item_slot: int = selected_quick_slot) -> DBItemResource:
return inventory . get ( item_slot )
return inventory . get ( item_slot )
func get_quick_slot_item_id ( item_slot : int = selected_quick_slot ) - > String :
func get_quick_slot_item_id ( item_slot : int = selected_quick_slot ) - > String :
if item_slot > = inventory . size ( ) : return " "
return inventory [ item_slot ] . id
return inventory [ item_slot ] . id
@ -84,6 +92,8 @@ func _find_stacks_with_space(item_resource: DBItemResource, item_id: String) ->
## Removes an amount of items from a specific slot
## Removes an amount of items from a specific slot
## If the amount exceeds the amount of the slot, will NOT remove from other stacks
## If the amount exceeds the amount of the slot, will NOT remove from other stacks
func _remove_from_slot ( slot_index : int , amount : int ) - > void :
func _remove_from_slot ( slot_index : int , amount : int ) - > void :
if creative_mode : return
if slot_index > = max_inventory_items :
if slot_index > = max_inventory_items :
printerr ( " Slot Index " , slot_index , " out of inventory range " )
printerr ( " Slot Index " , slot_index , " out of inventory range " )
return
return
@ -95,6 +105,8 @@ func _remove_from_slot(slot_index: int, amount: int) -> void:
inventory_slot_updated . emit ( slot_index )
inventory_slot_updated . emit ( slot_index )
func _update_cache_total ( item_id : String , amount : int ) - > void :
func _update_cache_total ( item_id : String , amount : int ) - > void :
if creative_mode : return
if not _inventory_cache . get ( item_id ) :
if not _inventory_cache . get ( item_id ) :
_inventory_cache [ item_id ] = { " total " : 0 }
_inventory_cache [ item_id ] = { " total " : 0 }
_inventory_cache [ item_id ] . total += amount
_inventory_cache [ item_id ] . total += amount
@ -131,7 +143,7 @@ func _on_add_to_inventory(item_id: String, amount: int = 1) -> void:
amount_remaining -= stack_resource . max_stack_size
amount_remaining -= stack_resource . max_stack_size
inventory_slot_updated . emit ( inventory . size ( ) - 1 )
inventory_slot_updated . emit ( inventory . size ( ) - 1 )
elif first_stack_index > - 1 :
elif first_stack_index > - 1 and not creative_mode :
if inventory [ first_stack_index ] == null : # Empty slot
if inventory [ first_stack_index ] == null : # Empty slot
inventory [ first_stack_index ] = item_resource . duplicate ( )
inventory [ first_stack_index ] = item_resource . duplicate ( )
@ -152,6 +164,8 @@ func _on_add_to_inventory(item_id: String, amount: int = 1) -> void:
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 :
func _on_remove_from_inventory ( item_id : String , amount : int = 1 ) - > void :
if creative_mode : return
var amount_remaining : int = amount
var amount_remaining : int = amount
while amount_remaining > 0 :
while amount_remaining > 0 :
var last_stack_index : int = inventory . rfind_custom ( _find_stacks_by_id . bind ( item_id ) )
var last_stack_index : int = inventory . rfind_custom ( _find_stacks_by_id . bind ( item_id ) )
@ -195,3 +209,10 @@ func _on_remove_from_slot(slot_index: int, amount: int) -> void:
func _on_remove_from_quickslot ( amount : int ) - > void :
func _on_remove_from_quickslot ( amount : int ) - > void :
_remove_from_slot ( selected_quick_slot , amount )
_remove_from_slot ( selected_quick_slot , amount )
func _on_toggle_creative_mode ( enabled : bool ) - > void :
creative_mode = enabled
if creative_mode :
creative_mode_enabled . emit ( )
else :
creative_mode_disabled . emit ( )