Skip to content

Commit

Permalink
Merge pull request #177 from janhohenheim/navmesh
Browse files Browse the repository at this point in the history
Former-commit-id: 4436bbc
  • Loading branch information
janhohenheim authored Feb 20, 2023
2 parents df8749d + a26ea8b commit 9d4e341
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub struct DevPlugin;
impl Plugin for DevPlugin {
fn build(&self, app: &mut App) {
{
app.insert_resource(default_editor_controls())
.add_plugin(EditorPlugin)
app.add_plugin(EditorPlugin)
.insert_resource(default_editor_controls())
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_plugin(DebugLinesPlugin::default())
.add_plugin(DevEditorPlugin)
Expand Down
58 changes: 19 additions & 39 deletions src/movement/navigation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(feature = "dev")]
use crate::dev::dev_editor::DevEditorWindow;
use crate::level_instantiation::spawning::objects::npc;
use crate::movement::general_movement::{apply_walking, reset_movement_components, Walking};
use crate::player_control::player_embodiment::Player;
use crate::util::log_error::log_errors;
Expand All @@ -8,7 +9,6 @@ use crate::GameState;
use anyhow::{Context, Result};
use bevy::prelude::*;
use bevy_prototype_debug_lines::DebugLines;
use bevy_rapier3d::prelude::*;
use oxidized_navigation::{
query::{find_path, perform_string_pulling_on_path},
NavMesh, NavMeshGenerationState, NavMeshSettings, OxidizedNavigationPlugin,
Expand All @@ -19,25 +19,27 @@ use serde::{Deserialize, Serialize};
/// Currently only one navmesh is supported. It is loaded automagically from any entity whose name contains `"[navmesh]"`.
pub struct NavigationPlugin;

const CELL_WIDTH: f32 = 0.5 * npc::RADIUS * 2.;

impl Plugin for NavigationPlugin {
fn build(&self, app: &mut App) {
app.add_plugin(OxidizedNavigationPlugin {
starting_state: NavMeshGenerationState::Running, // Generate tile updates.
})
.insert_resource(NavMeshSettings {
cell_width: 0.25,
cell_height: 0.1,
tile_width: 100,
cell_width: CELL_WIDTH,
cell_height: 0.5 * CELL_WIDTH,
tile_width: 150,
world_half_extents: 250.0,
world_bottom_bound: -100.0,
max_traversable_slope_radians: (40.0_f32 - 0.1).to_radians(),
walkable_height: 20,
walkable_radius: 1,
walkable_height: 25,
walkable_radius: 2,
step_height: 3,
min_region_area: 100,
min_region_area: 50,
merge_region_area: 500,
max_contour_simplification_error: 1.1,
max_edge_length: 80,
max_edge_length: 70,
})
.add_system_set(
SystemSet::on_update(GameState::Playing).with_system(
Expand All @@ -56,48 +58,25 @@ pub struct Follower;

#[allow(clippy::type_complexity)]
fn query_mesh(
mut with_follower: Query<(Entity, &Transform, &mut Walking), (With<Follower>, Without<Player>)>,
with_player: Query<(Entity, &Transform), (With<Player>, Without<Follower>)>,
mut with_follower: Query<(&Transform, &mut Walking), (With<Follower>, Without<Player>)>,
with_player: Query<&Transform, (With<Player>, Without<Follower>)>,
nav_mesh_settings: Res<NavMeshSettings>,
nav_mesh: Res<NavMesh>,
mut lines: ResMut<DebugLines>,
rapier_context: Res<RapierContext>,
#[cfg(feature = "dev")] editor_state: Res<bevy_editor_pls::Editor>,
) -> Result<()> {
if let Ok(nav_mesh) = nav_mesh.get().read() {
for (follower_entity, follower_transform, mut walking) in &mut with_follower {
for (player_entity, player_transform) in &with_player {
for (follower_transform, mut walking) in &mut with_follower {
for player_transform in &with_player {
let from = follower_transform.translation;
let to = player_transform.translation;
if (to - from).length_squared() < 3.0f32.squared() {
continue;
}

let max_toi = 50.;
let solid = false;
let filter = QueryFilter::new()
.exclude_sensors()
.exclude_collider(follower_entity);
let path = if let Some((entity, _toi)) =
rapier_context.cast_ray(from, to - from, max_toi, solid, filter)
&& entity == player_entity
{
Some(vec![from, to])
} else if let Ok(path) = find_path(
&nav_mesh,
&nav_mesh_settings,
from,
to,
None,
None,
) {
let string_path = perform_string_pulling_on_path(&nav_mesh, from, to, &path)
if let Ok(path) = find_path(&nav_mesh, &nav_mesh_settings, from, to, None, None) {
let path = perform_string_pulling_on_path(&nav_mesh, from, to, &path)
.map_err(|e| anyhow::Error::msg(format!("{e:?}")))?;
Some(string_path)
} else {
None
};
if let Some(path) = path {
#[cfg(feature = "dev")]
if editor_state
.window_state::<DevEditorWindow>()
Expand All @@ -108,12 +87,13 @@ fn query_mesh(
}
let dir = path
.into_iter()
.filter_map(|next_point| {
.map(|next_point| {
(next_point - from)
.split(follower_transform.up())
.horizontal
.try_normalize()
})
.filter(|dir| dir.length_squared() > 1e-3f32.squared())
.filter_map(|dir| dir.try_normalize())
.next();
walking.direction = dir;
}
Expand Down

0 comments on commit 9d4e341

Please sign in to comment.