Pathfinding optimizations.

This commit is contained in:
Nolan Darilek 2021-07-21 15:20:02 -05:00
parent e6af8f7618
commit fee446cb7d

View File

@ -176,7 +176,7 @@ fn find_path_for_shape(
} else { } else {
None None
}) })
.expect("Channel should exist"); .ok();
}) })
.detach(); .detach();
} }
@ -188,10 +188,7 @@ fn calculate_path(
obstructions: Query<&MapObstruction>, obstructions: Query<&MapObstruction>,
collider_query: QueryPipelineColliderComponentsQuery, collider_query: QueryPipelineColliderComponentsQuery,
mut calculating: Local<HashMap<Entity, Receiver<Option<Path>>>>, mut calculating: Local<HashMap<Entity, Receiver<Option<Path>>>>,
query: Query< query: Query<(Entity, &Destination, &Coordinates, &ColliderShape), Changed<Destination>>,
(Entity, &Destination, &Coordinates, &ColliderShape),
Or<(Without<Path>, Changed<Destination>)>,
>,
destinations: Query<&Destination>, destinations: Query<&Destination>,
map: Query<&Map>, map: Query<&Map>,
) { ) {
@ -211,9 +208,16 @@ fn calculate_path(
} }
} }
for (entity, destination, coordinates, shape) in query.iter() { for (entity, destination, coordinates, shape) in query.iter() {
if let std::collections::hash_map::Entry::Vacant(e) = calculating.entry(entity) { if calculating.contains_key(&entity) {
continue;
}
if coordinates.i32() == **destination {
commands.entity(entity).remove::<Path>();
commands.entity(entity).remove::<Destination>();
continue;
}
let (tx, rx) = unbounded(); let (tx, rx) = unbounded();
e.insert(rx); calculating.insert(entity, rx);
for map in map.iter() { for map in map.iter() {
find_path_for_shape( find_path_for_shape(
&*pool, &*pool,
@ -230,7 +234,6 @@ fn calculate_path(
} }
} }
} }
}
fn negotiate_path( fn negotiate_path(
mut commands: Commands, mut commands: Commands,
@ -257,8 +260,17 @@ fn negotiate_path(
new_path = vec![start_i32]; new_path = vec![start_i32];
new_path.append(&mut upcoming.to_vec()); new_path.append(&mut upcoming.to_vec());
} else { } else {
commands.entity(entity).remove::<Path>(); let start = Vec2::new(
velocity.linvel = Vec2::ZERO.into(); position.position.translation.x,
position.position.translation.y,
);
let next = path[1];
let next = Vec2::new(next.0 as f32 + 0.5, next.1 as f32 + 0.5);
let mut direction = next - start;
direction = direction.normalize();
direction *= **speed;
velocity.linvel = direction.into();
continue;
} }
} else { } else {
commands.entity(entity).remove::<Path>(); commands.entity(entity).remove::<Path>();