Splitter
This commit is contained in:
parent
3c0777f4fd
commit
a26dd235a2
1 changed files with 31 additions and 0 deletions
|
@ -1 +1,32 @@
|
|||
extends StructureBehaviour
|
||||
|
||||
@onready var inventory: SplitterInventory = structure_parent.inventory
|
||||
var split: bool = true
|
||||
|
||||
func get_upper_next() -> Vector2:
|
||||
return Vector2.RIGHT.rotated(structure_parent.direction)
|
||||
|
||||
func get_down_next() -> Vector2:
|
||||
var base = get_upper_next()
|
||||
return base + base.rotated(PI/2).abs()
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
var transfer_upper = try_transfer(get_upper_next(),false)
|
||||
var transfer_down = try_transfer(get_down_next(),true)
|
||||
if transfer_upper or transfer_down or (not transfer_down and not transfer_upper):
|
||||
split = not split
|
||||
|
||||
func try_transfer(point:Vector2,down:bool) -> bool:
|
||||
var next :Structure = structure_parent.get_relative(point)
|
||||
if next == null:
|
||||
return false
|
||||
var array : Array[InventorySlot] = inventory.down_array if split != down else inventory.upper_array
|
||||
var context : InventoryContext = InventoryContext.new(structure_parent,next,to_global(point))
|
||||
for i in range(inventory.capacity):
|
||||
if array[i].amount > 0 and next.inventory.can_add(array[i].held_item,context):
|
||||
var stack = array[i].extract()
|
||||
stack = next.inventory.add(stack,context)
|
||||
array[i].merge_stack(stack)
|
||||
|
||||
return true
|
||||
return false
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue