Simplify visibility calculation.

This commit is contained in:
Nolan Darilek 2021-09-16 14:27:08 -05:00
parent dc1c14fd23
commit 10fc85d384

View File

@ -107,7 +107,6 @@ fn update_viewshed(
collider_query: &QueryPipelineColliderComponentsQuery, collider_query: &QueryPipelineColliderComponentsQuery,
map: &Map, map: &Map,
blocks_visibility: &Query<&BlocksVisibility>, blocks_visibility: &Query<&BlocksVisibility>,
coordinates: &Query<&Coordinates>,
) { ) {
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);
@ -129,17 +128,9 @@ fn update_viewshed(
Some(&|v| v.entity() != *viewer_entity && blocks_visibility.get(v.entity()).is_ok()), Some(&|v| v.entity() != *viewer_entity && blocks_visibility.get(v.entity()).is_ok()),
|handle| { |handle| {
if let Ok(blocks_visibility) = blocks_visibility.get(handle.entity()) { if let Ok(blocks_visibility) = blocks_visibility.get(handle.entity()) {
if let Ok(coordinates) = coordinates.get(handle.entity()) { // println!("Visibility blocked at {:?}", coord);
if coordinates.i32() == (coord.x, coord.y) { opacity = **blocks_visibility;
opacity = **blocks_visibility; false
false
} else {
true
}
} else {
opacity = **blocks_visibility;
false
}
} else { } else {
true true
} }
@ -170,7 +161,6 @@ fn update_viewshed_for_coordinates(
query_pipeline: Res<QueryPipeline>, query_pipeline: Res<QueryPipeline>,
collider_query: QueryPipelineColliderComponentsQuery, collider_query: QueryPipelineColliderComponentsQuery,
blocks_visibility: Query<&BlocksVisibility>, blocks_visibility: Query<&BlocksVisibility>,
coordinates_query: Query<&Coordinates>,
) { ) {
for (visible_entity, 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() {
@ -188,7 +178,6 @@ fn update_viewshed_for_coordinates(
&collider_query, &collider_query,
map, map,
&blocks_visibility, &blocks_visibility,
&coordinates_query,
); );
} }
} }
@ -201,7 +190,6 @@ fn update_viewshed_for_start(
query_pipeline: Res<QueryPipeline>, query_pipeline: Res<QueryPipeline>,
collider_query: QueryPipelineColliderComponentsQuery, collider_query: QueryPipelineColliderComponentsQuery,
blocks_visibility: Query<&BlocksVisibility>, blocks_visibility: Query<&BlocksVisibility>,
coordinates_query: Query<&Coordinates>,
) { ) {
for (viewer_entity, mut viewshed, start) in viewers.iter_mut() { for (viewer_entity, mut viewshed, start) in viewers.iter_mut() {
if let Ok(map) = map.single() { if let Ok(map) = map.single() {
@ -213,7 +201,6 @@ fn update_viewshed_for_start(
&collider_query, &collider_query,
map, map,
&blocks_visibility, &blocks_visibility,
&coordinates_query,
); );
} }
} }
@ -226,7 +213,6 @@ fn remove_blocks_visibility(
query_pipeline: Res<QueryPipeline>, query_pipeline: Res<QueryPipeline>,
collider_query: QueryPipelineColliderComponentsQuery, collider_query: QueryPipelineColliderComponentsQuery,
blocks_visibility: Query<&BlocksVisibility>, blocks_visibility: Query<&BlocksVisibility>,
coordinates_query: Query<&Coordinates>,
) { ) {
for _ in removed.iter() { for _ in removed.iter() {
for (viewer_entity, mut viewshed, start) in viewers.iter_mut() { for (viewer_entity, mut viewshed, start) in viewers.iter_mut() {
@ -239,7 +225,6 @@ fn remove_blocks_visibility(
&collider_query, &collider_query,
map, map,
&blocks_visibility, &blocks_visibility,
&coordinates_query,
); );
} }
} }
@ -288,7 +273,7 @@ fn log_visible(
let collider_set = QueryPipelineColliderComponentsSet(&collider_query); let collider_set = QueryPipelineColliderComponentsSet(&collider_query);
let shape = Cuboid::new(Vec2::new(0.49, 0.49).into()); let shape = Cuboid::new(Vec2::new(0.49, 0.49).into());
for viewed_coordinates in &viewshed.visible { for viewed_coordinates in &viewshed.visible {
if coordinates.distance(viewed_coordinates) >= viewshed.range as f32 - 1.5 { if coordinates.distance(viewed_coordinates) >= viewshed.range as f32 - 0.1 {
continue; continue;
} }
let shape_pos = ( let shape_pos = (