Stack rework
This commit is contained in:
parent
5da9e11f36
commit
ea9c41054b
7 changed files with 60 additions and 39 deletions
|
@ -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)
|
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)
|
, 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"
|
metadata/_custom_type_script = "uid://m6b6vawdqgqb"
|
||||||
|
|
||||||
[sub_resource type="AtlasTexture" id="AtlasTexture_xh4eg"]
|
[sub_resource type="AtlasTexture" id="AtlasTexture_xh4eg"]
|
||||||
|
|
|
@ -43,19 +43,22 @@ func add(stack: Stack) -> Stack:
|
||||||
if internal_array[0].amount != 0:
|
if internal_array[0].amount != 0:
|
||||||
return null
|
return null
|
||||||
stack_added.emit(stack,0)
|
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:
|
func add_from_side(stack : Stack, ang_diff : float) -> Stack:
|
||||||
if is_equal_approx(abs(ang_diff),PI/2):
|
if is_equal_approx(abs(ang_diff),PI/2):
|
||||||
if internal_array[capacity/2].amount != 0:
|
if internal_array[capacity/2].amount != 0:
|
||||||
return null
|
return null
|
||||||
stack_added.emit(stack,capacity/2)
|
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):
|
elif is_equal_approx(abs(ang_diff), PI):
|
||||||
if internal_array[capacity-1].amount != 0:
|
if internal_array[capacity-1].amount != 0:
|
||||||
return null
|
return null
|
||||||
stack_added.emit(stack,capacity-1)
|
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)
|
return add(stack)
|
||||||
|
|
||||||
func can_add(_item : Item = null) -> bool:
|
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:
|
if progress_array[i-1] >= 1.0 and internal_array[i].amount == 0:
|
||||||
progress_array[i-1] = 0
|
progress_array[i-1] = 0
|
||||||
progress_array[i] = 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)
|
||||||
|
|
|
@ -20,17 +20,11 @@ func _init() -> void:
|
||||||
resize.call_deferred()
|
resize.call_deferred()
|
||||||
|
|
||||||
func add(stack : Stack) -> Stack:
|
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):
|
for i in range(input_capacity):
|
||||||
if input_array[i].can_be_merged(stack.held_item):
|
if input_array[i].can_be_merged(stack.held_item):
|
||||||
stack_added.emit(stack,i)
|
stack_added.emit(stack,i)
|
||||||
stack = input_array[i].merge_stack(stack)
|
input_array[i].merge_stack(stack)
|
||||||
if stack == null:
|
if stack.is_valid() == false:
|
||||||
return null
|
return null
|
||||||
return stack
|
return stack
|
||||||
|
|
||||||
|
@ -39,15 +33,12 @@ func add_from_side(stack : Stack, _ang_diff : float) -> Stack:
|
||||||
|
|
||||||
func can_add(item : Item = null) -> bool:
|
func can_add(item : Item = null) -> bool:
|
||||||
if item == null:
|
if item == null:
|
||||||
for i in range(input_capacity):
|
|
||||||
return input_array[i].amount == 0
|
|
||||||
return false
|
return false
|
||||||
else:
|
else:
|
||||||
for i in range(input_capacity):
|
for i in range(input_capacity):
|
||||||
if input_array[i].can_be_merged(item):
|
if input_array[i].can_be_merged(item):
|
||||||
return true
|
return true
|
||||||
#return true
|
return false
|
||||||
return false
|
|
||||||
|
|
||||||
func can_add_from_side(_ang_diff : float,item: Item = null) -> bool:
|
func can_add_from_side(_ang_diff : float,item: Item = null) -> bool:
|
||||||
return can_add(item)
|
return can_add(item)
|
||||||
|
@ -68,6 +59,8 @@ func peek() -> Stack:
|
||||||
|
|
||||||
## Tries to take certain item from inventory. Returns null if no item found
|
## Tries to take certain item from inventory. Returns null if no item found
|
||||||
func take(item: Item,amount: int) -> Stack:
|
func take(item: Item,amount: int) -> Stack:
|
||||||
|
if item != output_slot.filter:
|
||||||
|
return null
|
||||||
var extracted = output_slot.extract_stack(amount)
|
var extracted = output_slot.extract_stack(amount)
|
||||||
return extracted
|
return extracted
|
||||||
|
|
||||||
|
|
|
@ -71,19 +71,25 @@ func can_be_merged(item : Item) -> bool:
|
||||||
return item == held_item
|
return item == held_item
|
||||||
|
|
||||||
## Add provided stack's amount to this amount. Returns null if stack is empty or items arent matching
|
## Add provided stack's amount to this amount. Returns null if stack is empty or items arent matching
|
||||||
func merge_stack(stack: Stack) -> Stack:
|
func merge_stack(stack: Stack) -> bool:
|
||||||
if stack == null or stack.held_item == null or stack.amount == 0:
|
if stack == null or stack.is_valid() == false:
|
||||||
return stack
|
return false
|
||||||
|
|
||||||
|
if filter and filter.is_equal(stack.held_item) == false:
|
||||||
|
return false
|
||||||
|
|
||||||
if held_item == null:
|
if held_item == null:
|
||||||
if filter != null and filter.is_equal(stack.held_item) == false:
|
|
||||||
return stack
|
|
||||||
held_item = stack.held_item
|
held_item = stack.held_item
|
||||||
amount = stack.amount
|
amount = stack.amount
|
||||||
return null
|
stack.invalidate()
|
||||||
if stack.held_item != held_item:
|
return true
|
||||||
return null
|
|
||||||
var return_stack_amount = max(0,stack.amount-amount)
|
var delta_amount = stack.amount - (held_item.stack_size - amount)
|
||||||
amount += stack.amount - return_stack_amount
|
amount += stack.amount
|
||||||
if return_stack_amount == 0:
|
|
||||||
return null
|
if delta_amount <= 0:
|
||||||
return Stack.new(held_item,return_stack_amount)
|
stack.invalidate()
|
||||||
|
return true
|
||||||
|
|
||||||
|
stack.amount = delta_amount
|
||||||
|
return true
|
||||||
|
|
|
@ -13,3 +13,10 @@ func _init(item: Item = null, amount: int = 0) -> void:
|
||||||
|
|
||||||
## Amount of items in stack
|
## Amount of items in stack
|
||||||
@export_storage var amount : int
|
@export_storage var amount : int
|
||||||
|
|
||||||
|
func invalidate() -> void:
|
||||||
|
held_item = null
|
||||||
|
amount = 0
|
||||||
|
|
||||||
|
func is_valid() -> bool:
|
||||||
|
return held_item != null or amount > 0
|
||||||
|
|
|
@ -40,14 +40,14 @@ func add(stack: Stack) -> Stack:
|
||||||
var found_index : int = find(stack.held_item)
|
var found_index : int = find(stack.held_item)
|
||||||
if found_index != -1:
|
if found_index != -1:
|
||||||
stack_added.emit(stack,found_index)
|
stack_added.emit(stack,found_index)
|
||||||
stack = internal_array[found_index].merge_stack(stack)
|
internal_array[found_index].merge_stack(stack)
|
||||||
if stack == null:
|
if stack.is_valid() == false:
|
||||||
return null
|
return null
|
||||||
for i in range(len(internal_array)):
|
for i in range(len(internal_array)):
|
||||||
if internal_array[i].held_item == null or internal_array[i].held_item == stack.held_item:
|
if internal_array[i].held_item == null or internal_array[i].held_item == stack.held_item:
|
||||||
stack_added.emit(stack,i)
|
stack_added.emit(stack,i)
|
||||||
stack = internal_array[i].merge_stack(stack)
|
internal_array[i].merge_stack(stack)
|
||||||
if stack == null:
|
if stack.is_valid() == false:
|
||||||
return null
|
return null
|
||||||
return stack
|
return stack
|
||||||
|
|
||||||
|
|
|
@ -19,12 +19,7 @@ func _process(delta: float) -> void:
|
||||||
inventory.advance(delta)
|
inventory.advance(delta)
|
||||||
queue_redraw()
|
queue_redraw()
|
||||||
var next : Structure = get_next()
|
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:
|
try_transfer(next)
|
||||||
return
|
|
||||||
var popped = inventory.pop()
|
|
||||||
if popped == null:
|
|
||||||
return
|
|
||||||
next.inventory.add_from_side(popped,Globals.facing_difference(next.facing,structure_parent.facing))
|
|
||||||
|
|
||||||
func calculate_position(index: int) -> Vector2:
|
func calculate_position(index: int) -> Vector2:
|
||||||
var indexed_part = 16.0 / inventory.capacity
|
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)
|
var faced_vector = Structure.facing_to_vector(structure_parent.facing)
|
||||||
return structure_parent.get_relative(faced_vector)
|
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:
|
func _on_conveyor_switched_facing(to: Structure.Facing) -> void:
|
||||||
match to:
|
match to:
|
||||||
Structure.Facing.RIGHT:
|
Structure.Facing.RIGHT:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue