Updated RecipesDB to store an array of tem_type resources instead of hardcoded slots
This commit is contained in:
1 parent
a336411c84
commit
46817025a6
5 files changed
+22
-23
No files matched your search
+1
-2
@@ -1,6 +1,5 @@
|
||||
extends Resource
|
||||
class_name Recipe
|
||||
|
||||
export var item_type_in_1: Resource
|
||||
export var item_type_in_2: Resource
|
||||
export(Array, Resource) var item_type_slots
|
||||
export var item_type_out: Resource
|
||||
+13
-15
@@ -3,28 +3,26 @@ 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)
|
||||
func find_recipe(in_item_types) -> Resource:
|
||||
assert(in_item_types.size() != 0)
|
||||
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 in_item_type in in_item_types:
|
||||
if in_items.has(in_item_type):
|
||||
in_items[in_item_type] += 1
|
||||
else:
|
||||
in_items[in_item_type] = 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:
|
||||
if recipe.item_type_out == null or recipe.item_type_slots.size() == 0:
|
||||
continue
|
||||
|
||||
var items = [recipe.item_type_in_1, recipe.item_type_in_2]
|
||||
var items = recipe.item_type_slots
|
||||
# build cost map
|
||||
var total_cost = {}
|
||||
for n in 2:
|
||||
if total_cost.has(items[n]):
|
||||
total_cost[items[n]] += 1
|
||||
for item_type in items:
|
||||
if total_cost.has(item_type):
|
||||
total_cost[item_type] += 1
|
||||
else:
|
||||
total_cost[items[n]] = 1
|
||||
total_cost[item_type] = 1
|
||||
|
||||
# check if you can afford the cost
|
||||
var cost_met : bool = true
|
||||
|
||||
@@ -7,7 +7,8 @@ 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)
|
||||
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)
|
||||
if crafted_item == null:
|
||||
return
|
||||
$slot_1.destroy_item()
|
||||
|
||||
Reference in new issue
Block a user