Remove VisibleTiles
in favor of looking up directly on the viewshed.
This commit is contained in:
parent
b24a30a5d3
commit
425c041613
|
@ -12,7 +12,7 @@ use crate::{
|
|||
error::error_handler,
|
||||
map::Map,
|
||||
pathfinding::Destination,
|
||||
visibility::{RevealedTiles, Viewshed, Visible, VisibleEntities, VisibleTiles},
|
||||
visibility::{RevealedTiles, Viewshed, Visible, VisibleEntities},
|
||||
};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Reflect)]
|
||||
|
@ -324,8 +324,8 @@ fn navigate_to_explored<S, A: 'static>(
|
|||
fn exploration_changed_announcement(
|
||||
mut commands: Commands,
|
||||
mut tts: ResMut<Tts>,
|
||||
map: Query<(&Map, &RevealedTiles, &VisibleTiles)>,
|
||||
explorer: Query<(&Coordinates, &Exploring), Changed<Exploring>>,
|
||||
map: Query<(&Map, &RevealedTiles)>,
|
||||
explorer: Query<(&Coordinates, &Exploring, &Viewshed), Changed<Exploring>>,
|
||||
focused: Query<Entity, With<ExplorationFocused>>,
|
||||
explorable: Query<Entity, Or<(With<Visible>, With<Explorable>)>>,
|
||||
names: Query<&Name>,
|
||||
|
@ -334,15 +334,15 @@ fn exploration_changed_announcement(
|
|||
query_pipeline: Res<QueryPipeline>,
|
||||
collider_query: QueryPipelineColliderComponentsQuery,
|
||||
) -> 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 coordinates = coordinates.floor();
|
||||
for (map, revealed_tiles, visible_tiles) in map.iter() {
|
||||
for (map, revealed_tiles) in map.iter() {
|
||||
let point = **exploring;
|
||||
let idx = point.to_index(map.width);
|
||||
let shape = Cuboid::new(Vec2::new(0.49, 0.49).into());
|
||||
let known = revealed_tiles[idx];
|
||||
let visible = visible_tiles[idx];
|
||||
let visible = viewshed.is_point_visible(exploring);
|
||||
let fog_of_war = known && !visible;
|
||||
let description = if known {
|
||||
let mut tokens: Vec<&str> = vec![];
|
||||
|
|
|
@ -68,10 +68,6 @@ impl Visible {
|
|||
#[derive(Clone, Debug, Default, Deref, DerefMut)]
|
||||
pub struct VisibleEntities(HashSet<Entity>);
|
||||
|
||||
#[derive(Clone, Debug, Default, Deref, DerefMut, Reflect)]
|
||||
#[reflect(Component)]
|
||||
pub struct VisibleTiles(pub Vec<bool>);
|
||||
|
||||
#[derive(Bundle, Default)]
|
||||
pub struct ViewshedBundle {
|
||||
pub viewshed: Viewshed,
|
||||
|
@ -113,14 +109,11 @@ impl PointLike for Coord {
|
|||
|
||||
fn add_visibility_indices(
|
||||
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>,
|
||||
) {
|
||||
for (entity, map) in query.iter() {
|
||||
let count = map.width * map.height;
|
||||
commands
|
||||
.entity(entity)
|
||||
.insert(VisibleTiles(vec![false; count]));
|
||||
commands
|
||||
.entity(entity)
|
||||
.insert(RevealedTiles(vec![map_config.start_revealed; count]));
|
||||
|
@ -363,21 +356,17 @@ fn remove_visible(
|
|||
}
|
||||
|
||||
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>>,
|
||||
) {
|
||||
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 t in visible_tiles.iter_mut() {
|
||||
*t = false
|
||||
}
|
||||
for v in viewshed.visible_points.iter() {
|
||||
let idx = v.to_index(map.width);
|
||||
if idx >= revealed_tiles.len() || idx >= visible_tiles.len() {
|
||||
if idx >= revealed_tiles.len() {
|
||||
continue;
|
||||
}
|
||||
revealed_tiles[idx] = true;
|
||||
visible_tiles[idx] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -389,6 +378,7 @@ fn intersection(
|
|||
colliders: Query<Entity, With<VisibilityCollider>>,
|
||||
mut viewers: Query<&mut VisibleEntities>,
|
||||
visible: Query<Entity, With<Visible>>,
|
||||
names: Query<&Name>,
|
||||
mut visibility_changed: EventWriter<VisibilityChanged>,
|
||||
) {
|
||||
for event in events.iter() {
|
||||
|
@ -401,12 +391,22 @@ fn intersection(
|
|||
if let Some(viewshed_entity) = collider_to_viewshed.get(&visibility_collider) {
|
||||
if let Ok(mut visible_entities) = viewers.get_mut(*viewshed_entity) {
|
||||
if event.intersecting {
|
||||
println!(
|
||||
"{:?} is visible, {:?}",
|
||||
other,
|
||||
names.get(other).map(|v| v.to_string())
|
||||
);
|
||||
visibility_changed.send(VisibilityChanged::Gained {
|
||||
viewer: *viewshed_entity,
|
||||
viewed: other,
|
||||
});
|
||||
visible_entities.insert(other);
|
||||
} else {
|
||||
println!(
|
||||
"{:?} is no longer visible: {:?}",
|
||||
other,
|
||||
names.get(other).map(|v| v.to_string())
|
||||
);
|
||||
visibility_changed.send(VisibilityChanged::Lost {
|
||||
viewer: *viewshed_entity,
|
||||
viewed: other,
|
||||
|
|
Loading…
Reference in New Issue
Block a user