2021-09-28 17:49:44 +00:00
|
|
|
use std::{
|
|
|
|
cell::RefCell,
|
|
|
|
collections::{HashMap, HashSet},
|
2022-03-15 15:37:28 +00:00
|
|
|
marker::PhantomData,
|
2021-09-28 17:49:44 +00:00
|
|
|
};
|
2021-05-13 17:25:45 +00:00
|
|
|
|
2022-07-20 13:24:38 +00:00
|
|
|
use bevy::prelude::*;
|
2022-07-11 15:26:03 +00:00
|
|
|
|
2024-09-22 19:45:36 +00:00
|
|
|
use bevy_rapier2d::{na::Isometry2, parry::bounding_volume::BoundingVolume};
|
2021-05-13 17:25:45 +00:00
|
|
|
use coord_2d::{Coord, Size};
|
|
|
|
use shadowcast::{vision_distance, Context, InputGrid};
|
|
|
|
|
|
|
|
use crate::{
|
2021-06-09 19:13:09 +00:00
|
|
|
bevy_rapier2d::prelude::*,
|
2022-07-19 16:24:45 +00:00
|
|
|
core::{GlobalTransformExt, Player, PointLike},
|
2021-05-13 17:25:45 +00:00
|
|
|
log::Log,
|
2024-08-11 14:08:20 +00:00
|
|
|
map::{Map, MapPlugin},
|
2021-05-13 17:25:45 +00:00
|
|
|
};
|
|
|
|
|
2022-01-10 19:50:52 +00:00
|
|
|
#[derive(Component, Clone, Copy, Debug, Default, Reflect)]
|
2021-05-17 14:50:03 +00:00
|
|
|
#[reflect(Component)]
|
|
|
|
pub struct DontLogWhenVisible;
|
|
|
|
|
2022-01-10 19:50:52 +00:00
|
|
|
#[derive(Component, Clone, Debug, Default, Deref, DerefMut, Reflect)]
|
2021-05-13 17:25:45 +00:00
|
|
|
#[reflect(Component)]
|
|
|
|
pub struct RevealedTiles(pub Vec<bool>);
|
|
|
|
|
2022-01-10 19:50:52 +00:00
|
|
|
#[derive(Component, Clone, Debug, Eq, PartialEq)]
|
2021-05-13 17:25:45 +00:00
|
|
|
pub struct Viewshed {
|
|
|
|
pub range: u32,
|
2021-09-21 11:38:51 +00:00
|
|
|
pub visible_points: HashSet<(i32, i32)>,
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Viewshed {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
range: 15,
|
2021-09-21 11:38:51 +00:00
|
|
|
visible_points: HashSet::new(),
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Viewshed {
|
2021-09-21 11:38:51 +00:00
|
|
|
pub fn is_point_visible(&self, point: &dyn PointLike) -> bool {
|
|
|
|
self.visible_points.contains(&point.into())
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
2022-05-06 16:07:59 +00:00
|
|
|
|
2022-07-25 18:52:50 +00:00
|
|
|
fn update(
|
2021-10-12 21:59:59 +00:00
|
|
|
&mut self,
|
2024-09-22 19:45:36 +00:00
|
|
|
commands: &mut Commands,
|
2021-10-12 21:59:59 +00:00
|
|
|
viewer_entity: &Entity,
|
|
|
|
visible_entities: &mut VisibleEntities,
|
2024-08-30 20:50:10 +00:00
|
|
|
start: &Vec2,
|
2022-05-06 16:07:59 +00:00
|
|
|
rapier_context: &RapierContext,
|
2022-07-20 13:24:38 +00:00
|
|
|
visible_query: &Query<(&Visible, &Collider, &GlobalTransform)>,
|
2024-09-22 19:45:36 +00:00
|
|
|
cache: &mut RefCell<HashMap<(i32, i32), (u8, HashSet<Entity>)>>,
|
2021-10-12 21:59:59 +00:00
|
|
|
) {
|
2022-07-20 13:24:38 +00:00
|
|
|
// println!("Start");
|
|
|
|
let mut boxes = vec![];
|
|
|
|
let shape = Collider::cuboid(self.range as f32, self.range as f32);
|
2024-09-22 19:45:36 +00:00
|
|
|
rapier_context.intersections_with_shape(*start, 0., &shape, default(), |entity| {
|
|
|
|
if let Ok((_, collider, transform)) = visible_query.get(entity) {
|
|
|
|
let position =
|
|
|
|
Isometry2::translation(transform.translation().x, transform.translation().y);
|
|
|
|
// println!(
|
|
|
|
// "Hit {:?}, pushing {:?}",
|
|
|
|
// entity,
|
|
|
|
// collider.raw.compute_aabb(&position)
|
|
|
|
// );
|
|
|
|
boxes.push(collider.raw.compute_aabb(&position));
|
|
|
|
}
|
|
|
|
true
|
|
|
|
});
|
2021-10-12 21:59:59 +00:00
|
|
|
let mut context: Context<u8> = Context::default();
|
|
|
|
let vision_distance = vision_distance::Circle::new(self.range);
|
2024-09-22 19:45:36 +00:00
|
|
|
let shape = Collider::cuboid(0.49, 0.49);
|
2022-07-25 18:52:50 +00:00
|
|
|
let size = (
|
2024-09-01 21:21:26 +00:00
|
|
|
(start.x.abs() + self.range as f32 * 2.) as u32,
|
|
|
|
(start.y.abs() + self.range as f32 * 2.) as u32,
|
2022-07-25 18:52:50 +00:00
|
|
|
);
|
2021-10-12 21:59:59 +00:00
|
|
|
let visibility_grid = VisibilityGrid(
|
2022-07-11 15:26:03 +00:00
|
|
|
size,
|
2021-10-12 21:59:59 +00:00
|
|
|
RefCell::new(Box::new(|coord: Coord| {
|
2022-07-11 15:26:03 +00:00
|
|
|
let shape_pos = Vec2::new(coord.x as f32 + 0.5, coord.y as f32 + 0.5);
|
2024-09-22 19:45:36 +00:00
|
|
|
// println!("Checking {coord:?}: {shape_pos:?}");
|
2022-08-29 17:24:32 +00:00
|
|
|
if start.distance(&shape_pos) > self.range as f32 {
|
2022-07-20 13:24:38 +00:00
|
|
|
// println!("Out of range");
|
2022-05-17 15:37:46 +00:00
|
|
|
return u8::MAX;
|
|
|
|
}
|
2024-09-22 19:45:36 +00:00
|
|
|
let mut coord_entities = HashSet::new();
|
|
|
|
let mut to_insert = None;
|
|
|
|
let opacity = if let Some((opacity, entities)) =
|
|
|
|
cache.borrow().get(&(coord.x, coord.y))
|
2022-07-20 15:12:14 +00:00
|
|
|
{
|
2024-09-22 19:45:36 +00:00
|
|
|
// println!("Cache hit: {opacity:?}: {entities:?}");
|
|
|
|
coord_entities = entities.clone();
|
|
|
|
*opacity
|
2022-07-20 15:12:14 +00:00
|
|
|
} else {
|
2024-09-22 19:45:36 +00:00
|
|
|
let position = Isometry2::translation(shape_pos.x, shape_pos.y);
|
|
|
|
let aabb = shape.raw.compute_aabb(&position);
|
|
|
|
if boxes.iter().any(|b| b.intersects(&aabb)) {
|
|
|
|
// println!("Hit");
|
2022-07-20 15:12:14 +00:00
|
|
|
let mut opacity = 0;
|
|
|
|
rapier_context.intersections_with_shape(
|
|
|
|
shape_pos,
|
|
|
|
0.,
|
|
|
|
&shape,
|
2024-09-22 19:45:36 +00:00
|
|
|
default(),
|
2022-07-20 15:12:14 +00:00
|
|
|
|entity| {
|
|
|
|
if let Ok((visible, _, _)) = visible_query.get(entity) {
|
2024-09-22 19:45:36 +00:00
|
|
|
// println!(
|
|
|
|
// "{entity:?}: {visible:?}: {:?}",
|
|
|
|
// transform.translation().truncate()
|
|
|
|
// );
|
2024-09-01 21:21:26 +00:00
|
|
|
coord_entities.insert(entity);
|
2022-07-20 15:12:14 +00:00
|
|
|
opacity = opacity.max(**visible);
|
|
|
|
}
|
2024-08-11 14:08:20 +00:00
|
|
|
true
|
2022-07-20 15:12:14 +00:00
|
|
|
},
|
|
|
|
);
|
2024-09-22 19:45:36 +00:00
|
|
|
to_insert = Some(((coord.x, coord.y), (opacity, coord_entities.clone())));
|
|
|
|
// println!("New opacity: {opacity}");
|
|
|
|
opacity
|
2022-07-20 15:12:14 +00:00
|
|
|
} else {
|
|
|
|
// println!("No hits, 0");
|
2024-09-22 19:45:36 +00:00
|
|
|
to_insert = Some(((coord.x, coord.y), default()));
|
|
|
|
0
|
2022-07-20 15:12:14 +00:00
|
|
|
}
|
|
|
|
};
|
2024-09-22 19:45:36 +00:00
|
|
|
if let Some((k, v)) = to_insert {
|
|
|
|
cache.borrow_mut().insert(k, v);
|
|
|
|
}
|
|
|
|
if coord_entities.contains(&viewer_entity) {
|
|
|
|
let mut coord_entities = coord_entities.clone();
|
|
|
|
coord_entities.retain(|e| e != viewer_entity);
|
|
|
|
let opacity = coord_entities
|
|
|
|
.iter()
|
|
|
|
.map(|v| visible_query.get(*v).unwrap().0 .0)
|
|
|
|
.max()
|
|
|
|
.unwrap_or(0);
|
|
|
|
// println!("Viewer hit, removing viewer opacity: {opacity}");
|
2024-08-11 14:08:20 +00:00
|
|
|
opacity
|
2022-05-17 15:37:46 +00:00
|
|
|
} else {
|
2024-09-22 19:45:36 +00:00
|
|
|
opacity
|
2022-05-17 15:37:46 +00:00
|
|
|
}
|
2021-10-12 21:59:59 +00:00
|
|
|
})),
|
|
|
|
);
|
|
|
|
let mut new_visible = HashSet::new();
|
2024-09-22 19:45:36 +00:00
|
|
|
let mut new_visible_entities = HashSet::new();
|
2024-09-01 21:21:26 +00:00
|
|
|
// println!("Start: {viewer_entity}");
|
2021-10-12 21:59:59 +00:00
|
|
|
context.for_each_visible(
|
2022-05-18 15:28:42 +00:00
|
|
|
Coord::new(start.x_i32(), start.y_i32()),
|
2021-10-12 21:59:59 +00:00
|
|
|
&visibility_grid,
|
2022-07-11 15:26:03 +00:00
|
|
|
&size,
|
2021-10-12 21:59:59 +00:00
|
|
|
vision_distance,
|
2022-07-11 15:26:03 +00:00
|
|
|
u8::MAX,
|
2021-10-12 21:59:59 +00:00
|
|
|
|coord, _directions, _visibility| {
|
|
|
|
new_visible.insert((coord.x, coord.y));
|
2024-09-22 19:45:36 +00:00
|
|
|
if let Some((_, entities)) = cache.borrow().get(&(coord.x, coord.y)) {
|
|
|
|
for e in entities {
|
|
|
|
new_visible_entities.insert(*e);
|
|
|
|
}
|
|
|
|
}
|
2021-10-12 21:59:59 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
if self.visible_points != new_visible {
|
|
|
|
self.visible_points = new_visible;
|
|
|
|
}
|
|
|
|
if new_visible_entities != **visible_entities {
|
|
|
|
for lost in visible_entities.difference(&new_visible_entities) {
|
2024-09-22 19:45:36 +00:00
|
|
|
commands.trigger_targets(VisibilityChanged::Lost(*lost), *viewer_entity);
|
2021-10-12 21:59:59 +00:00
|
|
|
}
|
|
|
|
for gained in new_visible_entities.difference(visible_entities) {
|
2024-09-22 19:45:36 +00:00
|
|
|
// println!("transition: {:?} gains {:?}", viewer_entity,
|
|
|
|
// gained);
|
|
|
|
commands.trigger_targets(VisibilityChanged::Gained(*gained), *viewer_entity);
|
2021-10-12 21:59:59 +00:00
|
|
|
}
|
|
|
|
**visible_entities = new_visible_entities;
|
|
|
|
}
|
|
|
|
}
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
|
|
|
|
2024-08-25 22:59:57 +00:00
|
|
|
#[derive(Component, Clone, Copy, Debug, Deref, DerefMut, Eq, PartialEq, Reflect)]
|
2021-09-22 13:23:01 +00:00
|
|
|
#[reflect(Component)]
|
|
|
|
pub struct Visible(pub u8);
|
|
|
|
|
|
|
|
impl Default for Visible {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::opaque()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Visible {
|
|
|
|
pub fn transparent() -> Self {
|
|
|
|
Self(0)
|
|
|
|
}
|
|
|
|
|
2021-09-23 17:58:39 +00:00
|
|
|
pub fn opaque() -> Self {
|
2021-09-22 13:23:01 +00:00
|
|
|
Self(u8::MAX)
|
|
|
|
}
|
|
|
|
}
|
2021-10-12 21:59:59 +00:00
|
|
|
|
2022-01-10 19:50:52 +00:00
|
|
|
#[derive(Component, Clone, Debug, Default, Deref, DerefMut)]
|
2021-09-21 11:38:51 +00:00
|
|
|
pub struct VisibleEntities(HashSet<Entity>);
|
|
|
|
|
|
|
|
#[derive(Bundle, Default)]
|
|
|
|
pub struct ViewshedBundle {
|
|
|
|
pub viewshed: Viewshed,
|
|
|
|
pub visible_entities: VisibleEntities,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ViewshedBundle {
|
|
|
|
pub fn new(range: u32) -> Self {
|
|
|
|
Self {
|
2022-05-10 18:56:49 +00:00
|
|
|
viewshed: Viewshed { range, ..default() },
|
|
|
|
..default()
|
2021-09-21 11:38:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-08 21:12:35 +00:00
|
|
|
#[derive(Event)]
|
2021-09-21 11:38:51 +00:00
|
|
|
pub enum VisibilityChanged {
|
2024-09-22 19:45:36 +00:00
|
|
|
Gained(Entity),
|
|
|
|
Lost(Entity),
|
2021-09-21 11:38:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 22:42:04 +00:00
|
|
|
impl PointLike for Coord {
|
|
|
|
fn x(&self) -> f32 {
|
|
|
|
self.x as f32
|
|
|
|
}
|
|
|
|
|
|
|
|
fn y(&self) -> f32 {
|
|
|
|
self.y as f32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 15:15:09 +00:00
|
|
|
fn add_visibility_indices<MapData: 'static + Clone + Default + Send + Sync>(
|
2021-05-13 17:25:45 +00:00
|
|
|
mut commands: Commands,
|
2022-12-20 15:15:09 +00:00
|
|
|
map_config: Res<MapPlugin<MapData>>,
|
|
|
|
query: Query<(Entity, &Map<MapData>), (Added<Map<MapData>>, Without<RevealedTiles>)>,
|
2021-05-13 17:25:45 +00:00
|
|
|
) {
|
2023-03-28 17:13:23 +00:00
|
|
|
for (entity, map) in &query {
|
2021-06-09 19:53:48 +00:00
|
|
|
let count = map.width * map.height;
|
2021-05-13 17:25:45 +00:00
|
|
|
commands
|
|
|
|
.entity(entity)
|
|
|
|
.insert(RevealedTiles(vec![map_config.start_revealed; count]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 11:38:51 +00:00
|
|
|
fn viewshed_removed(
|
2024-09-22 19:45:36 +00:00
|
|
|
mut commands: Commands,
|
2023-03-28 16:57:37 +00:00
|
|
|
mut query: RemovedComponents<Viewshed>,
|
2021-09-28 17:49:44 +00:00
|
|
|
visible_entities: Query<&VisibleEntities>,
|
2021-09-21 11:38:51 +00:00
|
|
|
) {
|
2024-02-09 20:59:16 +00:00
|
|
|
for entity in query.read() {
|
2021-09-28 17:49:44 +00:00
|
|
|
if let Ok(visible) = visible_entities.get(entity) {
|
|
|
|
for e in visible.iter() {
|
2024-09-22 19:45:36 +00:00
|
|
|
commands.trigger_targets(VisibilityChanged::Lost(*e), entity);
|
2021-09-28 17:49:44 +00:00
|
|
|
}
|
2021-09-21 11:38:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 15:26:03 +00:00
|
|
|
pub struct VisibilityGrid<F>(pub (u32, u32), pub RefCell<Box<F>>);
|
2021-05-13 17:25:45 +00:00
|
|
|
|
2022-07-11 15:26:03 +00:00
|
|
|
impl<F> InputGrid for VisibilityGrid<F>
|
2021-06-09 19:13:09 +00:00
|
|
|
where
|
2021-09-28 17:49:44 +00:00
|
|
|
F: FnMut(Coord) -> u8,
|
2021-06-09 19:13:09 +00:00
|
|
|
{
|
2022-07-11 15:26:03 +00:00
|
|
|
type Grid = (u32, u32);
|
2021-05-13 17:25:45 +00:00
|
|
|
|
|
|
|
type Opacity = u8;
|
|
|
|
|
|
|
|
fn size(&self, grid: &Self::Grid) -> Size {
|
2022-07-11 15:26:03 +00:00
|
|
|
Size::new(grid.0, grid.1)
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 19:13:09 +00:00
|
|
|
fn get_opacity(&self, _grid: &Self::Grid, coord: Coord) -> Self::Opacity {
|
2021-09-28 17:49:44 +00:00
|
|
|
(self.1.borrow_mut())(coord)
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 18:52:50 +00:00
|
|
|
fn update_viewshed(
|
2024-09-22 19:45:36 +00:00
|
|
|
mut commands: Commands,
|
2021-09-23 17:58:39 +00:00
|
|
|
config: Res<RapierConfiguration>,
|
2022-07-20 13:24:38 +00:00
|
|
|
visible: Query<(&Visible, &Collider, &GlobalTransform)>,
|
|
|
|
mut viewers: Query<(
|
|
|
|
Entity,
|
|
|
|
&mut Viewshed,
|
|
|
|
&mut VisibleEntities,
|
|
|
|
&GlobalTransform,
|
|
|
|
)>,
|
2022-05-06 16:07:59 +00:00
|
|
|
rapier_context: Res<RapierContext>,
|
2021-06-16 16:28:06 +00:00
|
|
|
) {
|
2021-09-23 17:58:39 +00:00
|
|
|
if !config.query_pipeline_active {
|
|
|
|
return;
|
|
|
|
}
|
2024-09-22 19:45:36 +00:00
|
|
|
let mut cache = default();
|
2023-03-28 17:13:23 +00:00
|
|
|
for (viewer_entity, mut viewshed, mut visible_entities, viewer_transform) in &mut viewers {
|
2022-07-25 18:52:50 +00:00
|
|
|
viewshed.update(
|
2024-09-22 19:45:36 +00:00
|
|
|
&mut commands,
|
2022-07-25 18:52:50 +00:00
|
|
|
&viewer_entity,
|
|
|
|
&mut visible_entities,
|
2024-08-30 20:50:10 +00:00
|
|
|
&viewer_transform.translation().truncate(),
|
2022-07-25 18:52:50 +00:00
|
|
|
&rapier_context,
|
|
|
|
&visible,
|
|
|
|
&mut cache,
|
|
|
|
);
|
2021-05-26 19:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 18:52:50 +00:00
|
|
|
fn remove_visible(
|
2024-09-22 19:45:36 +00:00
|
|
|
mut commands: Commands,
|
2023-03-28 16:57:37 +00:00
|
|
|
mut removed: RemovedComponents<Visible>,
|
2022-07-20 13:24:38 +00:00
|
|
|
mut viewers: Query<(
|
|
|
|
Entity,
|
|
|
|
&mut Viewshed,
|
|
|
|
&mut VisibleEntities,
|
|
|
|
&GlobalTransform,
|
|
|
|
)>,
|
2022-05-06 16:07:59 +00:00
|
|
|
rapier_context: Res<RapierContext>,
|
2022-07-20 13:24:38 +00:00
|
|
|
visible: Query<(&Visible, &Collider, &GlobalTransform)>,
|
2021-06-16 16:28:06 +00:00
|
|
|
) {
|
2023-03-28 16:57:37 +00:00
|
|
|
if !removed.is_empty() {
|
2024-09-22 19:45:36 +00:00
|
|
|
let mut cache = default();
|
2024-02-09 20:59:16 +00:00
|
|
|
for removed in removed.read() {
|
2023-03-28 17:13:23 +00:00
|
|
|
for (viewer_entity, mut viewshed, mut visible_entities, start) in &mut viewers {
|
2022-07-20 13:24:38 +00:00
|
|
|
if !visible_entities.contains(&removed) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-07-25 18:52:50 +00:00
|
|
|
viewshed.update(
|
2024-09-22 19:45:36 +00:00
|
|
|
&mut commands,
|
2022-07-25 18:52:50 +00:00
|
|
|
&viewer_entity,
|
|
|
|
&mut visible_entities,
|
2024-08-30 20:50:10 +00:00
|
|
|
&start.translation().truncate(),
|
2022-12-19 20:08:31 +00:00
|
|
|
&rapier_context,
|
2022-07-25 18:52:50 +00:00
|
|
|
&visible,
|
|
|
|
&mut cache,
|
|
|
|
);
|
2022-01-21 00:06:20 +00:00
|
|
|
}
|
2021-06-16 16:28:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 15:37:28 +00:00
|
|
|
fn update_revealed_tiles<D: 'static + Clone + Default + Send + Sync>(
|
|
|
|
mut map: Query<(&Map<D>, &mut RevealedTiles)>,
|
2022-05-18 15:28:42 +00:00
|
|
|
viewers: Query<&Viewshed, (With<Player>, Changed<Viewshed>)>,
|
2021-05-13 17:25:45 +00:00
|
|
|
) {
|
2023-03-28 17:13:23 +00:00
|
|
|
for viewshed in &viewers {
|
|
|
|
for (map, mut revealed_tiles) in &mut map {
|
2021-09-21 11:38:51 +00:00
|
|
|
for v in viewshed.visible_points.iter() {
|
2021-06-09 19:53:48 +00:00
|
|
|
let idx = v.to_index(map.width);
|
2021-09-27 18:45:52 +00:00
|
|
|
if idx >= revealed_tiles.len() {
|
2021-09-01 16:34:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-05-13 17:25:45 +00:00
|
|
|
revealed_tiles[idx] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn log_visible(
|
2024-09-22 19:45:36 +00:00
|
|
|
trigger: Trigger<VisibilityChanged>,
|
|
|
|
viewers: Query<(&GlobalTransform, Option<&Collider>), (With<Player>, With<Viewshed>)>,
|
2022-07-19 16:24:45 +00:00
|
|
|
visible: Query<(&Name, &GlobalTransform, Option<&Collider>), Without<DontLogWhenVisible>>,
|
2024-09-22 19:45:36 +00:00
|
|
|
mut log: Query<&mut Log>,
|
2021-05-13 17:25:45 +00:00
|
|
|
) {
|
2024-09-22 19:45:36 +00:00
|
|
|
if let VisibilityChanged::Gained(entity) = trigger.event() {
|
|
|
|
if *entity == trigger.entity() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if let Ok((viewer_transform, viewer_collider)) = viewers.get(trigger.entity()) {
|
|
|
|
if let Ok((name, viewed_transform, viewed_collider)) = visible.get(*entity) {
|
|
|
|
// println!("Gain {name}: {entity}");
|
|
|
|
let location = if let (Some(viewer_collider), Some(viewed_collider)) =
|
|
|
|
(viewer_collider, viewed_collider)
|
|
|
|
{
|
|
|
|
viewer_transform.collider_direction_and_distance(
|
|
|
|
viewer_collider,
|
|
|
|
viewed_transform,
|
|
|
|
viewed_collider,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
let yaw = viewer_transform.yaw();
|
|
|
|
viewer_transform.direction_and_distance(viewed_transform, Some(yaw))
|
|
|
|
};
|
|
|
|
if let Ok(mut log) = log.get_single_mut() {
|
|
|
|
log.push(format!("{}: {location}", name));
|
2021-09-21 11:38:51 +00:00
|
|
|
}
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 15:15:09 +00:00
|
|
|
pub struct VisibilityPlugin<MapData: 'static + Clone + Default + Send + Sync>(PhantomData<MapData>);
|
2021-05-13 17:25:45 +00:00
|
|
|
|
2022-12-20 15:15:09 +00:00
|
|
|
impl<MapData: 'static + Clone + Default + Send + Sync> Default for VisibilityPlugin<MapData> {
|
2022-03-15 15:37:28 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self(Default::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 15:15:09 +00:00
|
|
|
impl<MapData: 'static + Clone + Default + Send + Sync> Plugin for VisibilityPlugin<MapData> {
|
2022-01-10 19:50:52 +00:00
|
|
|
fn build(&self, app: &mut App) {
|
2024-09-22 19:45:36 +00:00
|
|
|
app.add_systems(
|
|
|
|
FixedPreUpdate,
|
|
|
|
(
|
|
|
|
add_visibility_indices::<MapData>,
|
|
|
|
update_viewshed,
|
|
|
|
update_revealed_tiles::<MapData>,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.add_systems(FixedPostUpdate, (viewshed_removed, remove_visible))
|
|
|
|
.observe(log_visible);
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
|
|
|
}
|