107 lines
2.9 KiB
GDScript
107 lines
2.9 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 add_from_side(stack : Stack, ang_diff : float) -> Stack:
|
|
if is_equal_approx(abs(ang_diff),PI/2):
|
|
if internal_array[capacity/2].amount != 0:
|
|
return null
|
|
stack_added.emit(stack,capacity/2)
|
|
return internal_array[capacity/2].merge_stack(stack)
|
|
elif is_equal_approx(abs(ang_diff), PI):
|
|
if internal_array[capacity-1].amount != 0:
|
|
return null
|
|
stack_added.emit(stack,capacity-1)
|
|
return internal_array[capacity-1].merge_stack(stack)
|
|
return add(stack)
|
|
|
|
func can_add() -> bool:
|
|
return internal_array[0].amount == 0
|
|
|
|
func can_add_from_side(ang_diff : float) -> bool:
|
|
if is_equal_approx(abs(ang_diff),PI/2):
|
|
return internal_array[capacity/2].amount == 0
|
|
elif is_equal_approx(abs(ang_diff),PI):
|
|
return internal_array[capacity-1].amount == 0
|
|
return can_add()
|
|
|
|
## 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())
|