Updated crafting machine code to check for valid recipe when deciding to enable/disable the coin machine

This commit is contained in:
akshay 2022-08-14 19:29:17 -04:00
parent 46817025a6
commit 01e57c169c
2 changed files with 11 additions and 6 deletions

View File

@ -1,11 +1,10 @@
[gd_resource type="Resource" load_steps=5 format=2]
[gd_resource type="Resource" load_steps=4 format=2]
[ext_resource path="res://scripts/Recipe.gd" type="Script" id=1]
[ext_resource path="res://item_types/ore.tres" type="Resource" id=2]
[ext_resource path="res://item_types/repair_kit.tres" type="Resource" id=3]
[ext_resource path="res://item_types/raw_yam.tres" type="Resource" id=4]
[resource]
script = ExtResource( 1 )
item_type_slots = [ ExtResource( 2 ), ExtResource( 4 ) ]
item_type_slots = [ ExtResource( 2 ), ExtResource( 2 ) ]
item_type_out = ExtResource( 3 )

View File

@ -6,9 +6,15 @@ export var recipes: Resource
func _ready():
assert(recipes != null)
func craft_item():
func get_craft_recipe_item():
var items_in_slots = [$slot_1.item_in_hold.item_type, $slot_2.item_in_hold.item_type]
var crafted_item = recipes.find_recipe(items_in_slots)
return recipes.find_recipe(items_in_slots)
func can_craft_recipe():
return get_craft_recipe_item() != null
func craft_item():
var crafted_item = get_craft_recipe_item()
if crafted_item == null:
return
$slot_1.destroy_item()
@ -29,7 +35,7 @@ func should_enable_coin_machine():
return false
if not $slot_1.has_item() or not $slot_2.has_item():
return false
return true
return can_craft_recipe()
func _on_craft_item_holder_item_changed(item):
$coin_machine.enabled = should_enable_coin_machine()