Added Crafting Machine
This commit is contained in:
1 parent
d20642e267
commit
a336411c84
5 files changed
+133
-13
No files matched your search
+34
-3
@@ -4,7 +4,38 @@ class_name RecipeDB
|
||||
export(Array, Resource) var recipes
|
||||
|
||||
func find_recipe(item_type_1: Resource, item_type_2: Resource) -> Resource:
|
||||
assert(item_type_1 != null)
|
||||
assert(item_type_2 != null)
|
||||
var in_items = {}
|
||||
if item_type_1 == item_type_2:
|
||||
in_items[item_type_1] = 2
|
||||
else:
|
||||
in_items[item_type_1] = 1
|
||||
in_items[item_type_2] = 1
|
||||
|
||||
for recipe in recipes:
|
||||
pass
|
||||
# TODO, find recipe
|
||||
return recipes[0] # This is just stubbed; remove later
|
||||
if recipe.item_type_in_1 == null or recipe.item_type_in_2 == null or recipe.item_type_out == null:
|
||||
continue
|
||||
|
||||
var items = [recipe.item_type_in_1, recipe.item_type_in_2]
|
||||
# build cost map
|
||||
var total_cost = {}
|
||||
for n in 2:
|
||||
if total_cost.has(items[n]):
|
||||
total_cost[items[n]] += 1
|
||||
else:
|
||||
total_cost[items[n]] = 1
|
||||
|
||||
# check if you can afford the cost
|
||||
var cost_met : bool = true
|
||||
for item_type in total_cost.keys():
|
||||
if not in_items.has(item_type):
|
||||
cost_met = false
|
||||
break
|
||||
if in_items[item_type] < total_cost[item_type]:
|
||||
cost_met = false
|
||||
break
|
||||
|
||||
if cost_met:
|
||||
return recipe.item_type_out
|
||||
return null
|
||||
@@ -0,0 +1,40 @@
|
||||
extends Spatial
|
||||
|
||||
export var recipes: Resource
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
assert(recipes != null)
|
||||
|
||||
func craft_item():
|
||||
var crafted_item = recipes.find_recipe($slot_1.item_in_hold.item_type, $slot_2.item_in_hold.item_type)
|
||||
if crafted_item == null:
|
||||
return
|
||||
$slot_1.destroy_item()
|
||||
$slot_2.destroy_item()
|
||||
$craft_item_holder.spawn_item(crafted_item)
|
||||
|
||||
func _on_coin_machine_coin_requirement_met(player):
|
||||
if not $slot_1.has_item():
|
||||
print("Slot 1 item missing")
|
||||
return
|
||||
if not $slot_2.has_item():
|
||||
print("Slot 2 item missing")
|
||||
return
|
||||
craft_item()
|
||||
|
||||
func should_enable_coin_machine():
|
||||
if $craft_item_holder.has_item():
|
||||
return false
|
||||
if not $slot_1.has_item() or not $slot_2.has_item():
|
||||
return false
|
||||
return true
|
||||
|
||||
func _on_craft_item_holder_item_changed(item):
|
||||
$coin_machine.enabled = should_enable_coin_machine()
|
||||
|
||||
func _on_slot_1_item_changed(item):
|
||||
$coin_machine.enabled = should_enable_coin_machine()
|
||||
|
||||
func _on_slot_2_item_changed(item):
|
||||
$coin_machine.enabled = should_enable_coin_machine()
|
||||
Reference in new issue
Block a user