Simplify visibility systems.

This commit is contained in:
Nolan Darilek 2021-05-21 12:04:10 -05:00
parent c49b6ac33f
commit 1878ead9be

View File

@ -89,12 +89,15 @@ fn map_visibility_indexing(
mut map: Query<(&Map, &mut VisibilityBlocked)>,
mut prev_index: ResMut<PreviousIndex>,
query: Query<
(Entity, &Coordinates, &BlocksVisibility),
(Entity, &Coordinates),
(
With<BlocksVisibility>,
Or<(Changed<Coordinates>, Changed<BlocksVisibility>)>,
),
>,
visibility_blockers: Query<&BlocksVisibility>,
) {
for (entity, coordinates, _) in query.iter() {
for (entity, coordinates) in query.iter() {
for (map, mut visibility_blocked) in map.iter_mut() {
let idx = coordinates.to_index(map.width());
if let Some(prev_idx) = prev_index.get(&entity) {
@ -130,14 +133,16 @@ fn remove_blocks_visibility(
if let Ok(coordinates) = coordinates.get_component::<Coordinates>(entity) {
prev_index.remove(&entity);
for (map, mut visibility_blocked) in map.iter_mut() {
let idx = (**coordinates).to_index(map.width());
let idx = coordinates.to_index(map.width());
let tile = map.base.tiles[idx];
let mut new_visibility_blocked = tile.blocks_visibility();
if !new_visibility_blocked {
for e in &map.entities[idx] {
new_visibility_blocked = new_visibility_blocked
|| blocks_visibility
.get_component::<BlocksVisibility>(*e)
.is_ok();
if blocks_visibility.get(*e).is_ok() {
new_visibility_blocked = true;
break;
}
}
}
visibility_blocked[idx] = new_visibility_blocked;
}
@ -195,25 +200,23 @@ fn update_viewshed(
}
}
fn map_visibility(
fn update_visible_and_revealed_tiles(
mut map: Query<
(&Map, &mut RevealedTiles, &mut VisibleTiles),
(
&Map,
&VisibilityBlocked,
&mut RevealedTiles,
&mut VisibleTiles,
),
With<VisibilityBlocked>,
Or<(Changed<Map>, Changed<VisibilityBlocked>)>,
),
>,
viewers: Query<(&Player, &Viewshed)>,
viewers: Query<&Viewshed, With<Player>>,
) {
for (_, viewshed) in viewers.iter() {
for (map, _, mut revealed_tiles, mut visible_tiles) in map.iter_mut() {
for (map, mut revealed_tiles, mut visible_tiles) in map.iter_mut() {
for viewshed in viewers.iter() {
for t in visible_tiles.iter_mut() {
*t = false
}
for v in viewshed.visible.iter() {
let idx = (*v).to_index(map.width());
let idx = v.to_index(map.width());
revealed_tiles[idx] = true;
visible_tiles[idx] = true;
}
@ -317,7 +320,7 @@ impl Plugin for VisibilityPlugin {
)
.add_system_to_stage(
CoreStage::PostUpdate,
map_visibility
update_visible_and_revealed_tiles
.system()
.label(MAP_VISIBILITY)
.after(UPDATE_VIEWSHED),