delurbelako/scripts/inventory/splitter_inventory.gd
2025-10-16 23:29:49 +05:00

74 lines
1.9 KiB
GDScript

@tool
extends Inventory
class_name SplitterInventory
@export var capacity : int:
set(value):
if value < 0:
return
if value == capacity:
return
capacity = value
get:
return capacity
@export_storage var upper_array : Array[InventorySlot]
@export_storage var down_array : Array[InventorySlot]
func _init() -> void:
super()
deferred_init.call_deferred()
func deferred_init():
upper_array.resize(capacity)
down_array.resize(capacity)
for i in range(capacity):
upper_array[i] = InventorySlot.new()
down_array[i] = InventorySlot.new()
func add(stack : Stack, context: InventoryContext = null) -> Stack:
if context == null:
return stack
if context.position == context.target.global_position:
return add_to_array(stack,upper_array,context)
return add_to_array(stack,down_array,context)
func add_to_array(stack: Stack, array: Array[InventorySlot], context: InventoryContext = null) -> Stack:
for i in range(len(array)):
if array[i].held_item == null or array[i].held_item == stack.held_item:
stack_added.emit(stack,i)
array[i].merge_stack(stack)
if stack.is_valid() == false:
return null
return stack
func can_add(item : Item = null, context: InventoryContext = null) -> bool:
if context == null:
return false
if item == null:
for i in range(capacity):
if upper_array[i].amount == 0 or down_array[i].amount == 0:
return true
else:
for i in range(capacity):
if upper_array[i].amount == 0:
return upper_array[i].can_be_merged(item)
if down_array[i].amount ==0:
return down_array[i].can_be_merged(item)
return false
func pop() -> Stack:
return null
func peek() -> Stack:
return null
## Tries to take certain item from inventory. Returns null if no item found
func take(item : Item,amount: int) -> Stack:
return null
## Finds first entry of item. Returns -1 if no item found
func find(item : Item) -> int:
return -1