Skip to content

Commit

Permalink
don't use default on NavMeshBundle to make obvious handle reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Sep 6, 2024
1 parent b93459c commit cffeb3d
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion examples/auto_navmesh_aabb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion examples/auto_navmesh_avian2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fn setup(
..default()
},
update_mode: NavMeshUpdateMode::Direct,
..default()
..NavMeshBundle::with_default_id()
});
}

Expand Down
3 changes: 1 addition & 2 deletions examples/auto_navmesh_avian3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<NavMesh>::weak_from_u128(idx as u128),

..default()
..NavMeshBundle::with_default_id()
},));
}
}
Expand Down
3 changes: 1 addition & 2 deletions examples/auto_navmesh_avian3d_inclined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<NavMesh>::weak_from_u128(0),

..default()
..NavMeshBundle::with_default_id()
},
NavMeshDebug(palettes::tailwind::RED_600.into()),
));
Expand Down
2 changes: 1 addition & 1 deletion examples/auto_navmesh_primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ fn setup(mut commands: Commands, mut materials: ResMut<Assets<StandardMaterial>>
// 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(
Expand Down
2 changes: 1 addition & 1 deletion examples/gltf_avian3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
));
Expand Down
12 changes: 4 additions & 8 deletions examples/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ fn setup(
..default()
},
update_mode: NavMeshUpdateMode::Direct,
handle: Handle::<NavMesh>::weak_from_u128(0),
..default()
..NavMeshBundle::with_unique_id(0)
},
NavMeshDebug(palettes::tailwind::FUCHSIA_800.into()),
));
Expand Down Expand Up @@ -231,8 +230,7 @@ fn setup(
..default()
},
update_mode: NavMeshUpdateMode::Direct,
handle: Handle::<NavMesh>::weak_from_u128(0),
..default()
..NavMeshBundle::with_unique_id(0)
},
NavMeshDebug(palettes::tailwind::YELLOW_800.into()),
));
Expand Down Expand Up @@ -283,8 +281,7 @@ fn setup(
..default()
},
update_mode: NavMeshUpdateMode::Direct,
handle: Handle::<NavMesh>::weak_from_u128(0),
..default()
..NavMeshBundle::with_unique_id(0)
},
NavMeshDebug(palettes::tailwind::TEAL_800.into()),
));
Expand Down Expand Up @@ -342,8 +339,7 @@ fn setup(
..default()
},
update_mode: NavMeshUpdateMode::Direct,
handle: Handle::<NavMesh>::weak_from_u128(0),
..default()
..NavMeshBundle::with_unique_id(0)
},
NavMeshDebug(palettes::tailwind::TEAL_800.into()),
));
Expand Down
6 changes: 2 additions & 4 deletions examples/layers_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ fn setup(
..default()
},
update_mode: NavMeshUpdateMode::Direct,
handle: Handle::<NavMesh>::weak_from_u128(0),
..default()
..NavMeshBundle::with_unique_id(0)
},
NavMeshDebug(palettes::tailwind::YELLOW_600.into()),
));
Expand Down Expand Up @@ -156,8 +155,7 @@ fn setup(
..default()
},
update_mode: NavMeshUpdateMode::Direct,
handle: Handle::<NavMesh>::weak_from_u128(0),
..default()
..NavMeshBundle::with_unique_id(0)
},
NavMeshDebug(palettes::tailwind::LIME_600.into()),
));
Expand Down
9 changes: 3 additions & 6 deletions examples/layers_inclined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ fn setup(
..default()
},
update_mode: UPDATE_MODE,
handle: Handle::<NavMesh>::weak_from_u128(0),
..default()
..NavMeshBundle::with_unique_id(0)
},
NavMeshDebug(palettes::tailwind::YELLOW_600.into()),
));
Expand Down Expand Up @@ -178,8 +177,7 @@ fn setup(
..default()
},
update_mode: UPDATE_MODE,
handle: Handle::<NavMesh>::weak_from_u128(0),
..default()
..NavMeshBundle::with_unique_id(0)
},
NavMeshDebug(palettes::tailwind::BLUE_600.into()),
));
Expand Down Expand Up @@ -244,8 +242,7 @@ fn setup(
..default()
},
update_mode: UPDATE_MODE,
handle: Handle::<NavMesh>::weak_from_u128(0),
..default()
..NavMeshBundle::with_unique_id(0)
},
NavMeshDebug(palettes::tailwind::LIME_600.into()),
));
Expand Down
2 changes: 1 addition & 1 deletion examples/primitive_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
));
Expand Down
18 changes: 15 additions & 3 deletions src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<NavMesh>::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,
Expand Down Expand Up @@ -174,7 +187,6 @@ fn build_navmesh<T: ObstacleSource>(
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;
Expand Down

0 comments on commit cffeb3d

Please sign in to comment.