Efficient NavMesh and pathfinding solutions for your Bevy projects
cargo add vleue_navigator
use vleue_navigator::prelude::*;
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
VleueNavigatorPlugin,
// Auto update navmesh using the `Aabb` component
NavmeshUpdaterPlugin::::default(),
))
...
.run();
}
fn spawn_navmesh(mut commands: Commands) {
commands.spawn(NavMeshBundle {
settings: NavMeshSettings {
// Define the outer borders of the navmesh.
fixed: Triangulation::from_outer_edges(&[
vec2(0.0, 0.0), vec2(10.0, 0.0),
vec2(10.0, 10.0), vec2(0.0, 10.0),
]),
..default()
},
// Mark it for update as soon as obstacles are changed.
// Other modes available are debounced or manually triggered.
update_mode: NavMeshUpdateMode::Direct,
..NavMeshBundle::with_default_id()
});
}
pub fn find_path(navmeshes: Res>, navmesh: Query<(&Handle, Ref)>) {
let (navmesh_handle, status) = navmesh.single();
if status != NavMeshStatus::Built {
// NavMesh is not ready yet.
return;
}
if let Some(navmesh) = navmeshes.get_mut(navmesh_handle) {
let mut rng = rand::thread_rng();
let from = vec2(rng.gen_range(0.0..10.0), rng.gen_range(0.0..10.0));
let to = vec2(rng.gen_range(0.0..10.0), rng.gen_range(0.0..10.0));
if let Some(path) = navmesh.path(from, to) {
info!("found path from {:?} to {:?}: {:?}", from, to, path);
} else {
info!("no path found from {:?} to {:?}", from, to);
}
}
}