Stack rework

This commit is contained in:
Rendo 2025-10-15 17:56:18 +05:00
commit ea9c41054b
7 changed files with 60 additions and 39 deletions

View file

@ -15,6 +15,8 @@ output_capacity = 1
input_array = Array[ExtResource("2_p1cyh")]([Object(RefCounted,"script":ExtResource("2_p1cyh"),"held_item":null,"amount":0,"filter":null)
, Object(RefCounted,"script":ExtResource("2_p1cyh"),"held_item":null,"amount":0,"filter":null)
])
output_slot = Object(RefCounted,"script":ExtResource("2_p1cyh"),"held_item":null,"amount":0,"filter":null)
metadata/_custom_type_script = "uid://m6b6vawdqgqb"
[sub_resource type="AtlasTexture" id="AtlasTexture_xh4eg"]

View file

@ -43,19 +43,22 @@ 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)
internal_array[0].merge_stack(stack)
return 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)
internal_array[capacity/2].merge_stack(stack)
return 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)
internal_array[capacity-1].merge_stack(stack)
return stack
return add(stack)
func can_add(_item : Item = null) -> bool:
@ -111,4 +114,7 @@ func sort() -> void:
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())
var extracted_stack = internal_array[i-1].extract()
internal_array[i].merge_stack(extracted_stack)
if extracted_stack.is_valid():
internal_array[i-1].merge_stack(extracted_stack)

View file

@ -20,17 +20,11 @@ func _init() -> void:
resize.call_deferred()
func add(stack : Stack) -> Stack:
var found_index : int = find_input(stack.held_item)
if found_index != -1:
stack_added.emit(stack,found_index)
stack = input_array[found_index].merge_stack(stack)
if stack == null:
return null
for i in range(input_capacity):
if input_array[i].can_be_merged(stack.held_item):
stack_added.emit(stack,i)
stack = input_array[i].merge_stack(stack)
if stack == null:
input_array[i].merge_stack(stack)
if stack.is_valid() == false:
return null
return stack
@ -39,15 +33,12 @@ func add_from_side(stack : Stack, _ang_diff : float) -> Stack:
func can_add(item : Item = null) -> bool:
if item == null:
for i in range(input_capacity):
return input_array[i].amount == 0
return false
else:
for i in range(input_capacity):
if input_array[i].can_be_merged(item):
return true
#return true
return false
return false
func can_add_from_side(_ang_diff : float,item: Item = null) -> bool:
return can_add(item)
@ -68,6 +59,8 @@ func peek() -> Stack:
## Tries to take certain item from inventory. Returns null if no item found
func take(item: Item,amount: int) -> Stack:
if item != output_slot.filter:
return null
var extracted = output_slot.extract_stack(amount)
return extracted

View file

@ -71,19 +71,25 @@ func can_be_merged(item : Item) -> bool:
return item == held_item
## Add provided stack's amount to this amount. Returns null if stack is empty or items arent matching
func merge_stack(stack: Stack) -> Stack:
if stack == null or stack.held_item == null or stack.amount == 0:
return stack
func merge_stack(stack: Stack) -> bool:
if stack == null or stack.is_valid() == false:
return false
if filter and filter.is_equal(stack.held_item) == false:
return false
if held_item == null:
if filter != null and filter.is_equal(stack.held_item) == false:
return stack
held_item = stack.held_item
amount = stack.amount
return null
if stack.held_item != held_item:
return null
var return_stack_amount = max(0,stack.amount-amount)
amount += stack.amount - return_stack_amount
if return_stack_amount == 0:
return null
return Stack.new(held_item,return_stack_amount)
stack.invalidate()
return true
var delta_amount = stack.amount - (held_item.stack_size - amount)
amount += stack.amount
if delta_amount <= 0:
stack.invalidate()
return true
stack.amount = delta_amount
return true

View file

@ -13,3 +13,10 @@ func _init(item: Item = null, amount: int = 0) -> void:
## Amount of items in stack
@export_storage var amount : int
func invalidate() -> void:
held_item = null
amount = 0
func is_valid() -> bool:
return held_item != null or amount > 0

View file

@ -40,14 +40,14 @@ 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:
internal_array[found_index].merge_stack(stack)
if stack.is_valid() == false:
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:
internal_array[i].merge_stack(stack)
if stack.is_valid() == false:
return null
return stack

View file

@ -19,12 +19,7 @@ func _process(delta: float) -> void:
inventory.advance(delta)
queue_redraw()
var next : Structure = get_next()
if next == null or next.inventory == null or next.inventory.can_add_from_side(Globals.facing_difference(next.facing,structure_parent.facing),inventory.internal_array[inventory.capacity-1].held_item) == false:
return
var popped = inventory.pop()
if popped == null:
return
next.inventory.add_from_side(popped,Globals.facing_difference(next.facing,structure_parent.facing))
try_transfer(next)
func calculate_position(index: int) -> Vector2:
var indexed_part = 16.0 / inventory.capacity
@ -43,6 +38,18 @@ func get_next() -> Structure:
var faced_vector = Structure.facing_to_vector(structure_parent.facing)
return structure_parent.get_relative(faced_vector)
func try_transfer(structure : Structure) -> void:
if structure == null or inventory.internal_array[inventory.capacity-1].amount == 0:
return
var last_slot = inventory.internal_array[inventory.capacity-1]
var ang_diff = Globals.facing_difference(structure_parent.facing,structure.facing)
if structure.inventory.can_add_from_side(ang_diff,last_slot.held_item) == false:
return
last_slot.merge_stack(structure.inventory.add_from_side(last_slot.extract(),ang_diff))
if last_slot.amount == 0:
inventory.sort()
func _on_conveyor_switched_facing(to: Structure.Facing) -> void:
match to:
Structure.Facing.RIGHT: