diff --git a/src/visibility.rs b/src/visibility.rs index de97870..e730b99 100644 --- a/src/visibility.rs +++ b/src/visibility.rs @@ -271,10 +271,14 @@ fn update_viewshed( positions: Query< ( Entity, - &RigidBodyPositionComponent, + Option<&RigidBodyPositionComponent>, + Option<&ColliderPositionComponent>, Option<&LastCoordinates>, ), - Changed, + Or<( + Changed, + Changed, + )>, >, visible: Query<&Visible>, mut viewers: Query<(Entity, &mut Viewshed, &mut VisibleEntities, &Coordinates)>, @@ -287,23 +291,33 @@ fn update_viewshed( 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(); - commands.run_if_exists(entity, move |mut entity| { - entity.insert(LastCoordinates(coordinates_i32)); - }); 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 **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( && (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); } }