46 lines
1.9 KiB
GDScript
46 lines
1.9 KiB
GDScript
extends StructureBehaviour
|
|
|
|
@export var selected_recipe : Recipe
|
|
@onready var inventory : InOutInventory = get_parent().inventory
|
|
|
|
func _ready() -> void:
|
|
inventory.stack_added.connect(check_for_recipe)
|
|
await get_tree().process_frame
|
|
switch_recipe(selected_recipe)
|
|
for i in len(selected_recipe.ingridients):
|
|
inventory.input_array[i].filter = selected_recipe.ingridients[i].item
|
|
|
|
func switch_recipe(recipe: Recipe) -> void:
|
|
selected_recipe = recipe
|
|
inventory.input_capacity = len(selected_recipe.ingridients)
|
|
inventory.resize()
|
|
for i in range(len(selected_recipe.ingridients)):
|
|
inventory.input_array[i].filter = selected_recipe.ingridients[i].item
|
|
|
|
func _process(_delta: float) -> void:
|
|
if inventory.output_slot.amount <= 0:
|
|
return
|
|
var output : Structure = get_output_structure()
|
|
if output == null:
|
|
return
|
|
var ang_diff = Globals.facing_difference(output.facing,structure_parent.facing)
|
|
if output.inventory.can_add_from_side(ang_diff):
|
|
inventory.output_slot.merge_stack(output.inventory.add_from_side(inventory.output_slot.extract(),ang_diff))
|
|
|
|
func get_output_structure() -> Structure:
|
|
var facing_direction = Structure.facing_to_vector(structure_parent.facing)
|
|
var rotated = Vector2(0.5,1.5).rotated(-Vector2.DOWN.angle_to(facing_direction))
|
|
return structure_parent.get_relative(rotated+Vector2(0.5,0.5))
|
|
|
|
func check_for_recipe(_stack : Stack, _position : int) -> void:
|
|
if inventory.output_slot.held_item != null and inventory.output_slot.amount == inventory.output_slot.held_item.stack_size:
|
|
return
|
|
for i in range(len(selected_recipe.ingridients)):
|
|
if inventory.input_array[i].amount < selected_recipe.ingridients[i].amount:
|
|
return
|
|
craft()
|
|
|
|
func craft() -> void:
|
|
for i in range(len(selected_recipe.ingridients)):
|
|
inventory.input_array[i].extract_stack(selected_recipe.ingridients[i].amount)
|
|
inventory.output_slot.merge_stack(Stack.new(selected_recipe.result.item,selected_recipe.result.amount))
|