From cb9aef6761a92904546fce02b3503728f5a7d178 Mon Sep 17 00:00:00 2001 From: Rendo Date: Sun, 12 Oct 2025 17:44:31 +0500 Subject: [PATCH] Queue --- scripts/inventory/queue.gd | 62 ++++++++++++++++++++++++++++++++++ scripts/inventory/queue.gd.uid | 1 + 2 files changed, 63 insertions(+) create mode 100644 scripts/inventory/queue.gd create mode 100644 scripts/inventory/queue.gd.uid diff --git a/scripts/inventory/queue.gd b/scripts/inventory/queue.gd new file mode 100644 index 0000000..73149c1 --- /dev/null +++ b/scripts/inventory/queue.gd @@ -0,0 +1,62 @@ +@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 + internal_array.resize(capacity) + for i in range(capacity): + internal_array[i] = InventorySlot.new() + get: + return capacity + +## :3 +@export_storage var internal_array : Array[InventorySlot] = [] + +## 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 = internal_array[i].merge_stack(stack) + if stack == null: + return null + return stack + +## 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() + 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) + 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()) diff --git a/scripts/inventory/queue.gd.uid b/scripts/inventory/queue.gd.uid new file mode 100644 index 0000000..c8e18b3 --- /dev/null +++ b/scripts/inventory/queue.gd.uid @@ -0,0 +1 @@ +uid://q2gbtqfi32d