Basic inventory, item and storage

This commit is contained in:
Rendo 2025-10-12 16:28:44 +05:00
commit 66a623e568
11 changed files with 155 additions and 1 deletions

View file

@ -0,0 +1,50 @@
@tool
extends Inventory
class_name 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
@export_storage var internal_array : Array[InventorySlot] = []
func find(item : Item) -> int:
for i in range(len(internal_array)):
if internal_array[i].held_item == item:
return i
return -1
func add(stack: Stack) -> Stack:
var found_index : int = find(stack.held_item)
if found_index != -1:
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 = internal_array[i].merge_stack(stack)
if stack == null:
return null
return stack
func pop() -> Stack:
for i in range(len(internal_array)):
if internal_array[i].held_item != null:
return internal_array[i].extract()
return null
func take(item: Item,amount: int) -> Stack:
var found = find(item)
if found == -1:
return null
return internal_array[found].extract_stack(amount)