Remove Map dependency from some visibility systems.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Nolan Darilek 2022-07-25 13:52:50 -05:00
parent 4e63ad21d1
commit 21f79b0528

View File

@ -48,13 +48,12 @@ impl Viewshed {
self.visible_points.contains(&point.into()) self.visible_points.contains(&point.into())
} }
fn update<D: 'static + Clone + Default + Send + Sync>( fn update(
&mut self, &mut self,
viewer_entity: &Entity, viewer_entity: &Entity,
visible_entities: &mut VisibleEntities, visible_entities: &mut VisibleEntities,
start: &GlobalTransform, start: &GlobalTransform,
rapier_context: &RapierContext, rapier_context: &RapierContext,
map: &Map<D>,
visible_query: &Query<(&Visible, &Collider, &GlobalTransform)>, visible_query: &Query<(&Visible, &Collider, &GlobalTransform)>,
obstructions_query: &Query<&MapObstruction>, obstructions_query: &Query<&MapObstruction>,
events: &mut EventWriter<VisibilityChanged>, events: &mut EventWriter<VisibilityChanged>,
@ -88,7 +87,10 @@ impl Viewshed {
let vision_distance = vision_distance::Circle::new(self.range); let vision_distance = vision_distance::Circle::new(self.range);
let shape = Collider::cuboid(0.5, 0.5); let shape = Collider::cuboid(0.5, 0.5);
let mut new_visible_entities = HashSet::new(); let mut new_visible_entities = HashSet::new();
let size = (map.width as u32, map.height as u32); let size = (
(start.translation.x + self.range as f32) as u32,
(start.translation.y + self.range as f32) as u32,
);
let visibility_grid = VisibilityGrid( let visibility_grid = VisibilityGrid(
size, size,
RefCell::new(Box::new(|coord: Coord| { RefCell::new(Box::new(|coord: Coord| {
@ -295,7 +297,7 @@ where
} }
} }
fn update_viewshed<D: 'static + Clone + Default + Send + Sync>( fn update_viewshed(
config: Res<RapierConfiguration>, config: Res<RapierConfiguration>,
visible: Query<(&Visible, &Collider, &GlobalTransform)>, visible: Query<(&Visible, &Collider, &GlobalTransform)>,
obstructions: Query<&MapObstruction>, obstructions: Query<&MapObstruction>,
@ -305,24 +307,20 @@ fn update_viewshed<D: 'static + Clone + Default + Send + Sync>(
&mut VisibleEntities, &mut VisibleEntities,
&GlobalTransform, &GlobalTransform,
)>, )>,
map: Query<&Map<D>>,
rapier_context: Res<RapierContext>, rapier_context: Res<RapierContext>,
mut changed: EventWriter<VisibilityChanged>, mut changed: EventWriter<VisibilityChanged>,
) { ) {
if !config.query_pipeline_active { if !config.query_pipeline_active {
return; return;
} }
if let Ok(map) = map.get_single() {
let mut cache = HashMap::new(); let mut cache = HashMap::new();
for (viewer_entity, mut viewshed, mut visible_entities, viewer_transform) in for (viewer_entity, mut viewshed, mut visible_entities, viewer_transform) in viewers.iter_mut()
viewers.iter_mut()
{ {
viewshed.update( viewshed.update(
&viewer_entity, &viewer_entity,
&mut visible_entities, &mut visible_entities,
viewer_transform, viewer_transform,
&rapier_context, &rapier_context,
map,
&visible, &visible,
&obstructions, &obstructions,
&mut changed, &mut changed,
@ -330,9 +328,8 @@ fn update_viewshed<D: 'static + Clone + Default + Send + Sync>(
); );
} }
} }
}
fn remove_visible<D: 'static + Clone + Default + Send + Sync>( fn remove_visible(
removed: RemovedComponents<Visible>, removed: RemovedComponents<Visible>,
mut viewers: Query<( mut viewers: Query<(
Entity, Entity,
@ -340,7 +337,6 @@ fn remove_visible<D: 'static + Clone + Default + Send + Sync>(
&mut VisibleEntities, &mut VisibleEntities,
&GlobalTransform, &GlobalTransform,
)>, )>,
map: Query<&Map<D>>,
rapier_context: Res<RapierContext>, rapier_context: Res<RapierContext>,
visible: Query<(&Visible, &Collider, &GlobalTransform)>, visible: Query<(&Visible, &Collider, &GlobalTransform)>,
obstructions: Query<&MapObstruction>, obstructions: Query<&MapObstruction>,
@ -354,13 +350,11 @@ fn remove_visible<D: 'static + Clone + Default + Send + Sync>(
continue; continue;
} }
visible_entities.remove(&removed); visible_entities.remove(&removed);
if let Ok(map) = map.get_single() {
viewshed.update( viewshed.update(
&viewer_entity, &viewer_entity,
&mut visible_entities, &mut visible_entities,
start, start,
&*rapier_context, &*rapier_context,
map,
&visible, &visible,
&obstructions, &obstructions,
&mut changed, &mut changed,
@ -370,7 +364,6 @@ fn remove_visible<D: 'static + Clone + Default + Send + Sync>(
} }
} }
} }
}
fn update_revealed_tiles<D: 'static + Clone + Default + Send + Sync>( fn update_revealed_tiles<D: 'static + Clone + Default + Send + Sync>(
mut map: Query<(&Map<D>, &mut RevealedTiles)>, mut map: Query<(&Map<D>, &mut RevealedTiles)>,
@ -449,9 +442,9 @@ impl<D: 'static + Clone + Default + Send + Sync> Plugin for VisibilityPlugin<D>
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_event::<VisibilityChanged>() app.add_event::<VisibilityChanged>()
.add_system_to_stage(CoreStage::PreUpdate, add_visibility_indices::<D>) .add_system_to_stage(CoreStage::PreUpdate, add_visibility_indices::<D>)
.add_system_to_stage(CoreStage::PreUpdate, update_viewshed::<D>) .add_system_to_stage(CoreStage::PreUpdate, update_viewshed)
.add_system_to_stage(CoreStage::PostUpdate, viewshed_removed) .add_system_to_stage(CoreStage::PostUpdate, viewshed_removed)
.add_system_to_stage(CoreStage::PostUpdate, remove_visible::<D>) .add_system_to_stage(CoreStage::PostUpdate, remove_visible)
.add_system_to_stage(CoreStage::PreUpdate, update_revealed_tiles::<D>) .add_system_to_stage(CoreStage::PreUpdate, update_revealed_tiles::<D>)
.add_system_to_stage(CoreStage::PreUpdate, log_visible); .add_system_to_stage(CoreStage::PreUpdate, log_visible);
} }