Update opacity map when collider AABBs change.

This commit is contained in:
Nolan Darilek 2024-12-04 18:28:52 -06:00
parent 56f4b6ce14
commit 0185b9c590

View File

@ -61,7 +61,7 @@ impl Viewshed {
let visibility_grid = VisibilityGrid(size, *viewer_entity, opacity_map.clone()); let visibility_grid = VisibilityGrid(size, *viewer_entity, opacity_map.clone());
let mut new_visible = HashSet::new(); let mut new_visible = HashSet::new();
let mut new_visible_entities = HashSet::new(); let mut new_visible_entities = HashSet::new();
println!("Start: {viewer_entity}"); // println!("Start: {viewer_entity}");
context.for_each_visible( context.for_each_visible(
Coord::new(start.x_i32(), start.y_i32()), Coord::new(start.x_i32(), start.y_i32()),
&visibility_grid, &visibility_grid,
@ -71,17 +71,17 @@ impl Viewshed {
|coord, _directions, _visibility| { |coord, _directions, _visibility| {
let coord = IVec2::new(coord.x, coord.y); let coord = IVec2::new(coord.x, coord.y);
new_visible.insert(coord); new_visible.insert(coord);
println!("Checking {coord:?}"); // println!("Checking {coord:?}");
if let Some((_, entities)) = opacity_map.get(&coord) { if let Some((_, entities)) = opacity_map.get(&coord) {
for e in entities { for e in entities {
if entities.len() == 1 || sensors.contains(*e) { if entities.len() == 1 || sensors.contains(*e) {
println!("Spotted {e:?}"); // println!("Spotted {e:?}");
new_visible_entities.insert(*e); new_visible_entities.insert(*e);
} else { } else {
let should_push = std::cell::RefCell::new(true); let should_push = std::cell::RefCell::new(true);
let coord = Vec2::new(coord.x as f32 + 0.5, coord.y as f32 + 0.5); let coord = Vec2::new(coord.x as f32 + 0.5, coord.y as f32 + 0.5);
let dir = Dir2::new_unchecked((coord - *start).normalize()); let dir = Dir2::new_unchecked((coord - *start).normalize());
println!("Casting from {coord} to {dir:?}"); // println!("Casting from {coord} to {dir:?}");
spatial_query.cast_ray_predicate( spatial_query.cast_ray_predicate(
*start, *start,
dir, dir,
@ -90,7 +90,7 @@ impl Viewshed {
default(), default(),
&|entity| { &|entity| {
if *e != entity && entities.contains(e) { if *e != entity && entities.contains(e) {
println!("{entities:?} contains {e}"); // println!("{entities:?} contains {e}");
*should_push.borrow_mut() = false; *should_push.borrow_mut() = false;
} }
true true
@ -109,11 +109,11 @@ impl Viewshed {
} }
if new_visible_entities != **visible_entities { if new_visible_entities != **visible_entities {
for lost in visible_entities.difference(&new_visible_entities) { for lost in visible_entities.difference(&new_visible_entities) {
println!("Lost {lost}"); // println!("Lost {lost}");
commands.trigger_targets(VisibilityChanged::Lost(*lost), *viewer_entity); commands.trigger_targets(VisibilityChanged::Lost(*lost), *viewer_entity);
} }
for gained in new_visible_entities.difference(visible_entities) { for gained in new_visible_entities.difference(visible_entities) {
println!("Gained {gained}"); // println!("Gained {gained}");
commands.trigger_targets(VisibilityChanged::Gained(*gained), *viewer_entity); commands.trigger_targets(VisibilityChanged::Gained(*gained), *viewer_entity);
} }
**visible_entities = new_visible_entities; **visible_entities = new_visible_entities;
@ -180,7 +180,11 @@ fn update_opacity_map(
spatial_query: SpatialQuery, spatial_query: SpatialQuery,
query: Query< query: Query<
(Entity, &GlobalTransform, &ColliderAabb, &Visible), (Entity, &GlobalTransform, &ColliderAabb, &Visible),
Or<(Changed<GlobalTransform>, Changed<Visible>)>, Or<(
Changed<GlobalTransform>,
Changed<ColliderAabb>,
Changed<Visible>,
)>,
>, >,
visible: Query<(Entity, &GlobalTransform, &Collider, &Visible)>, visible: Query<(Entity, &GlobalTransform, &Collider, &Visible)>,
) { ) {