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,23 +208,29 @@ 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) {
let (tx, rx) = unbounded(); continue;
e.insert(rx); }
for map in map.iter() { if coordinates.i32() == **destination {
find_path_for_shape( commands.entity(entity).remove::<Path>();
&*pool, commands.entity(entity).remove::<Destination>();
query_pipeline.clone(), continue;
entity, }
coordinates, let (tx, rx) = unbounded();
destination, calculating.insert(entity, rx);
&map, for map in map.iter() {
&collider_query, find_path_for_shape(
&obstructions, &*pool,
shape, query_pipeline.clone(),
&tx, entity,
); coordinates,
} destination,
&map,
&collider_query,
&obstructions,
shape,
&tx,
);
} }
} }
} }
@ -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>();