Improve performance of visibility calculations.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Nolan Darilek 2022-03-21 19:22:13 -05:00
parent cc4b8751f4
commit cadaeceba9

View File

@ -271,10 +271,14 @@ fn update_viewshed<D: 'static + Clone + Default + Send + Sync>(
positions: Query<
(
Entity,
&RigidBodyPositionComponent,
Option<&RigidBodyPositionComponent>,
Option<&ColliderPositionComponent>,
Option<&LastCoordinates>,
),
Or<(
Changed<RigidBodyPositionComponent>,
Changed<ColliderPositionComponent>,
)>,
>,
visible: Query<&Visible>,
mut viewers: Query<(Entity, &mut Viewshed, &mut VisibleEntities, &Coordinates)>,
@ -287,23 +291,33 @@ fn update_viewshed<D: 'static + Clone + Default + Send + Sync>(
return;
}
let mut to_update = HashSet::new();
for (entity, position, last_coordinates) in positions.iter() {
for (entity, body_position, collider_position, last_coordinates) in positions.iter() {
if to_update.contains(&entity) {
continue;
}
let coordinates = (
position.position.translation.x,
position.position.translation.y,
);
let coordinates = if let Some(body_position) = body_position {
(
body_position.position.translation.x,
body_position.position.translation.y,
)
} else {
let collider_position = collider_position.unwrap();
(
collider_position.translation.x,
collider_position.translation.y,
)
};
let coordinates_i32 = coordinates.i32();
if viewers.get_mut(entity).is_ok() {
commands.run_if_exists(entity, move |mut entity| {
entity.insert(LastCoordinates(coordinates_i32));
});
if viewers.get_mut(entity).is_ok() {
if let Some(last_coordinates) = last_coordinates {
if **last_coordinates != coordinates.i32() {
to_update.insert(entity);
}
} else {
to_update.insert(entity);
}
}
if visible.get(entity).is_err() {
@ -314,6 +328,11 @@ fn update_viewshed<D: 'static + Clone + Default + Send + Sync>(
&& (viewer_coordinates.distance(&coordinates) <= viewshed.range as f32
|| visible_entities.contains(&entity))
{
if let Some(last_coords) = last_coordinates {
if **last_coords == coordinates_i32 {
continue;
}
}
to_update.insert(viewer_entity);
}
}