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