Even more cleanup.

This commit is contained in:
Nolan Darilek 2021-07-27 20:26:16 -05:00
parent d28dd051e4
commit 7f61eb3a90

View File

@ -226,14 +226,9 @@ fn calculate_path(
} }
} }
fn poll_tasks( fn poll_tasks(mut commands: Commands, mut query: Query<(Entity, &mut Calculating)>) {
mut commands: Commands,
mut query: Query<(Entity, &mut Calculating)>,
destinations: Query<&Destination>,
) {
for (entity, mut calculating) in query.iter_mut() { for (entity, mut calculating) in query.iter_mut() {
println!("Got a calculating task for {:?}", entity); println!("Got a calculating task for {:?}", entity);
if destinations.get(entity).is_ok() {
if let Some(result) = future::block_on(future::poll_once(&mut **calculating)) { if let Some(result) = future::block_on(future::poll_once(&mut **calculating)) {
println!("Got a result, setting and returning"); println!("Got a result, setting and returning");
if let Some(path) = result { if let Some(path) = result {
@ -244,11 +239,13 @@ fn poll_tasks(
println!("Removing calculation task"); println!("Removing calculation task");
commands.entity(entity).remove::<Calculating>(); commands.entity(entity).remove::<Calculating>();
} }
} else {
println!("Destination was removed, clearing task");
commands.entity(entity).remove::<Calculating>();
} }
} }
fn remove_destination(mut commands: Commands, removed: RemovedComponents<Destination>) {
for entity in removed.iter() {
commands.entity(entity).remove::<Calculating>();
}
} }
fn negotiate_path( fn negotiate_path(
@ -336,6 +333,7 @@ pub struct PathfindingPlugin;
impl Plugin for PathfindingPlugin { impl Plugin for PathfindingPlugin {
fn build(&self, app: &mut AppBuilder) { fn build(&self, app: &mut AppBuilder) {
app.add_system(calculate_path.system()) app.add_system(calculate_path.system())
.add_system_to_stage(CoreStage::PostUpdate, remove_destination.system())
.add_system(poll_tasks.system()) .add_system(poll_tasks.system())
.add_system(negotiate_path.system()) .add_system(negotiate_path.system())
.add_system_to_stage(CoreStage::PostUpdate, remove_calculating.system()); .add_system_to_stage(CoreStage::PostUpdate, remove_calculating.system());