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