From cffeb3dcece1841938792694d7652bf8b5f0aa84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Fri, 6 Sep 2024 22:46:24 +0200 Subject: [PATCH] don't use default on NavMeshBundle to make obvious handle reuse --- examples/auto_navmesh_aabb.rs | 2 +- examples/auto_navmesh_avian2d.rs | 2 +- examples/auto_navmesh_avian3d.rs | 3 +-- examples/auto_navmesh_avian3d_inclined.rs | 3 +-- examples/auto_navmesh_primitive.rs | 2 +- examples/demo.rs | 2 +- examples/gltf_avian3d.rs | 2 +- examples/layers.rs | 12 ++++-------- examples/layers_basic.rs | 6 ++---- examples/layers_inclined.rs | 9 +++------ examples/primitive_3d.rs | 2 +- src/updater.rs | 18 +++++++++++++++--- 12 files changed, 32 insertions(+), 31 deletions(-) diff --git a/examples/auto_navmesh_aabb.rs b/examples/auto_navmesh_aabb.rs index ebcede1..fafe4fe 100644 --- a/examples/auto_navmesh_aabb.rs +++ b/examples/auto_navmesh_aabb.rs @@ -94,7 +94,7 @@ fn setup(mut commands: Commands) { 0.0, )) .with_scale(Vec3::splat(FACTOR)), - ..default() + ..NavMeshBundle::with_default_id() }); // Spawn a few obstacles to start with. diff --git a/examples/auto_navmesh_avian2d.rs b/examples/auto_navmesh_avian2d.rs index 3047c0e..f56123b 100644 --- a/examples/auto_navmesh_avian2d.rs +++ b/examples/auto_navmesh_avian2d.rs @@ -142,7 +142,7 @@ fn setup( ..default() }, update_mode: NavMeshUpdateMode::Direct, - ..default() + ..NavMeshBundle::with_default_id() }); } diff --git a/examples/auto_navmesh_avian3d.rs b/examples/auto_navmesh_avian3d.rs index 8315ed3..06f7855 100644 --- a/examples/auto_navmesh_avian3d.rs +++ b/examples/auto_navmesh_avian3d.rs @@ -197,8 +197,7 @@ fn setup( transform: Transform::from_xyz(0.0, idx as f32 * height_step + 0.1, 0.0) .with_rotation(Quat::from_rotation_x(FRAC_PI_2)), handle: Handle::::weak_from_u128(idx as u128), - - ..default() + ..NavMeshBundle::with_default_id() },)); } } diff --git a/examples/auto_navmesh_avian3d_inclined.rs b/examples/auto_navmesh_avian3d_inclined.rs index ed42830..cfbe145 100644 --- a/examples/auto_navmesh_avian3d_inclined.rs +++ b/examples/auto_navmesh_avian3d_inclined.rs @@ -110,8 +110,7 @@ fn setup( transform: Transform::from_xyz(0.0, 0.1, 5.0) .with_rotation(Quat::from_rotation_x(ANGLE) * Quat::from_rotation_x(FRAC_PI_2)), handle: Handle::::weak_from_u128(0), - - ..default() + ..NavMeshBundle::with_default_id() }, NavMeshDebug(palettes::tailwind::RED_600.into()), )); diff --git a/examples/auto_navmesh_primitive.rs b/examples/auto_navmesh_primitive.rs index 9c0fd20..af2be06 100644 --- a/examples/auto_navmesh_primitive.rs +++ b/examples/auto_navmesh_primitive.rs @@ -97,7 +97,7 @@ fn setup(mut commands: Commands) { 0.0, )) .with_scale(Vec3::splat(FACTOR)), - ..default() + ..NavMeshBundle::with_default_id() }); let mut rng = rand::thread_rng(); diff --git a/examples/demo.rs b/examples/demo.rs index c1fde6d..286653d 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -233,7 +233,7 @@ fn setup(mut commands: Commands, mut materials: ResMut> // Mark it for update as soon as obstacles are changed. // Other modes can be debounced or manually triggered. update_mode: NavMeshUpdateMode::Direct, - ..default() + ..NavMeshBundle::with_default_id() }); materials.insert( diff --git a/examples/gltf_avian3d.rs b/examples/gltf_avian3d.rs index c700be5..ee86b0a 100644 --- a/examples/gltf_avian3d.rs +++ b/examples/gltf_avian3d.rs @@ -197,7 +197,7 @@ fn setup_scene( transform: Transform::from_rotation(Quat::from_rotation_x(FRAC_PI_2)), // update_mode: NavMeshUpdateMode::Debounced(1.0), update_mode: NavMeshUpdateMode::Direct, - ..default() + ..NavMeshBundle::with_default_id() }, NavMeshDebug(palettes::tailwind::RED_400.into()), )); diff --git a/examples/layers.rs b/examples/layers.rs index 4928511..2ff050f 100644 --- a/examples/layers.rs +++ b/examples/layers.rs @@ -140,8 +140,7 @@ fn setup( ..default() }, update_mode: NavMeshUpdateMode::Direct, - handle: Handle::::weak_from_u128(0), - ..default() + ..NavMeshBundle::with_unique_id(0) }, NavMeshDebug(palettes::tailwind::FUCHSIA_800.into()), )); @@ -231,8 +230,7 @@ fn setup( ..default() }, update_mode: NavMeshUpdateMode::Direct, - handle: Handle::::weak_from_u128(0), - ..default() + ..NavMeshBundle::with_unique_id(0) }, NavMeshDebug(palettes::tailwind::YELLOW_800.into()), )); @@ -283,8 +281,7 @@ fn setup( ..default() }, update_mode: NavMeshUpdateMode::Direct, - handle: Handle::::weak_from_u128(0), - ..default() + ..NavMeshBundle::with_unique_id(0) }, NavMeshDebug(palettes::tailwind::TEAL_800.into()), )); @@ -342,8 +339,7 @@ fn setup( ..default() }, update_mode: NavMeshUpdateMode::Direct, - handle: Handle::::weak_from_u128(0), - ..default() + ..NavMeshBundle::with_unique_id(0) }, NavMeshDebug(palettes::tailwind::TEAL_800.into()), )); diff --git a/examples/layers_basic.rs b/examples/layers_basic.rs index 854e9c1..d201781 100644 --- a/examples/layers_basic.rs +++ b/examples/layers_basic.rs @@ -103,8 +103,7 @@ fn setup( ..default() }, update_mode: NavMeshUpdateMode::Direct, - handle: Handle::::weak_from_u128(0), - ..default() + ..NavMeshBundle::with_unique_id(0) }, NavMeshDebug(palettes::tailwind::YELLOW_600.into()), )); @@ -156,8 +155,7 @@ fn setup( ..default() }, update_mode: NavMeshUpdateMode::Direct, - handle: Handle::::weak_from_u128(0), - ..default() + ..NavMeshBundle::with_unique_id(0) }, NavMeshDebug(palettes::tailwind::LIME_600.into()), )); diff --git a/examples/layers_inclined.rs b/examples/layers_inclined.rs index e9e7e68..b51055d 100644 --- a/examples/layers_inclined.rs +++ b/examples/layers_inclined.rs @@ -113,8 +113,7 @@ fn setup( ..default() }, update_mode: UPDATE_MODE, - handle: Handle::::weak_from_u128(0), - ..default() + ..NavMeshBundle::with_unique_id(0) }, NavMeshDebug(palettes::tailwind::YELLOW_600.into()), )); @@ -178,8 +177,7 @@ fn setup( ..default() }, update_mode: UPDATE_MODE, - handle: Handle::::weak_from_u128(0), - ..default() + ..NavMeshBundle::with_unique_id(0) }, NavMeshDebug(palettes::tailwind::BLUE_600.into()), )); @@ -244,8 +242,7 @@ fn setup( ..default() }, update_mode: UPDATE_MODE, - handle: Handle::::weak_from_u128(0), - ..default() + ..NavMeshBundle::with_unique_id(0) }, NavMeshDebug(palettes::tailwind::LIME_600.into()), )); diff --git a/examples/primitive_3d.rs b/examples/primitive_3d.rs index 3fc73d7..a62aa5c 100644 --- a/examples/primitive_3d.rs +++ b/examples/primitive_3d.rs @@ -117,7 +117,7 @@ fn setup( // Mark it for update as soon as obstacles are changed. // Other modes available are debounced or manual trigger. update_mode: NavMeshUpdateMode::Direct, - ..default() + ..NavMeshBundle::with_default_id() }, NavMeshDebug(palettes::tailwind::SLATE_800.into()), )); diff --git a/src/updater.rs b/src/updater.rs index 3b425f2..cf3b314 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -40,8 +40,21 @@ pub struct NavMeshBundle { pub update_mode: NavMeshUpdateMode, } -impl Default for NavMeshBundle { - fn default() -> Self { +impl NavMeshBundle { + /// Create a new `NavMeshBundle` with the provided id used for the handle of the `NavMesh`. + /// + /// In case there are several `NavMeshBundle`s with the same handle, they will overwrite each others unless they target different layers. + pub fn with_unique_id(id: u128) -> Self { + Self { + handle: Handle::::weak_from_u128(id), + ..Self::with_default_id() + } + } + + /// Create a new `NavMeshBundle` with the default handle for the `NavMesh`. + /// + /// In case there are several `NavMeshBundle`s with the same handle, they will overwrite each others unless they target different layers. + pub fn with_default_id() -> Self { Self { settings: NavMeshSettings::default(), status: NavMeshStatus::Invalid, @@ -174,7 +187,6 @@ fn build_navmesh( for _ in 0..settings.merge_steps { layer.merge_polygons(); } - // layer.offset = mesh_transform.translation.xz() / mesh_transform.scale.xz(); #[cfg(feature = "detailed-layers")] { layer.scale = scale;