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

View file

@ -12,3 +12,5 @@ class_name Item
## Maximum amount of items that can be held in inventory
@export var stack_size : int
@export var scene : PackedScene

View file

@ -13,6 +13,9 @@ class_name Structure
get:
return dimensions
## Inventory of this structure
@export var inventory : Inventory
## Debug draw of points
func _draw() -> void:
if Engine.is_editor_hint():
@ -23,7 +26,9 @@ func _draw() -> void:
## Get structure at tile coordinates relative to this structure [br]
## dv : Vector2 - get position in tiles [br]
func get_relative(dv : Vector2) -> Structure:
return get_parent().get_at(global_position+dv*Globals.GRID_SIZE)
if get_parent() is GridController:
return get_parent().get_at(global_position+dv*Globals.GRID_SIZE)
return null
## Check structure is in zone and does not collide with any other structures [br]
## Returns true if structure is in zone and does not collide with any other structure [br]

View file

@ -0,0 +1,20 @@
extends Node2D
@onready var inventory : ConveyorInventory = get_parent().inventory
func _draw() -> void:
for i in range(inventory.capacity):
if inventory.internal_array[i].amount > 0:
var calculated_position = Vector2(-8,0)+Vector2(16/inventory.capacity*i+16/inventory.capacity*inventory.progress_array[i],0)
draw_texture(inventory.internal_array[i].held_item.preview,calculated_position - inventory.internal_array[i].held_item.preview.get_size()/2.0)
func _process(delta: float) -> void:
inventory.advance(delta)
queue_redraw()
var right : Structure = get_parent().get_relative(Vector2.RIGHT)
if right == null or right.inventory.can_add() == false:
return
var popped = inventory.pop()
if popped == null:
return
right.inventory.add(popped)

View file

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

View file

@ -0,0 +1,9 @@
extends Node
const test_item :=preload("res://generic/items/dbg_item.tres")
func _process(_delta: float) -> void:
for point in [Vector2(-1,0),Vector2(1,0),Vector2(0,1),Vector2(0,-1)]:
var found_structure = get_parent().get_relative(point)
if found_structure:
found_structure.inventory.add(Stack.new(test_item,1))

View file

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