35 lines
1.5 KiB
GDScript
35 lines
1.5 KiB
GDScript
extends StructureBehaviour
|
|
|
|
@onready var inventory : BeltInventory = structure_parent.inventory
|
|
|
|
func _draw() -> void:
|
|
for i in range(inventory.capacity):
|
|
if inventory.internal_array[i].amount > 0:
|
|
var calculated_position = calculate_position(i) - inventory.internal_array[i].held_item.preview.get_size()/2.0
|
|
draw_texture(inventory.internal_array[i].held_item.preview,calculated_position)
|
|
|
|
func _process(delta: float) -> void:
|
|
inventory.advance(delta)
|
|
queue_redraw()
|
|
var next : Structure = get_next()
|
|
try_transfer(next)
|
|
|
|
func calculate_position(index: int) -> Vector2:
|
|
var indexed_part = 16.0 / inventory.capacity
|
|
|
|
return -structure_parent.direction_vector()*8 + structure_parent.direction_vector() * indexed_part * (index + inventory.progress_array[index])
|
|
|
|
func get_next() -> Structure:
|
|
return structure_parent.get_relative(structure_parent.direction_vector())
|
|
|
|
func try_transfer(structure : Structure) -> void:
|
|
if structure == null or inventory.internal_array[inventory.capacity-1].amount == 0 or inventory.progress_array[inventory.capacity-1] < inventory.pop_treshold:
|
|
return
|
|
var last_slot = inventory.internal_array[inventory.capacity-1]
|
|
var transfer_context : InventoryContext = InventoryContext.new(structure_parent,structure,to_global(structure_parent.direction_vector()))
|
|
if structure.inventory.can_add(last_slot.held_item,transfer_context) == false:
|
|
return
|
|
last_slot.merge_stack(structure.inventory.add(last_slot.extract(),transfer_context))
|
|
|
|
if last_slot.amount == 0:
|
|
inventory.sort()
|