From 1eb5351a9ccd0b07ace294672e24c3871a687af6 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 27 Jul 2021 11:17:06 -0500 Subject: [PATCH] Round 1 of pathfinding optimizations. --- src/pathfinding.rs | 49 +++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/src/pathfinding.rs b/src/pathfinding.rs index ef2dc3d..19ebafd 100644 --- a/src/pathfinding.rs +++ b/src/pathfinding.rs @@ -22,6 +22,10 @@ pub struct Destination(pub (i32, i32)); impl_pointlike_for_tuple_component!(Destination); +#[derive(Clone, Debug, Default, Reflect)] +#[reflect(Component)] +pub struct NoPath; + #[derive(Clone, Debug, Default, Deref, DerefMut, Reflect)] #[reflect(Component)] pub struct Path(pub Vec<(i32, i32)>); @@ -170,13 +174,12 @@ fn find_path_for_shape( |p| (p.distance_squared(&destination) * 100.) as u32, |p| *p == destination, ); - channel_clone - .send(if let Some(path) = path { - Some(Path(path.0)) - } else { - None - }) - .ok(); + let rv = if let Some(path) = path { + Some(Path(path.0)) + } else { + None + }; + channel_clone.send(rv).ok(); }) .detach(); } @@ -189,7 +192,7 @@ fn calculate_path( collider_query: QueryPipelineColliderComponentsQuery, mut calculating: Local>>>, query: Query<(Entity, &Destination, &Coordinates, &ColliderShape), Changed>, - destinations: Query<&Destination>, + destinations: Query<&Destination, Without>, map: Query<&Map>, ) { let calculating_clone = calculating.clone(); @@ -199,7 +202,8 @@ fn calculate_path( if let Some(path) = msg { commands.entity(*entity).insert(path); } else { - commands.entity(*entity).remove::(); + println!("No path, tagging {:?}", entity); + commands.entity(*entity).insert(NoPath); } calculating.remove(&entity); } @@ -212,8 +216,11 @@ fn calculate_path( continue; } if coordinates.i32() == **destination { - commands.entity(entity).remove::(); - commands.entity(entity).remove::(); + println!("Removing path1: {:?}", entity); + commands + .entity(entity) + .remove::() + .remove::(); continue; } let (tx, rx) = unbounded(); @@ -273,6 +280,7 @@ fn negotiate_path( continue; } } else { + println!("Removing path2"); commands.entity(entity).remove::(); velocity.linvel = Vec2::ZERO.into(); } @@ -294,18 +302,31 @@ fn negotiate_path( direction *= **speed; velocity.linvel = direction.into(); } else { - commands.entity(entity).remove::(); + println!("Removing path3"); velocity.linvel = Vec2::ZERO.into(); - commands.entity(entity).remove::(); + commands + .entity(entity) + .remove::() + .remove::(); } } } +fn remove_no_path( + mut commands: Commands, + query: Query, Changed)>, +) { + for entity in query.iter() { + commands.entity(entity).remove::(); + } +} + pub struct PathfindingPlugin; impl Plugin for PathfindingPlugin { fn build(&self, app: &mut AppBuilder) { app.add_system_to_stage(CoreStage::PostUpdate, calculate_path.system()) - .add_system(negotiate_path.system()); + .add_system(negotiate_path.system()) + .add_system(remove_no_path.system()); } }