Craft
This commit is contained in:
parent
3af13d0a8b
commit
5da9e11f36
5 changed files with 48 additions and 26 deletions
|
@ -4,16 +4,41 @@ extends StructureBehaviour
|
|||
@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)
|
||||
await get_tree().process_frame
|
||||
for i in len(selected_recipe.ingridients):
|
||||
inventory.input_array[i].filter = selected_recipe.ingridients[i].item
|
||||
print(inventory.input_array[i].filter)
|
||||
|
||||
func switch_recipe(recipe: Recipe) -> void:
|
||||
selected_recipe = recipe
|
||||
inventory.input_capacity = len(selected_recipe.ingridients)
|
||||
inventory.resize()
|
||||
for i in len(selected_recipe.ingridients):
|
||||
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:
|
||||
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))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue