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);
|
impl_pointlike_for_tuple_component!(Destination);
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default, Reflect)]
|
||||||
|
#[reflect(Component)]
|
||||||
|
pub struct NoPath;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Deref, DerefMut, Reflect)]
|
#[derive(Clone, Debug, Default, Deref, DerefMut, Reflect)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct Path(pub Vec<(i32, i32)>);
|
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.distance_squared(&destination) * 100.) as u32,
|
||||||
|p| *p == destination,
|
|p| *p == destination,
|
||||||
);
|
);
|
||||||
channel_clone
|
let rv = if let Some(path) = path {
|
||||||
.send(if let Some(path) = path {
|
Some(Path(path.0))
|
||||||
Some(Path(path.0))
|
} else {
|
||||||
} else {
|
None
|
||||||
None
|
};
|
||||||
})
|
channel_clone.send(rv).ok();
|
||||||
.ok();
|
|
||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
||||||
}
|
}
|
||||||
|
@ -189,7 +192,7 @@ fn calculate_path(
|
||||||
collider_query: QueryPipelineColliderComponentsQuery,
|
collider_query: QueryPipelineColliderComponentsQuery,
|
||||||
mut calculating: Local<HashMap<Entity, Receiver<Option<Path>>>>,
|
mut calculating: Local<HashMap<Entity, Receiver<Option<Path>>>>,
|
||||||
query: Query<(Entity, &Destination, &Coordinates, &ColliderShape), Changed<Destination>>,
|
query: Query<(Entity, &Destination, &Coordinates, &ColliderShape), Changed<Destination>>,
|
||||||
destinations: Query<&Destination>,
|
destinations: Query<&Destination, Without<NoPath>>,
|
||||||
map: Query<&Map>,
|
map: Query<&Map>,
|
||||||
) {
|
) {
|
||||||
let calculating_clone = calculating.clone();
|
let calculating_clone = calculating.clone();
|
||||||
|
@ -199,7 +202,8 @@ fn calculate_path(
|
||||||
if let Some(path) = msg {
|
if let Some(path) = msg {
|
||||||
commands.entity(*entity).insert(path);
|
commands.entity(*entity).insert(path);
|
||||||
} else {
|
} else {
|
||||||
commands.entity(*entity).remove::<Destination>();
|
println!("No path, tagging {:?}", entity);
|
||||||
|
commands.entity(*entity).insert(NoPath);
|
||||||
}
|
}
|
||||||
calculating.remove(&entity);
|
calculating.remove(&entity);
|
||||||
}
|
}
|
||||||
|
@ -212,8 +216,11 @@ fn calculate_path(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if coordinates.i32() == **destination {
|
if coordinates.i32() == **destination {
|
||||||
commands.entity(entity).remove::<Path>();
|
println!("Removing path1: {:?}", entity);
|
||||||
commands.entity(entity).remove::<Destination>();
|
commands
|
||||||
|
.entity(entity)
|
||||||
|
.remove::<Path>()
|
||||||
|
.remove::<Destination>();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let (tx, rx) = unbounded();
|
let (tx, rx) = unbounded();
|
||||||
|
@ -273,6 +280,7 @@ fn negotiate_path(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
println!("Removing path2");
|
||||||
commands.entity(entity).remove::<Path>();
|
commands.entity(entity).remove::<Path>();
|
||||||
velocity.linvel = Vec2::ZERO.into();
|
velocity.linvel = Vec2::ZERO.into();
|
||||||
}
|
}
|
||||||
|
@ -294,18 +302,31 @@ fn negotiate_path(
|
||||||
direction *= **speed;
|
direction *= **speed;
|
||||||
velocity.linvel = direction.into();
|
velocity.linvel = direction.into();
|
||||||
} else {
|
} else {
|
||||||
commands.entity(entity).remove::<Path>();
|
println!("Removing path3");
|
||||||
velocity.linvel = Vec2::ZERO.into();
|
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;
|
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_to_stage(CoreStage::PostUpdate, calculate_path.system())
|
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