From 197080e5fdfcfc7d1143716fe7082a665d44a34c Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 16 Mar 2022 10:50:43 -0500 Subject: [PATCH] Trim down on unnecessary visibility updates. --- src/visibility.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/visibility.rs b/src/visibility.rs index 8bf4f21..0fb4d66 100644 --- a/src/visibility.rs +++ b/src/visibility.rs @@ -22,6 +22,10 @@ use crate::{ #[reflect(Component)] 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)] #[reflect(Component)] pub struct RevealedTiles(pub Vec); @@ -242,8 +246,16 @@ where } fn update_viewshed( + mut commands: Commands, config: Res, - positions: Query<(Entity, &RigidBodyPositionComponent), Changed>, + positions: Query< + ( + Entity, + &RigidBodyPositionComponent, + Option<&LastCoordinates>, + ), + Changed, + >, visible: Query<&Visible>, mut viewers: Query<(Entity, &mut Viewshed, &mut VisibleEntities, &Coordinates)>, map: Query<&Map>, @@ -255,7 +267,7 @@ fn update_viewshed( return; } 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) { continue; } @@ -263,8 +275,15 @@ fn update_viewshed( position.position.translation.x, position.position.translation.y, ); + commands + .entity(entity) + .insert(LastCoordinates(coordinates.i32())); if viewers.get_mut(entity).is_ok() { - to_update.insert(entity); + if let Some(last_coordinates) = last_coordinates { + if **last_coordinates != coordinates.i32() { + to_update.insert(entity); + } + } } if visible.get(entity).is_err() { continue;