Combinable Items

This commit is contained in:
algodoogle
2026-07-14 17:47:47 +01:00
parent 8845a519c5
commit 1454303a80
8 changed files with 175 additions and 0 deletions
+15
View File
@@ -6,12 +6,17 @@
[ext_resource type="Script" uid="uid://dvobm6vcfnqe8" path="res://addons/godot-xr-tools/hands/poses/hand_pose_settings.gd" id="4_mgacb"]
[ext_resource type="PackedScene" uid="uid://ctw7nbntd5pcj" path="res://addons/godot-xr-tools/objects/grab_points/grab_point_hand_right.tscn" id="5_vw7i6"]
[ext_resource type="Animation" uid="uid://d1xnpyc08njjx" path="res://addons/godot-xr-tools/hands/animations/right/Grip 4.res" id="6_cp3eg"]
[ext_resource type="Script" path="res://combinable_item.gd" id="7_comb"]
[ext_resource type="Script" path="res://combine_zone.gd" id="8_zone"]
[sub_resource type="CylinderShape3D" id="CylinderShape3D_gkni8"]
custom_solver_bias = 0.1
height = 0.1
radius = 0.1
[sub_resource type="BoxShape3D" id="BoxShape3D_combine"]
size = Vector3(0.16, 0.16, 0.16)
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_e28iu"]
albedo_color = Color(0.06879827, 0.06879828, 0.06879828, 1)
@@ -46,3 +51,13 @@ hand_pose = SubResource("Resource_lc22d")
[node name="GrabPointHandRight" parent="." index="3" unique_id=514404634 instance=ExtResource("5_vw7i6")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.06531982, 0.042291984, -0.08604182)
hand_pose = SubResource("Resource_qyiot")
[node name="CombinableItem" type="Node" parent="." index="4"]
script = ExtResource("7_comb")
id = &"charcoal"
[node name="CombineZone" type="Area3D" parent="." index="5"]
script = ExtResource("8_zone")
[node name="CollisionShape3D" type="CollisionShape3D" parent="CombineZone"]
shape = SubResource("BoxShape3D_combine")
+22
View File
@@ -8,6 +8,9 @@
[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="Script" uid="uid://bs7cxydoxk1cx" path="res://cookable_item.gd" id="7_hc3f7"]
[ext_resource type="PackedScene" uid="uid://cucc3gaqu2nab" path="res://Charcoal.tscn" id="8_a0a5b"]
[ext_resource type="Script" uid="uid://dounic663dn5q" path="res://combinable_item.gd" id="9_comb"]
[ext_resource type="Script" uid="uid://e3im02nq5cye" path="res://combine_zone.gd" id="10_zone"]
[ext_resource type="Script" uid="uid://coidnv8b2yvxr" path="res://combine_recipe.gd" id="11_recipe"]
[sub_resource type="BoxShape3D" id="BoxShape3D_m1xbq"]
size = Vector3(0.1, 0.1, 0.1)
@@ -29,6 +32,14 @@ script = ExtResource("4_hc3f7")
closed_pose = ExtResource("6_bdp75")
metadata/_custom_type_script = "uid://dvobm6vcfnqe8"
[sub_resource type="Resource" id="Resource_recipe_cube"]
script = ExtResource("11_recipe")
ingredient_id = &"cube"
result = ExtResource("8_a0a5b")
[sub_resource type="BoxShape3D" id="BoxShape3D_combine"]
size = Vector3(0.16, 0.16, 0.16)
[node name="PickableObject" unique_id=1675596942 instance=ExtResource("1_dbtw8")]
[node name="CollisionShape3D" parent="." index="0"]
@@ -49,3 +60,14 @@ hand_pose = SubResource("Resource_qyiot")
script = ExtResource("7_hc3f7")
cooking_time = 2.0
turns_into = ExtResource("8_a0a5b")
[node name="CombinableItem" type="Node" parent="." index="5" unique_id=1265358312]
script = ExtResource("9_comb")
id = &"cube"
recipes = Array[ExtResource("11_recipe")]([SubResource("Resource_recipe_cube")])
[node name="CombineZone" type="Area3D" parent="." index="6" unique_id=1412308162]
script = ExtResource("10_zone")
[node name="CollisionShape3D" type="CollisionShape3D" parent="CombineZone" index="0" unique_id=1926800015]
shape = SubResource("BoxShape3D_combine")
+24
View File
@@ -0,0 +1,24 @@
class_name CombinableItem
extends Node
## Identity of this item, referenced by other items' recipes.
@export var id: StringName
## Recipes describing what the item holding this component turns into when
## another item is combined into it while snapped. Recipes are directional:
## only the snapped (base) item's recipes are consulted.
@export var recipes: Array[CombineRecipe] = []
func _ready() -> void:
if id.is_empty():
push_error("CombinableItem on ", get_parent().name, " has no 'id' set.")
## Returns the scene this item becomes when combined with [param other_id],
## or null if there is no matching recipe.
func get_result_for(other_id: StringName) -> PackedScene:
for recipe in recipes:
if recipe and recipe.ingredient_id == other_id:
return recipe.result
return null
+1
View File
@@ -0,0 +1 @@
uid://dounic663dn5q
+8
View File
@@ -0,0 +1,8 @@
class_name CombineRecipe
extends Resource
## The [CombinableItem.id] of the item that must be combined into the base item.
@export var ingredient_id: StringName
## The scene the base item turns into when [member ingredient_id] is combined in.
@export var result: PackedScene
+1
View File
@@ -0,0 +1 @@
uid://coidnv8b2yvxr
+103
View File
@@ -0,0 +1,103 @@
class_name CombineZone
extends Area3D
## Trigger area that lets a snapped item combine with another item pushed into
## it. The zone only monitors while its owning item is held by an
## [XRToolsSnapZone]; when an item carrying a matching [CombinableItem.id]
## enters, the snapped (base) item transforms into the recipe result and the
## incoming item is consumed.
# The pickable this zone belongs to (its parent).
@onready var _item: XRToolsPickable = get_parent() as XRToolsPickable
# The base item's recipe data.
@onready var _combinable: CombinableItem = _find_combinable(_item)
# Guard so a combine only fires once.
var _combining: bool = false
func _ready() -> void:
# Detect items whether held (layer 17 "Held Objects") or loose
# (layer 3 "Pickable Objects"). Don't advertise a layer of our own.
collision_mask = 0b0000_0000_0000_0001_0000_0000_0000_0100
collision_layer = 0
# Deferred: _ready can run during a physics signal flush (when a combine
# spawns the result), where toggling monitoring directly is forbidden.
set_deferred("monitoring", false)
set_deferred("monitorable", false)
if not _item:
push_error("CombineZone must be a child of an XRToolsPickable.")
return
if not _combinable:
push_error("CombineZone requires a CombinableItem sibling on ", _item.name, ".")
return
_item.picked_up.connect(_on_item_picked_up)
_item.dropped.connect(_on_item_dropped)
body_entered.connect(_on_body_entered)
# Enable the trigger only while snapped into a snap zone (not hand-held).
func _on_item_picked_up(_pickable: Node3D) -> void:
var by := _item.get_picked_up_by()
var snapped: bool = by != null and by.has_method("is_xr_class") and by.is_xr_class("XRToolsSnapZone")
set_deferred("monitoring", snapped)
func _on_item_dropped(_pickable: Node3D) -> void:
set_deferred("monitoring", false)
func _on_body_entered(body: Node3D) -> void:
if _combining or body == _item:
return
var other := _find_combinable(body)
if not other:
return
var result: PackedScene = _combinable.get_result_for(other.id)
if not result:
return
_combine(body, result)
# Transform the base item into [param result], consuming the incoming item.
func _combine(other_body: Node3D, result: PackedScene) -> void:
_combining = true
var snap_zone := _item.get_picked_up_by()
if not snap_zone or not snap_zone.has_method("pick_up_object"):
push_warning("CombineZone: base item is not held by a snap zone; cannot combine.")
_combining = false
return
# Spawn the result at the base item's location, in world space.
var base_transform := _item.global_transform
var result_instance: Node3D = result.instantiate()
_item.get_tree().current_scene.add_child(result_instance)
result_instance.global_transform = base_transform
# Free the base item and consume the incoming item.
snap_zone.drop_object()
_item.queue_free()
if other_body.has_method("drop_and_free"):
other_body.drop_and_free()
else:
other_body.queue_free()
# Snap the result into the now-empty zone.
snap_zone.pick_up_object(result_instance)
# Find a CombinableItem among the direct children of [param node].
func _find_combinable(node: Node) -> CombinableItem:
if not node:
return null
for child in node.get_children():
if child is CombinableItem:
return child
return null
+1
View File
@@ -0,0 +1 @@
uid://e3im02nq5cye