Round 1 of pathfinding optimizations.
This commit is contained in:
parent
5dbc6510bc
commit
1eb5351a9c
|
@ -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<HashMap<Entity, Receiver<Option<Path>>>>,
|
||||
query: Query<(Entity, &Destination, &Coordinates, &ColliderShape), Changed<Destination>>,
|
||||
destinations: Query<&Destination>,
|
||||
destinations: Query<&Destination, Without<NoPath>>,
|
||||
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::<Destination>();
|
||||
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::<Path>();
|
||||
commands.entity(entity).remove::<Destination>();
|
||||
println!("Removing path1: {:?}", entity);
|
||||
commands
|
||||
.entity(entity)
|
||||
.remove::<Path>()
|
||||
.remove::<Destination>();
|
||||
continue;
|
||||
}
|
||||
let (tx, rx) = unbounded();
|
||||
|
@ -273,6 +280,7 @@ fn negotiate_path(
|
|||
continue;
|
||||
}
|
||||
} else {
|
||||
println!("Removing path2");
|
||||
commands.entity(entity).remove::<Path>();
|
||||
velocity.linvel = Vec2::ZERO.into();
|
||||
}
|
||||
|
@ -294,18 +302,31 @@ fn negotiate_path(
|
|||
direction *= **speed;
|
||||
velocity.linvel = direction.into();
|
||||
} else {
|
||||
commands.entity(entity).remove::<Path>();
|
||||
println!("Removing path3");
|
||||
velocity.linvel = Vec2::ZERO.into();
|
||||
commands.entity(entity).remove::<Destination>();
|
||||
commands
|
||||
.entity(entity)
|
||||
.remove::<Path>()
|
||||
.remove::<Destination>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_no_path(
|
||||
mut commands: Commands,
|
||||
query: Query<Entity, (With<NoPath>, Changed<Destination>)>,
|
||||
) {
|
||||
for entity in query.iter() {
|
||||
commands.entity(entity).remove::<NoPath>();
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user