This commit is contained in:
Rendo 2025-10-14 21:55:24 +05:00
commit 222960a824
19 changed files with 282 additions and 22 deletions

View file

@ -0,0 +1,87 @@
@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 can_add() -> bool:
return internal_array[0].amount == 0
## 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())

View file

@ -0,0 +1 @@
uid://v0hkuo3gda1b

View file

@ -4,12 +4,19 @@ extends Resource
## Class that can hold items in it
class_name Inventory
signal stack_added(item : Stack,position : int)
signal stack_taken(item : Stack,position : int)
func _init() -> void:
resource_local_to_scene = true
## Tries to add an item into inventory. Returns not stored stack of item.
@abstract
func add(item : Stack) -> Stack
func add(stack : Stack) -> Stack
## Returns if conditions of adding are met
@abstract
func can_add() -> bool
## Tries to take first item. Returns null if no items in inventory
@abstract

View file

@ -14,15 +14,19 @@ class_name Queue
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] = []
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)):
@ -34,16 +38,24 @@ func find(item : Item) -> int:
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_added.emit(stack,i)
stack = internal_array[i].merge_stack(stack)
if stack == null:
return null
return stack
func can_add() -> bool:
for i in range(capacity):
if internal_array[i].amount == 0:
return true
return false
## 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()
stack_taken.emit(stack_to_return,0)
sort()
return stack_to_return
@ -53,6 +65,7 @@ func take(item: Item,amount: int) -> Stack:
if found == -1:
return null
var stack_to_return = internal_array[found].extract_stack(amount)
stack_taken.emit(stack_to_return,found)
sort()
return stack_to_return

View file

@ -13,15 +13,18 @@ class_name Storage
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] = []
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)):
@ -33,21 +36,31 @@ func find(item : Item) -> int:
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 can_add() -> bool:
for i in range(capacity):
if internal_array[i].amount == 0:
return true
return false
## 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:
return internal_array[i].extract()
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
@ -55,4 +68,6 @@ func take(item: Item,amount: int) -> Stack:
var found = find(item)
if found == -1:
return null
return internal_array[found].extract_stack(amount)
var extracted = internal_array[found].extract_stack(amount)
stack_taken.emit(extracted,found)
return extracted