Visibility system cleanup.

This commit is contained in:
Nolan Darilek 2021-08-23 10:14:03 -05:00
parent 60ad00276e
commit d33f26d31c

View File

@ -112,7 +112,6 @@ fn update_viewshed(
let mut context: Context<u8> = Context::default(); let mut context: Context<u8> = Context::default();
let vision_distance = vision_distance::Circle::new(viewshed.range); let vision_distance = vision_distance::Circle::new(viewshed.range);
let coord = Coord::new(start.x_i32(), start.y_i32()); let coord = Coord::new(start.x_i32(), start.y_i32());
viewshed.visible.clear();
let shape = Cuboid::new(Vec2::new(0.49, 0.49).into()); let shape = Cuboid::new(Vec2::new(0.49, 0.49).into());
let range = viewshed.range as f32; let range = viewshed.range as f32;
let collider_set = QueryPipelineColliderComponentsSet(&collider_query); let collider_set = QueryPipelineColliderComponentsSet(&collider_query);
@ -148,6 +147,7 @@ fn update_viewshed(
); );
opacity opacity
}); });
let mut new_visible = HashSet::new();
context.for_each_visible( context.for_each_visible(
coord, coord,
&visibility_grid, &visibility_grid,
@ -155,13 +155,16 @@ fn update_viewshed(
vision_distance, vision_distance,
255, 255,
|coord, _directions, _visibility| { |coord, _directions, _visibility| {
viewshed.visible.insert((coord.x, coord.y)); new_visible.insert((coord.x, coord.y));
}, },
); );
if viewshed.visible != new_visible {
viewshed.visible = new_visible;
}
} }
fn update_viewshed_for_coordinates( fn update_viewshed_for_coordinates(
visible: Query<&Coordinates, (Changed<Coordinates>, With<BlocksVisibility>)>, visible: Query<(Entity, &Coordinates), (Changed<Coordinates>, With<BlocksVisibility>)>,
mut viewers: Query<(Entity, &mut Viewshed, &Coordinates)>, mut viewers: Query<(Entity, &mut Viewshed, &Coordinates)>,
map: Query<&Map>, map: Query<&Map>,
query_pipeline: Res<QueryPipeline>, query_pipeline: Res<QueryPipeline>,
@ -169,9 +172,11 @@ fn update_viewshed_for_coordinates(
blocks_visibility: Query<&BlocksVisibility>, blocks_visibility: Query<&BlocksVisibility>,
coordinates_query: Query<&Coordinates>, coordinates_query: Query<&Coordinates>,
) { ) {
for coordinates in visible.iter() { for (visible_entity, coordinates) in visible.iter() {
for (viewer_entity, mut viewshed, start) in viewers.iter_mut() { for (viewer_entity, mut viewshed, start) in viewers.iter_mut() {
if coordinates.distance(start) > viewshed.range as f32 { if viewer_entity == visible_entity
|| coordinates.distance(start) > viewshed.range as f32
{
continue; continue;
} }
if let Ok(map) = map.single() { if let Ok(map) = map.single() {
@ -273,12 +278,7 @@ fn log_visible(
for timer in recently_lost.values_mut() { for timer in recently_lost.values_mut() {
timer.tick(time.delta()); timer.tick(time.delta());
} }
let recently_lost_clone = recently_lost.clone(); recently_lost.retain(|_, v| !v.finished());
for (entity, timer) in recently_lost_clone.iter() {
if timer.finished() {
recently_lost.remove(&entity);
}
}
let mut new_seen = HashSet::new(); let mut new_seen = HashSet::new();
if let Ok(mut log) = log.single_mut() { if let Ok(mut log) = log.single_mut() {
for (viewer_entity, viewshed, coordinates, transform) in viewers.iter() { for (viewer_entity, viewshed, coordinates, transform) in viewers.iter() {