Skip to content

Commit

Permalink
Properly assign owners of generated nodes (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
TitanNano committed Feb 13, 2024
1 parent 41853e6 commit 91f976a
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 31 deletions.
15 changes: 8 additions & 7 deletions native/src/scripts/spawner/car_spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ impl CarSpawner {
}

pub fn spawn_car(&mut self) {
if self.base.get_child_count() > 20 {
return;
}

let inst = self
.default_car
.as_ref()
Expand All @@ -50,9 +54,10 @@ impl CarSpawner {
self.road_network_path.to_variant(),
);
if let Some(mut parent) = self.base.get_parent() {
parent.add_child(inst.clone());
}
self.base
.add_child_ex(inst.clone())
.force_readable_name(true)
.done();
let Some(current_scene) = self
.base
Expand All @@ -64,10 +69,6 @@ impl CarSpawner {
};

inst.set_owner(current_scene);

let mut inst: Gd<Node3D> = inst.cast();

inst.global_translate(self.base.get_global_transform().origin);
inst.call(StringName::from("activate"), &[]);
}

Expand Down
30 changes: 25 additions & 5 deletions native/src/scripts/world/buildings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use anyhow::Context;
use derive_debug::Dbg;
use godot::{
builtin::{meta::ToGodot, Array, Dictionary},
engine::{utilities::snappedi, Node, Node3D, NodeExt, Resource, Time},
engine::{utilities::snappedi, Node, Node3D, Resource, Time},
obj::{Gd, NewAlloc},
};
use godot_rust_script::{godot_script_impl, GodotScript, ScriptSignal, Signal};
Expand Down Expand Up @@ -146,7 +146,7 @@ impl Buildings {
));
}

let (Some(instance), instance_time) = with_timing(|| object.instantiate()) else {
let (Some(mut instance), instance_time) = with_timing(|| object.instantiate()) else {
logger::error!("failed to instantiate building {}", name);
return;
};
Expand All @@ -172,7 +172,18 @@ impl Buildings {
self.get_sector(sector_name)
.add_child_ex(instance.clone())
.force_readable_name(true)
.done()
.done();

let Some(root) = self
.base
.get_tree()
.and_then(|tree| tree.get_current_scene())
else {
logger::warn!("there is no active scene!");
return;
};

instance.set_owner(root);
});

let (_, translate_time) = with_timing(|| instance.cast::<Node3D>().translate(location));
Expand All @@ -198,8 +209,17 @@ impl Buildings {

sector.set_name(name.to_godot());

self.base.add_child(sector);
self.base.get_node_as(name)
self.base.add_child(sector.clone());

if let Some(root) = self
.base
.get_tree()
.and_then(|tree| tree.get_current_scene())
{
sector.set_owner(root);
};

sector
})
}
}
Expand Down
29 changes: 11 additions & 18 deletions resources/Objects/Vehicles/car_station_wagon.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -33,58 +33,51 @@ script = ExtResource("2")
surface_material_override/0 = SubResource("StandardMaterial3D_juekg")
surface_material_override/1 = SubResource("StandardMaterial3D_i4xi6")

[node name="NavigationAgent3D" type="NavigationAgent3D" parent="." index="1"]
target_desired_distance = 1.5
path_max_distance = 2.0
avoidance_enabled = true
radius = 3.0
max_speed = 100.0

[node name="SpotLightLeft" type="SpotLight3D" parent="." index="2"]
[node name="SpotLightLeft" type="SpotLight3D" parent="." index="1"]
transform = Transform3D(1, 0, 0, 0, 0.965926, 0.258819, 0, -0.258819, 0.965926, 0.5, 0.8, -2.6)
spot_range = 3.0
spot_angle = 10.0

[node name="SpotLightRight" type="SpotLight3D" parent="." index="3"]
[node name="SpotLightRight" type="SpotLight3D" parent="." index="2"]
transform = Transform3D(1, 0, 0, 0, 0.965926, 0.258819, 0, -0.258819, 0.965926, -0.5, 0.8, -2.6)
spot_range = 3.0
spot_angle = 10.0

[node name="Debug" type="MeshInstance3D" parent="." index="4"]
[node name="Debug" type="MeshInstance3D" parent="." index="3"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 151, 0)
visible = false
mesh = SubResource("BoxMesh_483kg")

[node name="DebugTarget" type="MeshInstance3D" parent="." index="5"]
[node name="DebugTarget" type="MeshInstance3D" parent="." index="4"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 151, 10)
visible = false
mesh = SubResource("BoxMesh_483kg")

[node name="GroundDetector" type="RayCast3D" parent="." index="6"]
[node name="GroundDetector" type="RayCast3D" parent="." index="5"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.398, -3.04569)
debug_shape_custom_color = Color(0.152941, 1, 0.882353, 1)
debug_shape_thickness = 5

[node name="CollisionShapeLower" type="CollisionShape3D" parent="." index="7"]
[node name="CollisionShapeLower" type="CollisionShape3D" parent="." index="6"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.814, 0.038)
shape = SubResource("BoxShape3D_6viug")

[node name="CollisionShapeUpper" type="CollisionShape3D" parent="." index="8"]
[node name="CollisionShapeUpper" type="CollisionShape3D" parent="." index="7"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.01, 1.487, 0.978)
shape = SubResource("BoxShape3D_agao4")

[node name="CollisionShapeWheelFrontLeft" type="CollisionShape3D" parent="." index="9"]
[node name="CollisionShapeWheelFrontLeft" type="CollisionShape3D" parent="." index="8"]
transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, -0.892745, 0.345847, -1.58433)
shape = SubResource("CylinderShape3D_eoo2s")

[node name="CollisionShapeWheelBackLeft" type="CollisionShape3D" parent="." index="10"]
[node name="CollisionShapeWheelBackLeft" type="CollisionShape3D" parent="." index="9"]
transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, -0.905135, 0.345847, 1.54605)
shape = SubResource("CylinderShape3D_eoo2s")

[node name="CollisionShapeWheelFrontRight" type="CollisionShape3D" parent="." index="11"]
[node name="CollisionShapeWheelFrontRight" type="CollisionShape3D" parent="." index="10"]
transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 0.878487, 0.346, -1.584)
shape = SubResource("CylinderShape3D_eoo2s")

[node name="CollisionShapeWheelBackRight" type="CollisionShape3D" parent="." index="12"]
[node name="CollisionShapeWheelBackRight" type="CollisionShape3D" parent="." index="11"]
transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 0.880683, 0.346, 1.54256)
shape = SubResource("CylinderShape3D_eoo2s")
2 changes: 2 additions & 0 deletions src/Objects/Terrain/Terrain.gd
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ func build_async(city: Dictionary):

self.add_child(mesh_instance, true)
mesh_instance.owner = get_tree().current_scene

prints("generated terain:", self.get_child_count(), "nodes generated")
1 change: 0 additions & 1 deletion src/Objects/Vehicles/Car.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var last_transform := Transform3D.IDENTITY
var target_nav_node: Building
var current_nav_node: Building

@onready var navigation: NavigationAgent3D = $NavigationAgent3D
@onready var debug_target: MeshInstance3D = $DebugTarget
@onready var ground_detector: RayCast3D = $GroundDetector
@onready var road_network: RoadNavigation = get_node(road_network_path)
Expand Down
3 changes: 3 additions & 0 deletions src/Objects/World/Networks.gd
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func build_async(city: Dictionary):
else:
print("network secction doesn't belong to any network, ", network_section)

instance.set_owner(self.get_tree().current_scene)

self.emit_signal("loading_progress", 1)

if budget.is_exceded():
Expand All @@ -96,3 +98,4 @@ func build_async(city: Dictionary):
# yield at least once at the end, to let the engine catch up
await self.get_tree().process_frame
self.is_built = true
prints("generated networks:", self.road_network.get_child_count(), "nodes in road network,", self.powerline_network.get_child_count(), "nodes in powerline network")

0 comments on commit 91f976a

Please sign in to comment.