Remove VisibleTiles in favor of looking up directly on the viewshed.

This commit is contained in:
Nolan Darilek 2021-09-27 13:45:52 -05:00
parent b24a30a5d3
commit 425c041613
2 changed files with 21 additions and 21 deletions

View File

@ -12,7 +12,7 @@ use crate::{
error::error_handler, error::error_handler,
map::Map, map::Map,
pathfinding::Destination, pathfinding::Destination,
visibility::{RevealedTiles, Viewshed, Visible, VisibleEntities, VisibleTiles}, visibility::{RevealedTiles, Viewshed, Visible, VisibleEntities},
}; };
#[derive(Clone, Copy, Debug, Default, PartialEq, Reflect)] #[derive(Clone, Copy, Debug, Default, PartialEq, Reflect)]
@ -324,8 +324,8 @@ fn navigate_to_explored<S, A: 'static>(
fn exploration_changed_announcement( fn exploration_changed_announcement(
mut commands: Commands, mut commands: Commands,
mut tts: ResMut<Tts>, mut tts: ResMut<Tts>,
map: Query<(&Map, &RevealedTiles, &VisibleTiles)>, map: Query<(&Map, &RevealedTiles)>,
explorer: Query<(&Coordinates, &Exploring), Changed<Exploring>>, explorer: Query<(&Coordinates, &Exploring, &Viewshed), Changed<Exploring>>,
focused: Query<Entity, With<ExplorationFocused>>, focused: Query<Entity, With<ExplorationFocused>>,
explorable: Query<Entity, Or<(With<Visible>, With<Explorable>)>>, explorable: Query<Entity, Or<(With<Visible>, With<Explorable>)>>,
names: Query<&Name>, names: Query<&Name>,
@ -334,15 +334,15 @@ fn exploration_changed_announcement(
query_pipeline: Res<QueryPipeline>, query_pipeline: Res<QueryPipeline>,
collider_query: QueryPipelineColliderComponentsQuery, collider_query: QueryPipelineColliderComponentsQuery,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
if let Ok((coordinates, exploring)) = explorer.single() { if let Ok((coordinates, exploring, viewshed)) = explorer.single() {
let collider_set = QueryPipelineColliderComponentsSet(&collider_query); let collider_set = QueryPipelineColliderComponentsSet(&collider_query);
let coordinates = coordinates.floor(); let coordinates = coordinates.floor();
for (map, revealed_tiles, visible_tiles) in map.iter() { for (map, revealed_tiles) in map.iter() {
let point = **exploring; let point = **exploring;
let idx = point.to_index(map.width); let idx = point.to_index(map.width);
let shape = Cuboid::new(Vec2::new(0.49, 0.49).into()); let shape = Cuboid::new(Vec2::new(0.49, 0.49).into());
let known = revealed_tiles[idx]; let known = revealed_tiles[idx];
let visible = visible_tiles[idx]; let visible = viewshed.is_point_visible(exploring);
let fog_of_war = known && !visible; let fog_of_war = known && !visible;
let description = if known { let description = if known {
let mut tokens: Vec<&str> = vec![]; let mut tokens: Vec<&str> = vec![];

View File

@ -68,10 +68,6 @@ impl Visible {
#[derive(Clone, Debug, Default, Deref, DerefMut)] #[derive(Clone, Debug, Default, Deref, DerefMut)]
pub struct VisibleEntities(HashSet<Entity>); pub struct VisibleEntities(HashSet<Entity>);
#[derive(Clone, Debug, Default, Deref, DerefMut, Reflect)]
#[reflect(Component)]
pub struct VisibleTiles(pub Vec<bool>);
#[derive(Bundle, Default)] #[derive(Bundle, Default)]
pub struct ViewshedBundle { pub struct ViewshedBundle {
pub viewshed: Viewshed, pub viewshed: Viewshed,
@ -113,14 +109,11 @@ impl PointLike for Coord {
fn add_visibility_indices( fn add_visibility_indices(
mut commands: Commands, mut commands: Commands,
query: Query<(Entity, &Map), (Added<Map>, Without<VisibleTiles>, Without<RevealedTiles>)>, query: Query<(Entity, &Map), (Added<Map>, Without<RevealedTiles>)>,
map_config: Res<MapConfig>, map_config: Res<MapConfig>,
) { ) {
for (entity, map) in query.iter() { for (entity, map) in query.iter() {
let count = map.width * map.height; let count = map.width * map.height;
commands
.entity(entity)
.insert(VisibleTiles(vec![false; count]));
commands commands
.entity(entity) .entity(entity)
.insert(RevealedTiles(vec![map_config.start_revealed; count])); .insert(RevealedTiles(vec![map_config.start_revealed; count]));
@ -363,21 +356,17 @@ fn remove_visible(
} }
fn update_visible_and_revealed_tiles( fn update_visible_and_revealed_tiles(
mut map: Query<(&Map, &mut RevealedTiles, &mut VisibleTiles)>, mut map: Query<(&Map, &mut RevealedTiles)>,
viewers: Query<&Viewshed, With<Player>>, viewers: Query<&Viewshed, With<Player>>,
) { ) {
for (map, mut revealed_tiles, mut visible_tiles) in map.iter_mut() { for (map, mut revealed_tiles) in map.iter_mut() {
for viewshed in viewers.iter() { for viewshed in viewers.iter() {
for t in visible_tiles.iter_mut() {
*t = false
}
for v in viewshed.visible_points.iter() { for v in viewshed.visible_points.iter() {
let idx = v.to_index(map.width); let idx = v.to_index(map.width);
if idx >= revealed_tiles.len() || idx >= visible_tiles.len() { if idx >= revealed_tiles.len() {
continue; continue;
} }
revealed_tiles[idx] = true; revealed_tiles[idx] = true;
visible_tiles[idx] = true;
} }
} }
} }
@ -389,6 +378,7 @@ fn intersection(
colliders: Query<Entity, With<VisibilityCollider>>, colliders: Query<Entity, With<VisibilityCollider>>,
mut viewers: Query<&mut VisibleEntities>, mut viewers: Query<&mut VisibleEntities>,
visible: Query<Entity, With<Visible>>, visible: Query<Entity, With<Visible>>,
names: Query<&Name>,
mut visibility_changed: EventWriter<VisibilityChanged>, mut visibility_changed: EventWriter<VisibilityChanged>,
) { ) {
for event in events.iter() { for event in events.iter() {
@ -401,12 +391,22 @@ fn intersection(
if let Some(viewshed_entity) = collider_to_viewshed.get(&visibility_collider) { if let Some(viewshed_entity) = collider_to_viewshed.get(&visibility_collider) {
if let Ok(mut visible_entities) = viewers.get_mut(*viewshed_entity) { if let Ok(mut visible_entities) = viewers.get_mut(*viewshed_entity) {
if event.intersecting { if event.intersecting {
println!(
"{:?} is visible, {:?}",
other,
names.get(other).map(|v| v.to_string())
);
visibility_changed.send(VisibilityChanged::Gained { visibility_changed.send(VisibilityChanged::Gained {
viewer: *viewshed_entity, viewer: *viewshed_entity,
viewed: other, viewed: other,
}); });
visible_entities.insert(other); visible_entities.insert(other);
} else { } else {
println!(
"{:?} is no longer visible: {:?}",
other,
names.get(other).map(|v| v.to_string())
);
visibility_changed.send(VisibilityChanged::Lost { visibility_changed.send(VisibilityChanged::Lost {
viewer: *viewshed_entity, viewer: *viewshed_entity,
viewed: other, viewed: other,