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())
}
fn update<D: 'static + Clone + Default + Send + Sync>(
fn update(
&mut self,
viewer_entity: &Entity,
visible_entities: &mut VisibleEntities,
start: &GlobalTransform,
rapier_context: &RapierContext,
map: &Map<D>,
visible_query: &Query<(&Visible, &Collider, &GlobalTransform)>,
obstructions_query: &Query<&MapObstruction>,
events: &mut EventWriter<VisibilityChanged>,
@ -88,7 +87,10 @@ impl Viewshed {
let vision_distance = vision_distance::Circle::new(self.range);
let shape = Collider::cuboid(0.5, 0.5);
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(
size,
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>,
visible: Query<(&Visible, &Collider, &GlobalTransform)>,
obstructions: Query<&MapObstruction>,
@ -305,34 +307,29 @@ fn update_viewshed<D: 'static + Clone + Default + Send + Sync>(
&mut VisibleEntities,
&GlobalTransform,
)>,
map: Query<&Map<D>>,
rapier_context: Res<RapierContext>,
mut changed: EventWriter<VisibilityChanged>,
) {
if !config.query_pipeline_active {
return;
}
if let Ok(map) = map.get_single() {
let mut cache = HashMap::new();
for (viewer_entity, mut viewshed, mut visible_entities, viewer_transform) in
viewers.iter_mut()
for (viewer_entity, mut viewshed, mut visible_entities, viewer_transform) in viewers.iter_mut()
{
viewshed.update(
&viewer_entity,
&mut visible_entities,
viewer_transform,
&rapier_context,
map,
&visible,
&obstructions,
&mut changed,
&mut cache,
);
}
}
}
fn remove_visible<D: 'static + Clone + Default + Send + Sync>(
fn remove_visible(
removed: RemovedComponents<Visible>,
mut viewers: Query<(
Entity,
@ -340,7 +337,6 @@ fn remove_visible<D: 'static + Clone + Default + Send + Sync>(
&mut VisibleEntities,
&GlobalTransform,
)>,
map: Query<&Map<D>>,
rapier_context: Res<RapierContext>,
visible: Query<(&Visible, &Collider, &GlobalTransform)>,
obstructions: Query<&MapObstruction>,
@ -354,13 +350,11 @@ fn remove_visible<D: 'static + Clone + Default + Send + Sync>(
continue;
}
visible_entities.remove(&removed);
if let Ok(map) = map.get_single() {
viewshed.update(
&viewer_entity,
&mut visible_entities,
start,
&*rapier_context,
map,
&visible,
&obstructions,
&mut changed,
@ -369,7 +363,6 @@ fn remove_visible<D: 'static + Clone + Default + Send + Sync>(
}
}
}
}
}
fn update_revealed_tiles<D: 'static + Clone + Default + Send + Sync>(
@ -449,9 +442,9 @@ impl<D: 'static + Clone + Default + Send + Sync> Plugin for VisibilityPlugin<D>
fn build(&self, app: &mut App) {
app.add_event::<VisibilityChanged>()
.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, 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, log_visible);
}