From b24a3f99eeae197df438a26139c8e3fdeab0472d Mon Sep 17 00:00:00 2001 From: JonShard Date: Sun, 2 Aug 2026 13:43:30 +0200 Subject: [PATCH] Add BuildModeController that deletes loose items on enter BuildMode --- Prefabs/build_mode_controller.gd | 38 ++++++++++++++++++++++++++++ Prefabs/build_mode_controller.gd.uid | 1 + Scenes/test_small.tscn | 4 +++ Stations/hatch.tscn | 2 +- global/GameManager.gd | 4 +-- project.godot | 1 + 6 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 Prefabs/build_mode_controller.gd create mode 100644 Prefabs/build_mode_controller.gd.uid diff --git a/Prefabs/build_mode_controller.gd b/Prefabs/build_mode_controller.gd new file mode 100644 index 0000000..97a0ede --- /dev/null +++ b/Prefabs/build_mode_controller.gd @@ -0,0 +1,38 @@ +extends Node +class_name BuildModeController + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + Signals.game_state_changed.connect(_on_game_state_changed) + + +func _on_game_state_changed(new_state: GameManager.GameState) -> void: + print("BuildModeController: game_state_changed to %s" % new_state) + if new_state == GameManager.GameState.BUILDING: + despawn_unheld_pickables() + + +# Despawn all food_items unless it's in a "persistent_inventory" snap_zone +func despawn_unheld_pickables(): + var root = get_tree().get_root() + var pickables: Array[Node] = [] + _collect_pickables(root, pickables) + for pickable in pickables: + if not Helper.find_food_item(pickable): + continue + var held_by: Node = pickable.get_picked_up_by() + if not held_by: + NetworkManager.despawn_item(pickable) + continue + if not held_by.is_in_group("persistent_inventory"): + held_by.get_parent().print_tree_pretty() + print("BuildModeController despawn_unheld_pickables held_by: %s, pickable: %s " % [held_by, pickable]) + NetworkManager.despawn_item(pickable) + + +func _collect_pickables(node: Node, out_array: Array) -> void: + if node is XRToolsPickable: + out_array.append(node) + for child in node.get_children(): + if child is Node: + _collect_pickables(child, out_array) diff --git a/Prefabs/build_mode_controller.gd.uid b/Prefabs/build_mode_controller.gd.uid new file mode 100644 index 0000000..a4cd9af --- /dev/null +++ b/Prefabs/build_mode_controller.gd.uid @@ -0,0 +1 @@ +uid://bsdpd18i1i17s diff --git a/Scenes/test_small.tscn b/Scenes/test_small.tscn index ec80731..9a63460 100644 --- a/Scenes/test_small.tscn +++ b/Scenes/test_small.tscn @@ -8,6 +8,7 @@ [ext_resource type="PackedScene" uid="uid://efaec6ymgabo" path="res://stations/Counter.tscn" id="7_jnxsa"] [ext_resource type="PackedScene" uid="uid://drwuhqb3pjtiy" path="res://items/potato.tscn" id="8_1qeqw"] [ext_resource type="PackedScene" uid="uid://dlw5geheupgpt" path="res://stations/hatch.tscn" id="8_atgwk"] +[ext_resource type="Script" uid="uid://bsdpd18i1i17s" path="res://prefabs/build_mode_controller.gd" id="9_atgwk"] [sub_resource type="Environment" id="Environment_bvwq1"] background_mode = 2 @@ -99,3 +100,6 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -1.2, 0, 0) [node name="Hatch2" parent="." unique_id=1755144529 instance=ExtResource("8_atgwk")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.6, 0, 0) + +[node name="BuildModeController" type="Node" parent="." unique_id=1876729190] +script = ExtResource("9_atgwk") diff --git a/Stations/hatch.tscn b/Stations/hatch.tscn index fc55b03..d5c415b 100644 --- a/Stations/hatch.tscn +++ b/Stations/hatch.tscn @@ -18,7 +18,7 @@ albedo_color = Color(0.38, 0.35162666, 0.3268, 1) [sub_resource type="BoxShape3D" id="BoxShape3D_vlqg6"] size = Vector3(0.6, 0.34027833, 0.6) -[node name="Hatch" type="StaticBody3D" unique_id=1341321603] +[node name="Hatch" type="StaticBody3D" unique_id=1341321603 groups=["persistent_inventory"]] [node name="XRToolsSnapZone" type="Area3D" parent="." unique_id=2093161322 groups=["station"]] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.2495586, 0) diff --git a/global/GameManager.gd b/global/GameManager.gd index 22db1f7..e67182b 100644 --- a/global/GameManager.gd +++ b/global/GameManager.gd @@ -17,6 +17,7 @@ static func _set_game_state(value: GameState): game_state = value Signals.game_state_changed.emit(value) + static func _restart(): print("restart Game") game_state = GameState.RUNNING @@ -45,6 +46,3 @@ static func game_over(): print("Game Over") game_state = GameState.GAME_OVER Signals.game_over.emit() - - - diff --git a/project.godot b/project.godot index 2e781f8..ce233c9 100644 --- a/project.godot +++ b/project.godot @@ -48,6 +48,7 @@ platalbe_item="Meals, sides, augments for meals" station="Hobs, Counters ..." table="all nodes with table script" chopping_tool="A pickable body that chops" +persistent_inventory="A station that keeps it's held items in build mode" [input]