Lots of visibility refactoring and bugfixes.

This commit is contained in:
Nolan Darilek 2022-02-23 20:57:43 -06:00
parent dee62d2dca
commit 7786de18e3

View File

@ -53,36 +53,29 @@ impl Viewshed {
collider_query: &QueryPipelineColliderComponentsQuery,
map: &Map,
visible_query: &Query<&Visible>,
cache: &mut HashMap<(i32, i32), (HashSet<Entity>, u8)>,
events: &mut EventWriter<VisibilityChanged>,
) {
let mut context: Context<u8> = Context::default();
let vision_distance = vision_distance::Circle::new(self.range);
let shape = ColliderShape::cuboid(0.49, 0.49);
let shape = ColliderShape::cuboid(0.5, 0.5);
let origin = point!(start.x(), start.y());
let coord = Coord::new(start.x_i32(), start.y_i32());
let collider_set = QueryPipelineColliderComponentsSet(collider_query);
let mut new_visible_entities = HashSet::<Entity>::new();
let mut new_visible_entities = HashSet::new();
let visibility_grid = VisibilityGrid(
map,
RefCell::new(Box::new(|coord: Coord| {
if let Some((entities, opacity)) = cache.get(&(coord.x, coord.y)) {
for e in entities {
new_visible_entities.insert(*e);
}
return *opacity;
}
let tile = map.at(coord.x as usize, coord.y as usize);
if tile.blocks_visibility() {
return u8::MAX;
}
let mut entities = HashSet::new();
let shape_pos = (Vec2::new(coord.x as f32 + 0.5, coord.y as f32 + 0.5), 0.);
let dest = point!(coord.x as f32 + 0.5, coord.y as f32 + 0.5);
if na::distance(&origin, &dest) >= self.range as f32 {
if na::distance(&origin, &dest) > self.range as f32 {
return u8::MAX;
}
let mut opacity = 0;
let mut entities = HashSet::new();
let shape_pos = (Vec2::new(coord.x as f32 + 0.5, coord.y as f32 + 0.5), 0.);
query_pipeline.intersections_with_shape(
&collider_set,
&shape_pos.into(),
@ -103,7 +96,6 @@ impl Viewshed {
for e in &entities {
new_visible_entities.insert(*e);
}
cache.insert((coord.x, coord.y), (entities, opacity));
opacity
})),
);
@ -268,12 +260,12 @@ fn update_viewshed(
if visible.get(entity).is_err() {
continue;
}
for (viewer_entity, viewshed, _, viewer_coordinates) in viewers.iter_mut() {
for (viewer_entity, viewshed, visible_entities, viewer_coordinates) in viewers.iter_mut() {
if viewer_entity != entity
&& viewer_coordinates.distance(&coordinates) <= viewshed.range as f32
&& (viewer_coordinates.distance(&coordinates) <= viewshed.range as f32
|| visible_entities.contains(&entity))
{
to_update.insert(viewer_entity);
continue;
}
}
}
@ -282,7 +274,6 @@ fn update_viewshed(
viewers.get_mut(*entity)
{
if let Ok(map) = map.get_single() {
let mut cache = HashMap::new();
viewshed.update(
&viewer_entity,
&mut visible_entities,
@ -291,7 +282,6 @@ fn update_viewshed(
&collider_query,
map,
&visible,
&mut cache,
&mut changed,
);
}
@ -315,7 +305,6 @@ fn remove_visible(
}
visible_entities.remove(&removed);
if let Ok(map) = map.get_single() {
let mut cache = HashMap::new();
viewshed.update(
&viewer_entity,
&mut visible_entities,
@ -324,7 +313,6 @@ fn remove_visible(
&collider_query,
map,
&visible,
&mut cache,
&mut changed,
);
}
@ -409,12 +397,8 @@ impl Plugin for VisibilityPlugin {
fn build(&self, app: &mut App) {
app.add_event::<VisibilityChanged>()
.add_system(add_visibility_indices)
.add_system(update_viewshed)
.add_system_to_stage(CoreStage::PostUpdate, viewshed_removed)
.add_system_set(
SystemSet::new()
.with_run_criteria(FixedTimestep::step(0.05))
.with_system(update_viewshed),
)
.add_system_to_stage(CoreStage::PostUpdate, remove_visible)
.add_system_to_stage(CoreStage::PostUpdate, update_revealed_tiles)
.add_system_to_stage(CoreStage::PostUpdate, log_visible);