42 lines
1.0 KiB
GDScript
42 lines
1.0 KiB
GDScript
extends Resource
|
|
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:
|
|
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
|