Optimize visibility.

This commit is contained in:
Nolan Darilek 2021-09-28 14:36:21 -05:00
parent f3df13ec25
commit e8caa58b08

View File

@ -3,7 +3,7 @@ use std::{
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
}; };
use bevy::prelude::*; use bevy::{core::FixedTimestep, prelude::*};
use bevy_rapier2d::{na, na::UnitComplex}; use bevy_rapier2d::{na, na::UnitComplex};
use coord_2d::{Coord, Size}; use coord_2d::{Coord, Size};
use derive_more::{Deref, DerefMut}; use derive_more::{Deref, DerefMut};
@ -241,10 +241,10 @@ fn update_viewshed_for_coordinates(
if !config.query_pipeline_active { if !config.query_pipeline_active {
return; return;
} }
for (visible_entity, coordinates) in visible.iter() { for (visible_entity, visible_coordinates) in visible.iter() {
for (viewer_entity, mut viewshed, mut visible_entities, start) in viewers.iter_mut() { for (viewer_entity, mut viewshed, mut visible_entities, start) in viewers.iter_mut() {
if viewer_entity == visible_entity if viewer_entity == visible_entity
|| coordinates.distance(start) > viewshed.range as f32 || start.distance(&visible_coordinates) > viewshed.range as f32
{ {
continue; continue;
} }
@ -265,10 +265,20 @@ fn update_viewshed_for_coordinates(
} }
} }
#[derive(Clone, Debug, Deref, DerefMut)]
struct LastStart((i32, i32));
fn update_viewshed_for_start( fn update_viewshed_for_start(
mut commands: Commands,
config: Res<RapierConfiguration>, config: Res<RapierConfiguration>,
mut viewers: Query< mut viewers: Query<
(Entity, &mut Viewshed, &mut VisibleEntities, &Coordinates), (
Entity,
&mut Viewshed,
&mut VisibleEntities,
&Coordinates,
Option<&LastStart>,
),
Changed<Coordinates>, Changed<Coordinates>,
>, >,
map: Query<&Map>, map: Query<&Map>,
@ -280,19 +290,30 @@ fn update_viewshed_for_start(
if !config.query_pipeline_active { if !config.query_pipeline_active {
return; return;
} }
for (viewer_entity, mut viewshed, mut visible_entities, start) in viewers.iter_mut() { for (viewer_entity, mut viewshed, mut visible_entities, start, last_start) in viewers.iter_mut()
if let Ok(map) = map.single() { {
update_viewshed( let should_update = if let Some(last) = last_start {
&viewer_entity, start.i32() != **last
&mut viewshed, } else {
&mut visible_entities, true
start, };
&*query_pipeline, commands
&collider_query, .entity(viewer_entity)
map, .insert(LastStart(start.i32()));
&visible, if should_update {
&mut changed, if let Ok(map) = map.single() {
); update_viewshed(
&viewer_entity,
&mut viewshed,
&mut visible_entities,
start,
&*query_pipeline,
&collider_query,
map,
&visible,
&mut changed,
);
}
} }
} }
} }
@ -403,8 +424,12 @@ impl Plugin for VisibilityPlugin {
app.add_event::<VisibilityChanged>() app.add_event::<VisibilityChanged>()
.add_system(add_visibility_indices.system()) .add_system(add_visibility_indices.system())
.add_system_to_stage(CoreStage::PostUpdate, viewshed_removed.system()) .add_system_to_stage(CoreStage::PostUpdate, viewshed_removed.system())
.add_system(update_viewshed_for_coordinates.system()) .add_system_set(
.add_system(update_viewshed_for_start.system()) SystemSet::new()
.with_run_criteria(FixedTimestep::step(0.25))
.with_system(update_viewshed_for_coordinates.system())
.with_system(update_viewshed_for_start.system()),
)
.add_system_to_stage(CoreStage::PostUpdate, remove_visible.system()) .add_system_to_stage(CoreStage::PostUpdate, remove_visible.system())
.add_system_to_stage(CoreStage::PostUpdate, update_revealed_tiles.system()) .add_system_to_stage(CoreStage::PostUpdate, update_revealed_tiles.system())
.add_system_to_stage(CoreStage::PostUpdate, log_visible.system()); .add_system_to_stage(CoreStage::PostUpdate, log_visible.system());