use std::collections::{HashMap, HashSet}; use bevy::prelude::*; use coord_2d::{Coord, Size}; use derive_more::{Deref, DerefMut}; use shadowcast::{vision_distance, Context, InputGrid}; use crate::{ bevy_rapier2d::prelude::*, core::{Angle, Coordinates, Player, PointLike}, log::Log, map::{ITileType, Map, MapConfig}, }; #[derive(Clone, Copy, Debug, Deref, DerefMut, Reflect)] #[reflect(Component)] pub struct BlocksVisibility(pub u8); impl Default for BlocksVisibility { fn default() -> Self { Self(u8::MAX) } } #[derive(Clone, Copy, Debug, Default, Reflect)] #[reflect(Component)] pub struct DontLogWhenVisible; #[derive(Clone, Debug, Default, Deref, DerefMut, Reflect)] #[reflect(Component)] pub struct RevealedTiles(pub Vec); #[derive(Clone, Debug, Eq, PartialEq)] pub struct Viewshed { pub visible: HashSet<(i32, i32)>, pub range: u32, } impl Default for Viewshed { fn default() -> Self { Self { range: 15, visible: HashSet::new(), } } } impl Viewshed { pub fn is_visible(&self, point: &dyn PointLike) -> bool { self.visible.contains(&point.into()) } } #[derive(Clone, Debug, Default, Deref, DerefMut, Reflect)] #[reflect(Component)] pub struct VisibleTiles(pub Vec); impl PointLike for Coord { fn x(&self) -> f32 { self.x as f32 } fn y(&self) -> f32 { self.y as f32 } } fn add_visibility_indices( mut commands: Commands, query: Query<(Entity, &Map), (Added, Without, Without)>, map_config: Res, ) { 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])); } } pub struct VisibilityGrid<'a, F>(pub &'a Map, pub F); impl<'a, F> InputGrid for VisibilityGrid<'a, F> where F: Fn(Coord) -> u8, { type Grid = VisibilityGrid<'a, F>; type Opacity = u8; fn size(&self, grid: &Self::Grid) -> Size { Size::new(grid.0.width as u32, grid.0.height as u32) } fn get_opacity(&self, _grid: &Self::Grid, coord: Coord) -> Self::Opacity { self.1(coord) } } fn update_viewshed( viewer_entity: &Entity, viewshed: &mut Viewshed, start: &dyn PointLike, query_pipeline: &QueryPipeline, collider_query: &QueryPipelineColliderComponentsQuery, map: &Map, blocks_visibility: &Query<&BlocksVisibility>, coordinates: &Query<&Coordinates>, ) { let mut context: Context = Context::default(); let vision_distance = vision_distance::Circle::new(viewshed.range); let coord = Coord::new(start.x_i32(), start.y_i32()); viewshed.visible.clear(); let shape = Cuboid::new(Vec2::new(0.49, 0.49).into()); let range = viewshed.range as f32; let collider_set = QueryPipelineColliderComponentsSet(&collider_query); let visibility_grid = VisibilityGrid(map, |coord: Coord| { if coord.distance(start) > range { return 255; } let shape_pos = (Vec2::new(coord.x as f32 + 0.5, coord.y as f32 + 0.5), 0.); let mut opacity = 0; query_pipeline.intersections_with_shape( &collider_set, &shape_pos.into(), &shape, InteractionGroups::all(), Some(&|v| v.entity() != *viewer_entity && blocks_visibility.get(v.entity()).is_ok()), |handle| { if let Ok(blocks_visibility) = blocks_visibility.get(handle.entity()) { if let Ok(coordinates) = coordinates.get(handle.entity()) { if coordinates.i32() == (coord.x, coord.y) { opacity = **blocks_visibility; false } else { true } } else { opacity = **blocks_visibility; false } } else { true } }, ); opacity }); context.for_each_visible( coord, &visibility_grid, &visibility_grid, vision_distance, 255, |coord, _directions, _visibility| { viewshed.visible.insert((coord.x, coord.y)); }, ); } fn update_viewshed_for_coordinates( visible: Query<&Coordinates, (Changed, With)>, mut viewers: Query<(Entity, &mut Viewshed, &Coordinates)>, map: Query<&Map>, query_pipeline: Res, collider_query: QueryPipelineColliderComponentsQuery, blocks_visibility: Query<&BlocksVisibility>, coordinates_query: Query<&Coordinates>, ) { for coordinates in visible.iter() { for (viewer_entity, mut viewshed, start) in viewers.iter_mut() { if coordinates.distance(start) > viewshed.range as f32 { continue; } if let Ok(map) = map.single() { update_viewshed( &viewer_entity, &mut viewshed, start, &*query_pipeline, &collider_query, map, &blocks_visibility, &coordinates_query, ); } } } } fn update_viewshed_for_start( mut viewers: Query<(Entity, &mut Viewshed, &Coordinates), Changed>, map: Query<&Map>, query_pipeline: Res, collider_query: QueryPipelineColliderComponentsQuery, blocks_visibility: Query<&BlocksVisibility>, coordinates_query: Query<&Coordinates>, ) { for (viewer_entity, mut viewshed, start) in viewers.iter_mut() { if let Ok(map) = map.single() { update_viewshed( &viewer_entity, &mut viewshed, start, &*query_pipeline, &collider_query, map, &blocks_visibility, &coordinates_query, ); } } } fn remove_blocks_visibility( removed: RemovedComponents, mut viewers: Query<(Entity, &mut Viewshed, &Coordinates)>, map: Query<&Map>, query_pipeline: Res, collider_query: QueryPipelineColliderComponentsQuery, blocks_visibility: Query<&BlocksVisibility>, coordinates_query: Query<&Coordinates>, ) { for _ in removed.iter() { for (viewer_entity, mut viewshed, start) in viewers.iter_mut() { if let Ok(map) = map.single() { update_viewshed( &viewer_entity, &mut viewshed, start, &*query_pipeline, &collider_query, map, &blocks_visibility, &coordinates_query, ); } } } } fn update_visible_and_revealed_tiles( mut map: Query<(&Map, &mut RevealedTiles, &mut VisibleTiles)>, viewers: Query<&Viewshed, With>, ) { 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); revealed_tiles[idx] = true; visible_tiles[idx] = true; } } } } fn log_visible( time: Res