2021-12-24 10:38:44 -05:00
|
|
|
use ::rltk::{field_of_view, Point, RandomNumberGenerator};
|
|
|
|
use ::specs::prelude::*;
|
2021-10-25 15:26:39 -04:00
|
|
|
|
2021-12-17 14:07:14 -05:00
|
|
|
use crate::components::{BlocksVisibility, Hidden, Name};
|
2022-02-01 10:39:46 -05:00
|
|
|
use crate::{colors, gamelog, spatial, Map, Player, Position, Viewshed};
|
2021-12-10 20:16:48 -05:00
|
|
|
|
2021-10-25 15:26:39 -04:00
|
|
|
pub struct VisibilitySystem {}
|
|
|
|
|
|
|
|
impl<'a> System<'a> for VisibilitySystem {
|
2021-12-01 10:14:30 -05:00
|
|
|
#[allow(clippy::type_complexity)]
|
2021-10-25 15:26:39 -04:00
|
|
|
type SystemData = (
|
|
|
|
WriteExpect<'a, Map>,
|
|
|
|
Entities<'a>,
|
|
|
|
WriteStorage<'a, Viewshed>,
|
2021-12-01 10:14:30 -05:00
|
|
|
ReadStorage<'a, Position>,
|
2021-10-25 15:26:39 -04:00
|
|
|
ReadStorage<'a, Player>,
|
2021-12-01 10:14:30 -05:00
|
|
|
WriteStorage<'a, Hidden>,
|
2021-12-17 14:07:14 -05:00
|
|
|
WriteExpect<'a, RandomNumberGenerator>,
|
2021-12-01 10:14:30 -05:00
|
|
|
ReadStorage<'a, Name>,
|
2021-12-17 14:07:14 -05:00
|
|
|
ReadStorage<'a, BlocksVisibility>,
|
2021-10-25 15:26:39 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
fn run(&mut self, data: Self::SystemData) {
|
2021-12-17 14:07:14 -05:00
|
|
|
let (
|
|
|
|
mut map,
|
|
|
|
entities,
|
|
|
|
mut viewshed,
|
|
|
|
pos,
|
|
|
|
player,
|
|
|
|
mut hidden,
|
|
|
|
mut rng,
|
|
|
|
names,
|
|
|
|
blocks_visibility,
|
|
|
|
) = data;
|
|
|
|
|
|
|
|
map.view_blocked.clear();
|
|
|
|
for (block_pos, _block) in (&pos, &blocks_visibility).join() {
|
|
|
|
let idx = map.xy_idx(block_pos.x, block_pos.y);
|
|
|
|
map.view_blocked.insert(idx);
|
|
|
|
}
|
2021-10-25 15:26:39 -04:00
|
|
|
|
|
|
|
for (ent, viewshed, pos) in (&entities, &mut viewshed, &pos).join() {
|
2021-10-26 14:23:08 -04:00
|
|
|
if viewshed.dirty {
|
|
|
|
viewshed.dirty = false;
|
|
|
|
viewshed.visible_tiles.clear();
|
|
|
|
viewshed.visible_tiles =
|
|
|
|
field_of_view(Point::new(pos.x, pos.y), viewshed.range, &*map);
|
|
|
|
viewshed
|
|
|
|
.visible_tiles
|
|
|
|
.retain(|p| p.x >= 0 && p.x < map.width && p.y >= 0 && p.y < map.height);
|
2021-10-25 15:26:39 -04:00
|
|
|
|
2021-10-26 14:23:08 -04:00
|
|
|
// if this is the player, reveal what they can see
|
2022-01-12 10:45:13 -05:00
|
|
|
if player.get(ent).is_some() {
|
2021-10-26 14:23:08 -04:00
|
|
|
for t in map.visible_tiles.iter_mut() {
|
|
|
|
*t = false
|
|
|
|
}
|
2021-12-01 10:14:30 -05:00
|
|
|
|
2021-10-26 14:23:08 -04:00
|
|
|
for vis in viewshed.visible_tiles.iter() {
|
|
|
|
let idx = map.xy_idx(vis.x, vis.y);
|
|
|
|
map.revealed_tiles[idx] = true;
|
|
|
|
map.visible_tiles[idx] = true;
|
2021-12-01 10:14:30 -05:00
|
|
|
|
|
|
|
// Chance to reveal hidden things
|
2022-01-12 10:45:13 -05:00
|
|
|
spatial::for_each_tile_content(idx, |e| {
|
|
|
|
let maybe_hidden = hidden.get(e);
|
|
|
|
if let Some(_maybe_hidden) = maybe_hidden {
|
2021-12-01 10:14:30 -05:00
|
|
|
if rng.roll_dice(1, 24) == 1 {
|
2022-01-12 10:45:13 -05:00
|
|
|
let name = names.get(e);
|
|
|
|
if let Some(name) = name {
|
2022-02-01 10:39:46 -05:00
|
|
|
gamelog::line("You spotted:")
|
|
|
|
.append_color(colors::RED, &name.name)
|
|
|
|
.log();
|
2021-12-01 10:14:30 -05:00
|
|
|
}
|
2022-01-12 10:45:13 -05:00
|
|
|
hidden.remove(e);
|
2021-12-01 10:14:30 -05:00
|
|
|
}
|
|
|
|
}
|
2022-01-12 10:45:13 -05:00
|
|
|
});
|
2021-10-26 14:23:08 -04:00
|
|
|
}
|
2021-10-25 15:26:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|