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

75 lines
1.8 KiB
GDScript

@tool
extends Inventory
## Base class for FIFO inventories.
class_name Queue
## 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
## :3
@export_storage var internal_array : Array[InventorySlot] = []
func _init() -> void:
super()
internal_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(len(internal_array)):
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:
for i in range(len(internal_array)):
if internal_array[i].held_item == null or internal_array[i].held_item == stack.held_item:
stack_added.emit(stack,i)
stack = internal_array[i].merge_stack(stack)
if stack == null:
return null
return stack
func can_add() -> bool:
for i in range(capacity):
if internal_array[i].amount == 0:
return true
return false
## Tries to take first item. Returns null if no items in inventory
func pop() -> Stack:
if internal_array[0].held_item == null:
return null
var stack_to_return = internal_array[0].extract()
stack_taken.emit(stack_to_return,0)
sort()
return stack_to_return
## 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 stack_to_return = internal_array[found].extract_stack(amount)
stack_taken.emit(stack_to_return,found)
sort()
return stack_to_return
func sort() -> void:
for i in range(len(internal_array)-1):
if internal_array[i] == null:
internal_array[i].merge_stack(internal_array[i+1].extract())