delurbelako/scripts/structures/belt.gd
2025-10-17 18:22:49 +05:00

35 lines
1.4 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 _tick(current_tick: int) -> void:
queue_redraw()
if current_tick % 8 != 0:
return
inventory.refresh()
inventory.advance()
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
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:
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))