Queue
This commit is contained in:
parent
23b6e5d180
commit
cb9aef6761
2 changed files with 63 additions and 0 deletions
62
scripts/inventory/queue.gd
Normal file
62
scripts/inventory/queue.gd
Normal file
|
@ -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())
|
1
scripts/inventory/queue.gd.uid
Normal file
1
scripts/inventory/queue.gd.uid
Normal file
|
@ -0,0 +1 @@
|
||||||
|
uid://q2gbtqfi32d
|
Loading…
Add table
Add a link
Reference in a new issue