Trim down on unnecessary visibility updates.

This commit is contained in:
Nolan Darilek 2022-03-16 10:50:43 -05:00
parent 81162c8ca8
commit 197080e5fd

View File

@ -22,6 +22,10 @@ use crate::{
#[reflect(Component)] #[reflect(Component)]
pub struct DontLogWhenVisible; pub struct DontLogWhenVisible;
#[derive(Component, Clone, Debug, Default, Deref, DerefMut, Reflect)]
#[reflect(Component)]
struct LastCoordinates((i32, i32));
#[derive(Component, Clone, Debug, Default, Deref, DerefMut, Reflect)] #[derive(Component, Clone, Debug, Default, Deref, DerefMut, Reflect)]
#[reflect(Component)] #[reflect(Component)]
pub struct RevealedTiles(pub Vec<bool>); pub struct RevealedTiles(pub Vec<bool>);
@ -242,8 +246,16 @@ where
} }
fn update_viewshed<D: 'static + Clone + Default + Send + Sync>( fn update_viewshed<D: 'static + Clone + Default + Send + Sync>(
mut commands: Commands,
config: Res<RapierConfiguration>, config: Res<RapierConfiguration>,
positions: Query<(Entity, &RigidBodyPositionComponent), Changed<RigidBodyPositionComponent>>, positions: Query<
(
Entity,
&RigidBodyPositionComponent,
Option<&LastCoordinates>,
),
Changed<RigidBodyPositionComponent>,
>,
visible: Query<&Visible>, visible: Query<&Visible>,
mut viewers: Query<(Entity, &mut Viewshed, &mut VisibleEntities, &Coordinates)>, mut viewers: Query<(Entity, &mut Viewshed, &mut VisibleEntities, &Coordinates)>,
map: Query<&Map<D>>, map: Query<&Map<D>>,
@ -255,7 +267,7 @@ 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) in positions.iter() { for (entity, position, last_coordinates) in positions.iter() {
if to_update.contains(&entity) { if to_update.contains(&entity) {
continue; continue;
} }
@ -263,9 +275,16 @@ fn update_viewshed<D: 'static + Clone + Default + Send + Sync>(
position.position.translation.x, position.position.translation.x,
position.position.translation.y, position.position.translation.y,
); );
commands
.entity(entity)
.insert(LastCoordinates(coordinates.i32()));
if viewers.get_mut(entity).is_ok() { if viewers.get_mut(entity).is_ok() {
if let Some(last_coordinates) = last_coordinates {
if **last_coordinates != coordinates.i32() {
to_update.insert(entity); to_update.insert(entity);
} }
}
}
if visible.get(entity).is_err() { if visible.get(entity).is_err() {
continue; continue;
} }