From 1878ead9be766774a8e7f60f9a65e6f29c10676a Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Fri, 21 May 2021 12:04:10 -0500 Subject: [PATCH] Simplify visibility systems. --- src/visibility.rs | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/visibility.rs b/src/visibility.rs index 236ac6e..da845ae 100644 --- a/src/visibility.rs +++ b/src/visibility.rs @@ -89,12 +89,15 @@ fn map_visibility_indexing( mut map: Query<(&Map, &mut VisibilityBlocked)>, mut prev_index: ResMut, query: Query< - (Entity, &Coordinates, &BlocksVisibility), - Or<(Changed, Changed)>, + (Entity, &Coordinates), + ( + With, + Or<(Changed, Changed)>, + ), >, 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::(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(); - for e in &map.entities[idx] { - new_visibility_blocked = new_visibility_blocked - || blocks_visibility - .get_component::(*e) - .is_ok(); + if !new_visibility_blocked { + for e in &map.entities[idx] { + 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, + Or<(Changed, Changed)>, ), - Or<(Changed, Changed)>, >, - viewers: Query<(&Player, &Viewshed)>, + viewers: Query<&Viewshed, With>, ) { - 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),