delurbelako/scripts/inventory/storage.gd

79 lines
2.1 KiB
GDScript

@tool
extends Inventory
## Base class for simple storages with no differentiation.
class_name Storage
## 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:
var found_index : int = find(stack.held_item)
if found_index != -1:
stack_added.emit(stack,found_index)
stack = internal_array[found_index].merge_stack(stack)
if stack == null:
return null
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 add_from_side(stack : Stack, _ang_diff : float) -> Stack:
return add(stack)
func can_add() -> bool:
for i in range(capacity):
if internal_array[i].amount == 0:
return true
return false
func can_add_from_side(_ang_diff : float) -> bool:
return can_add()
## Tries to take first item. Returns null if no items in inventory
func pop() -> Stack:
for i in range(len(internal_array)):
if internal_array[i].held_item != null:
var extracted = internal_array[i].extract()
stack_taken.emit(extracted,i)
return extracted
return null
## 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