Reafactoring Recipemanager
This commit is contained in:
@@ -60,7 +60,7 @@ func _on_body_entered(body: Node3D) -> void:
|
|||||||
print("CombinableItem _on_body_entered: other is not a foodItem")
|
print("CombinableItem _on_body_entered: other is not a foodItem")
|
||||||
return
|
return
|
||||||
|
|
||||||
var result: PackedScene = RecipeManager.get_combination(_food_item.id, other.id)
|
var result: PackedScene = RECIPE_MANAGER.get_combination_result(_food_item.id, other.id)
|
||||||
if not result:
|
if not result:
|
||||||
print("CombinableItem _on_body_entered: There is no recipe for %s + %s" % [_food_item.id, other.id])
|
print("CombinableItem _on_body_entered: There is no recipe for %s + %s" % [_food_item.id, other.id])
|
||||||
return
|
return
|
||||||
|
|||||||
+47
-4
@@ -1,8 +1,9 @@
|
|||||||
class_name RecipeManager
|
class_name RecipeManager
|
||||||
extends Node
|
extends Node
|
||||||
|
|
||||||
static var _recipes: Dictionary = {}
|
static var _recipes: Dictionary
|
||||||
static var _combining_map: Dictionary = {}
|
static var _combining_map: Dictionary
|
||||||
|
static var _cooking_map: Dictionary
|
||||||
static var _scene_paths: Dictionary
|
static var _scene_paths: Dictionary
|
||||||
static var _loaded: bool = false
|
static var _loaded: bool = false
|
||||||
|
|
||||||
@@ -26,10 +27,11 @@ static func load_recipes() -> void:
|
|||||||
_recipes = data
|
_recipes = data
|
||||||
_build_scene_paths()
|
_build_scene_paths()
|
||||||
_build_combining_map()
|
_build_combining_map()
|
||||||
|
_build_cooking_map()
|
||||||
_loaded = true
|
_loaded = true
|
||||||
|
|
||||||
|
|
||||||
static func get_combination(first_id: StringName, second_id: StringName) -> PackedScene:
|
static func get_combination_result(first_id: StringName, second_id: StringName) -> PackedScene:
|
||||||
load_recipes()
|
load_recipes()
|
||||||
var result_id = _combining_map.get(_make_pair_key(first_id, second_id))
|
var result_id = _combining_map.get(_make_pair_key(first_id, second_id))
|
||||||
if result_id == null:
|
if result_id == null:
|
||||||
@@ -37,6 +39,15 @@ static func get_combination(first_id: StringName, second_id: StringName) -> Pack
|
|||||||
return get_item_scene(result_id)
|
return get_item_scene(result_id)
|
||||||
|
|
||||||
|
|
||||||
|
static func get_cooking_result(ingredient_id: StringName) -> Dictionary:
|
||||||
|
load_recipes()
|
||||||
|
for cooked_id in _cooking_map.keys():
|
||||||
|
var cook_def = _cooking_map[cooked_id]
|
||||||
|
if cook_def["ingredient"] == ingredient_id:
|
||||||
|
return {"cooked_id": cooked_id, "time": cook_def["time"]}
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
static func get_item_scene(item_id: StringName) -> PackedScene:
|
static func get_item_scene(item_id: StringName) -> PackedScene:
|
||||||
load_recipes()
|
load_recipes()
|
||||||
var scene_path = _scene_paths.get(str(item_id), "")
|
var scene_path = _scene_paths.get(str(item_id), "")
|
||||||
@@ -78,7 +89,7 @@ static func print_all_recipes() -> void:
|
|||||||
if typeof(recipe) == TYPE_ARRAY and recipe.size() == 2:
|
if typeof(recipe) == TYPE_ARRAY and recipe.size() == 2:
|
||||||
var first = str(recipe[0])
|
var first = str(recipe[0])
|
||||||
var second = str(recipe[1])
|
var second = str(recipe[1])
|
||||||
print("%s + %s -> %s" % [first, second, result_id])
|
print("%s <- %s + %s" % [result_id, first, second])
|
||||||
else:
|
else:
|
||||||
print("RecipeManager: invalid combining recipe for '%s'" % result_id)
|
print("RecipeManager: invalid combining recipe for '%s'" % result_id)
|
||||||
|
|
||||||
@@ -113,6 +124,10 @@ static func _get_recipe_entries(recipe_value: Variant) -> Array:
|
|||||||
return recipes
|
return recipes
|
||||||
|
|
||||||
|
|
||||||
|
# Reads strucure into _combining_map:
|
||||||
|
# combining:
|
||||||
|
# hamburger:
|
||||||
|
# - [cooked_burger, burger_buns]
|
||||||
static func _build_combining_map() -> void:
|
static func _build_combining_map() -> void:
|
||||||
_combining_map.clear()
|
_combining_map.clear()
|
||||||
var combining = _recipes.get("combining", {})
|
var combining = _recipes.get("combining", {})
|
||||||
@@ -132,3 +147,31 @@ static func _make_pair_key(first_id: StringName, second_id: StringName) -> Strin
|
|||||||
if str(first_id) <= str(second_id):
|
if str(first_id) <= str(second_id):
|
||||||
return StringName(str(first_id) + "|" + str(second_id))
|
return StringName(str(first_id) + "|" + str(second_id))
|
||||||
return StringName(str(second_id) + "|" + str(first_id))
|
return StringName(str(second_id) + "|" + str(first_id))
|
||||||
|
|
||||||
|
|
||||||
|
# Reads structure into _cooking_map:
|
||||||
|
# cooking:
|
||||||
|
# cooked_burger:
|
||||||
|
# ingredient: raw_burger
|
||||||
|
# time: 3
|
||||||
|
static func _build_cooking_map() -> void:
|
||||||
|
_cooking_map.clear()
|
||||||
|
var cooking = _recipes.get("cooking", {})
|
||||||
|
if typeof(cooking) != TYPE_DICTIONARY:
|
||||||
|
return
|
||||||
|
for cooked_id in cooking.keys():
|
||||||
|
var cook_def = cooking[cooked_id]
|
||||||
|
if typeof(cook_def) != TYPE_DICTIONARY:
|
||||||
|
push_error("RecipeManager: cooking recipe for '%s' must be a dictionary" % cooked_id)
|
||||||
|
continue
|
||||||
|
var ingredient = cook_def.get("ingredient", "")
|
||||||
|
if typeof(ingredient) != TYPE_STRING or str(ingredient).is_empty():
|
||||||
|
push_error("RecipeManager: cooking recipe '%s' missing a valid 'ingredient' field" % cooked_id)
|
||||||
|
continue
|
||||||
|
var time_val = cook_def.get("time", null)
|
||||||
|
if typeof(time_val) != TYPE_INT and typeof(time_val) != TYPE_FLOAT:
|
||||||
|
push_error("RecipeManager: cooking recipe '%s' missing a valid 'time' field" % cooked_id)
|
||||||
|
continue
|
||||||
|
# store cooked item -> { ingredient: StringName, time: float }
|
||||||
|
_cooking_map[StringName(cooked_id)] = {"ingredient": StringName(str(ingredient)), "time": float(time_val)}
|
||||||
|
|
||||||
@@ -17,3 +17,13 @@ combining:
|
|||||||
charcoal:
|
charcoal:
|
||||||
- [cube, cube]
|
- [cube, cube]
|
||||||
|
|
||||||
|
cooking:
|
||||||
|
cooked_burger:
|
||||||
|
ingredient: raw_burger
|
||||||
|
time: 3
|
||||||
|
charcoal:
|
||||||
|
ingredient: cooked_burger
|
||||||
|
time: 4
|
||||||
|
charcoal:
|
||||||
|
ingredient: toast
|
||||||
|
time: 2
|
||||||
|
|||||||
Reference in New Issue
Block a user