delurbelako/scripts/inventory/conveyor_inventory.gd
2025-10-14 21:55:24 +05:00

87 lines
2.2 KiB
GDScript

@tool
extends Inventory
class_name ConveyorInventory
## Amount of stacks that can be held in storage
@export var capacity : int:
set(value):
if value < 0:
return
if value == capacity:
return
capacity = value
get:
return capacity
@export var progress_speed : float = 1.0
@export var pop_treshold : float = 0.95
## :3
@export_storage var internal_array : Array[InventorySlot] = []
@export_storage var progress_array : Array[float] = []
func _init() -> void:
super()
deferred_init.call_deferred()
func deferred_init():
internal_array.resize(capacity)
progress_array.resize(capacity)
for i in range(capacity):
internal_array[i] = InventorySlot.new()
## Finds first entry of item. Returns -1 if no item found
func find(item : Item) -> int:
for i in range(capacity):
if internal_array[i].held_item == item:
return i
return -1
## Tries to add an item into inventory. Returns not stored stack of item.
func add(stack: Stack) -> Stack:
if internal_array[0].amount != 0:
return null
stack_added.emit(stack,0)
return internal_array[0].merge_stack(stack)
func can_add() -> bool:
return internal_array[0].amount == 0
## Tries to take first item. Returns null if no items in inventory
func pop() -> Stack:
if internal_array[capacity-1].amount == 0:
return null
if progress_array[capacity-1] < pop_treshold:
return null
stack_taken.emit(internal_array[capacity-1], capacity-1)
return internal_array[capacity-1].extract()
func advance(delta : float) -> void:
var progress_flag : bool = false
for i in range(capacity):
if internal_array[i].amount == 0:
continue
if progress_array[i] >= 1:
progress_flag = true
continue
progress_array[i] += delta * progress_speed
if progress_flag:
sort()
## Tries to take certain item from inventory. Returns null if no item found
func take(item: Item,amount: int) -> Stack:
var found = find(item)
if found == -1:
return null
var extracted = internal_array[found].extract_stack(amount)
stack_taken.emit(extracted,found)
return extracted
func sort() -> void:
for i in range(capacity-1,0,-1):
if progress_array[i-1] >= 1.0 and internal_array[i].amount == 0:
progress_array[i-1] = 0
progress_array[i] = 0
internal_array[i].merge_stack(internal_array[i-1].extract())