From a48611c0ff5e325ba005ae2ad7cafd0d1162706f Mon Sep 17 00:00:00 2001 From: JonShard Date: Sun, 19 Jul 2026 10:34:01 +0200 Subject: [PATCH] Refactor hob to use Recipemanager --- Items/PickupCube.tscn | 7 +--- Items/burger.tscn | 10 ++--- Items/cooked_burger.tscn | 7 +--- Prefabs/combinable_item.gd | 1 - Prefabs/food_item.tscn | 3 +- RecipeManager.gd | 31 ++++++++++----- Stations/hob.gd | 79 +++++++++++++++++++++----------------- recipes.yaml | 7 +++- 8 files changed, 81 insertions(+), 64 deletions(-) diff --git a/Items/PickupCube.tscn b/Items/PickupCube.tscn index b1812d2..91287d6 100644 --- a/Items/PickupCube.tscn +++ b/Items/PickupCube.tscn @@ -7,7 +7,6 @@ [ext_resource type="PackedScene" uid="uid://ctw7nbntd5pcj" path="res://addons/godot-xr-tools/objects/grab_points/grab_point_hand_right.tscn" id="5_a0a5b"] [ext_resource type="Animation" uid="uid://d1xnpyc08njjx" path="res://addons/godot-xr-tools/hands/animations/right/Grip 4.res" id="6_bdp75"] [ext_resource type="PackedScene" uid="uid://3lr2dhy62rhk" path="res://Prefabs/combinable_item.tscn" id="7_bdp75"] -[ext_resource type="PackedScene" uid="uid://7earnjgdmwgx" path="res://Prefabs/cookable_item.tscn" id="10_65ccv"] [ext_resource type="PackedScene" uid="uid://bfj80jh13t6e5" path="res://Prefabs/food_item.tscn" id="11_vjw41"] [sub_resource type="BoxShape3D" id="BoxShape3D_m1xbq"] @@ -46,10 +45,8 @@ hand_pose = SubResource("Resource_lc22d") transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.06531982, 0.042291984, -0.08604182) hand_pose = SubResource("Resource_qyiot") -[node name="CookableItem" parent="." index="4" unique_id=280820828 instance=ExtResource("10_65ccv")] - -[node name="FoodItem" parent="." index="5" unique_id=63948206 instance=ExtResource("11_vjw41")] +[node name="FoodItem" parent="." index="4" unique_id=63948206 instance=ExtResource("11_vjw41")] id = "cube" type = 1 -[node name="CombinableItem" parent="." index="6" unique_id=996936271 instance=ExtResource("7_bdp75")] +[node name="CombinableItem" parent="." index="5" unique_id=996936271 instance=ExtResource("7_bdp75")] diff --git a/Items/burger.tscn b/Items/burger.tscn index 32b1e23..cdaff38 100644 --- a/Items/burger.tscn +++ b/Items/burger.tscn @@ -7,8 +7,7 @@ [ext_resource type="Script" uid="uid://dvobm6vcfnqe8" path="res://addons/godot-xr-tools/hands/poses/hand_pose_settings.gd" id="5_w8sii"] [ext_resource type="PackedScene" uid="uid://ctw7nbntd5pcj" path="res://addons/godot-xr-tools/objects/grab_points/grab_point_hand_right.tscn" id="6_mvnl5"] [ext_resource type="Animation" uid="uid://d1xnpyc08njjx" path="res://addons/godot-xr-tools/hands/animations/right/Grip 4.res" id="7_wqxjj"] -[ext_resource type="PackedScene" uid="uid://7earnjgdmwgx" path="res://Prefabs/cookable_item.tscn" id="9_hwyyd"] -[ext_resource type="PackedScene" uid="uid://cfc4ho67u4r5e" path="res://Items/cooked_burger.tscn" id="9_r8jvx"] +[ext_resource type="PackedScene" uid="uid://bfj80jh13t6e5" path="res://Prefabs/food_item.tscn" id="10_yxtxs"] [sub_resource type="CylinderShape3D" id="CylinderShape3D_fco8w"] height = 0.0338974 @@ -27,7 +26,7 @@ script = ExtResource("5_w8sii") closed_pose = ExtResource("7_wqxjj") metadata/_custom_type_script = "uid://dvobm6vcfnqe8" -[node name="burger" unique_id=1675596942 instance=ExtResource("1_fco8w")] +[node name="raw_burger" unique_id=1675596942 instance=ExtResource("1_fco8w")] [node name="CollisionShape3D" parent="." index="0"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.00077831745, 0) @@ -47,5 +46,6 @@ hand_pose = SubResource("Resource_lc22d") transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.06531982, 0.042291984, -0.08604182) hand_pose = SubResource("Resource_qyiot") -[node name="CookableItem" parent="." index="4" unique_id=280820828 instance=ExtResource("9_hwyyd")] -turns_into = ExtResource("9_r8jvx") +[node name="FoodItem" parent="." index="4" unique_id=63948206 instance=ExtResource("10_yxtxs")] +id = "raw_burger" +type = 2 diff --git a/Items/cooked_burger.tscn b/Items/cooked_burger.tscn index a9612cb..59941aa 100644 --- a/Items/cooked_burger.tscn +++ b/Items/cooked_burger.tscn @@ -8,7 +8,6 @@ [ext_resource type="Animation" uid="uid://d1xnpyc08njjx" path="res://addons/godot-xr-tools/hands/animations/right/Grip 4.res" id="7_rl64h"] [ext_resource type="PackedScene" uid="uid://3lr2dhy62rhk" path="res://Prefabs/combinable_item.tscn" id="10_ut7mg"] [ext_resource type="PackedScene" uid="uid://bfj80jh13t6e5" path="res://Prefabs/food_item.tscn" id="10_zz42p"] -[ext_resource type="PackedScene" uid="uid://7earnjgdmwgx" path="res://Prefabs/cookable_item.tscn" id="11_5ch2b"] [sub_resource type="CylinderShape3D" id="CylinderShape3D_fco8w"] height = 0.04777527 @@ -47,10 +46,8 @@ hand_pose = SubResource("Resource_lc22d") transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.06531982, 0.042291984, -0.08604182) hand_pose = SubResource("Resource_qyiot") -[node name="CookableItem" parent="." index="4" unique_id=280820828 instance=ExtResource("11_5ch2b")] - -[node name="FoodItem" parent="." index="5" unique_id=234567891 instance=ExtResource("10_zz42p")] +[node name="FoodItem" parent="." index="4" unique_id=234567891 instance=ExtResource("10_zz42p")] id = "cooked_burger" type = 2 -[node name="CombinableItem" parent="." index="6" unique_id=996936271 instance=ExtResource("10_ut7mg")] +[node name="CombinableItem" parent="." index="5" unique_id=996936271 instance=ExtResource("10_ut7mg")] diff --git a/Prefabs/combinable_item.gd b/Prefabs/combinable_item.gd index 7ac0a5a..9669aa9 100644 --- a/Prefabs/combinable_item.gd +++ b/Prefabs/combinable_item.gd @@ -50,7 +50,6 @@ func _on_item_dropped(_item: Node3D) -> void: # If the other body is a FoodItem, check for a recipe and combine if one exists. func _on_body_entered(body: Node3D) -> void: - print("CombinableItem _on_body_entered") if _combining or body == _pickable: print("CombinableItem _on_body_entered: other is our own pickable") return diff --git a/Prefabs/food_item.tscn b/Prefabs/food_item.tscn index 489b0c9..09a8a42 100644 --- a/Prefabs/food_item.tscn +++ b/Prefabs/food_item.tscn @@ -4,5 +4,4 @@ [node name="FoodItem" type="Node" unique_id=63948206] script = ExtResource("1_0na5k") -id = "hamburger" -item_type = "Meal" +id = "some_food_id" diff --git a/RecipeManager.gd b/RecipeManager.gd index e51f994..adcece0 100644 --- a/RecipeManager.gd +++ b/RecipeManager.gd @@ -45,13 +45,22 @@ static func get_combination_result(first_id: StringName, second_id: StringName) return get_item_scene(result_id) -static func get_cooking_result(ingredient_id: StringName) -> Dictionary: +static func get_cooking_result(ingredient_id: StringName) -> PackedScene: 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 {} + return get_item_scene(cooked_id) + return null + + +static func get_cooking_time(ingredient_id: StringName) -> float: + load_recipes() + for cooked_id in _cooking_map.keys(): + var cook_def = _cooking_map[cooked_id] + if cook_def["ingredient"] == ingredient_id: + return cook_def["time"] + return 0.0 static func get_item_scene(item_id: StringName) -> PackedScene: @@ -78,11 +87,15 @@ static func print_all_recipes() -> void: print("RecipeManager: no combining recipes found") return - print("RecipeManager: Loaded recipes") + print("#### RecipeManager: Loaded recipes ####") _print_combining_recipes() + print("") _print_cooking_recipes() + print("") _print_chopping_recipes() + print("") _print_rolling_recipes() + print("") _print_augmenting_recipes() static func _print_combining_recipes() -> void: @@ -96,25 +109,25 @@ static func _print_combining_recipes() -> void: var first = key_str.substr(0, separator_index) var second = key_str.substr(separator_index + 1, key_str.length() - separator_index - 1) - print("%s <-combine-- %s + %s" % [result_id, first, second]) + print(" %s <-combine-- %s + %s" % [result_id, first, second]) static func _print_cooking_recipes() -> void: for cooked_id in _cooking_map.keys(): var cook_def = _cooking_map[cooked_id] - print("%s <-cook-- %s - time: %.1f" % [cooked_id, cook_def["ingredient"], cook_def["time"]]) + print(" %s <-cook-- %s - time: %.1f" % [cooked_id, cook_def["ingredient"], cook_def["time"]]) static func _print_chopping_recipes() -> void: for chopped_id in _chopping_map.keys(): var chop_def = _chopping_map[chopped_id] - print("%s <-chop-- %s - work: %.1f" % [chopped_id, chop_def["ingredient"], chop_def["work"]]) + print(" %s <-chop-- %s - work: %.1f" % [chopped_id, chop_def["ingredient"], chop_def["work"]]) static func _print_rolling_recipes() -> void: for rolled_id in _rolling_map.keys(): var roll_def = _rolling_map[rolled_id] - print("%s <-roll-- %s - work: %.1f" % [rolled_id, roll_def["ingredient"], roll_def["work"]]) + print(" %s <-roll-- %s - work: %.1f" % [rolled_id, roll_def["ingredient"], roll_def["work"]]) static func _print_augmenting_recipes() -> void: @@ -122,7 +135,7 @@ static func _print_augmenting_recipes() -> void: var augment_def = _augmenting_map[target_id] for ingredient_id in augment_def.keys(): var attr_key = augment_def[ingredient_id] - print("%s <-augment-- %s adds attribute '%s'" % [target_id, ingredient_id, attr_key]) + print(" %s <-augment-- %s adds attribute '%s'" % [target_id, ingredient_id, attr_key]) static func _build_scene_paths() -> void: diff --git a/Stations/hob.gd b/Stations/hob.gd index c1467d4..f925bed 100644 --- a/Stations/hob.gd +++ b/Stations/hob.gd @@ -1,59 +1,68 @@ extends StaticBody3D +const RECIPE_MANAGER = preload("res://RecipeManager.gd") @export var cook_speed = 1 @onready var snap_zone: XRToolsSnapZone = $XRToolsSnapZone -var item = null -var progress = 0 +var time_cooked = 0 +var cooking_result = null +var cooking_result_time = 0 -# Called when the node enters the scene tree for the first time. func _ready() -> void: - if snap_zone.initial_object: - item = snap_zone.initial_object - print("Hob has item: ", item) - else: - print("Hob is empty") + snap_zone.has_picked_up.connect(_on_object_picked_up) + snap_zone.has_dropped.connect(_on_object_dropped) -func convert_item(cookable: CookableItem) -> void: - print("Hob converting ", cookable) +func _on_object_picked_up(_item) -> void: + print("Hob: object picked up: ", _item) + + # Find the CookableItem in held object + var _food_item = _item.get_node_or_null("FoodItem") as FoodItem + if not _food_item: + print("Hob: held object is not a FoodItem") + return + var result = RECIPE_MANAGER.get_cooking_result(_food_item.id) + if not result: + print("Hob: held a FoodItem that is not cookable, id: ", _food_item.id) + return + + cooking_result = result + cooking_result_time = RECIPE_MANAGER.get_cooking_time(_food_item.id) + + +func _on_object_dropped() -> void: + print("Hob: object drop ") + time_cooked = 0 + cooking_result = null + cooking_result_time = 0 + + +func convert_held_to_item(_item: PackedScene) -> void: + print("Hob converting ", _item) var old_pickable = snap_zone.picked_up_object if not old_pickable: - push_warning("Hob finished cooking item, but snap zone is missing its reference") + push_warning("Hob finished cooking _item, but snap zone is missing its reference") return - print("picked_up_object ", snap_zone.picked_up_object) - + # Create new item as child of root in the same position var original_transform = old_pickable.global_transform - var new_scene_instance = cookable.turns_into.instantiate() - - # Add the new item to the hob's parent (so it lives in the main world space) - get_parent().add_child(new_scene_instance) + var new_scene_instance = _item.instantiate() + get_tree().get_root().add_child(new_scene_instance) new_scene_instance.global_transform = original_transform + # Drop and free the old item and pick up the new one + print("Hob freeing old_pickable ", old_pickable) snap_zone.drop_object() - print("old_pickable ", old_pickable) old_pickable.queue_free() snap_zone.pick_up_object(new_scene_instance) -# Called every frame. 'delta' is the elapsed time since the previous frame. +# If cooking something, progress cooking, when done, convert item func _process(delta: float) -> void: - # Find the CookableItem in held object - var cookable = null - if snap_zone.picked_up_object: - var matches = snap_zone.picked_up_object.get_children().filter(func(c): return c is CookableItem) - if matches.size() > 0: - cookable = matches[0] - - # Cook it if exists, then convert item after cooking - if cookable: - progress += cook_speed * delta - print("Cooking ", progress) - if progress >= cookable.cooking_time: - progress = 0 - convert_item(cookable) - else: - progress = 0 + if cooking_result: + time_cooked += cook_speed * delta + if time_cooked >= cooking_result_time: + time_cooked = 0 + convert_held_to_item(cooking_result) diff --git a/recipes.yaml b/recipes.yaml index 8f89cef..9a9eef4 100644 --- a/recipes.yaml +++ b/recipes.yaml @@ -21,12 +21,15 @@ cooking: cooked_burger: ingredient: raw_burger time: 3 - charcoal: + charcoal: ingredient: cooked_burger - time: 4 + time: 2 charcoal: ingredient: toast time: 2 + charcoal: + ingredient: cube + time: 1 chopping: salad: