Improve performance of visibility calculations.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
cc4b8751f4
commit
cadaeceba9
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user