visibility_system.rs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. extern crate specs;
  2. use specs::prelude::*;
  3. use super::{Viewshed, Position, Map, Player};
  4. extern crate rltk;
  5. use rltk::{field_of_view, Point};
  6. pub struct VisibilitySystem {}
  7. impl<'a> System<'a> for VisibilitySystem {
  8. type SystemData = ( WriteExpect<'a, Map>,
  9. Entities<'a>,
  10. WriteStorage<'a, Viewshed>,
  11. ReadStorage<'a, Position>,
  12. ReadStorage<'a, Player>);
  13. fn run(&mut self, data : Self::SystemData) {
  14. let (mut map, entities, mut viewshed, pos, player) = data;
  15. for (ent,viewshed,pos) in (&entities, &mut viewshed, &pos).join() {
  16. if viewshed.dirty {
  17. viewshed.dirty = false;
  18. viewshed.visible_tiles = field_of_view(Point::new(pos.x, pos.y), viewshed.range, &*map);
  19. // If this is the player, reveal what they can see
  20. let _p : Option<&Player> = player.get(ent);
  21. if let Some(_p) = _p {
  22. for t in map.visible_tiles.iter_mut() { *t = false };
  23. for vis in viewshed.visible_tiles.iter() {
  24. let idx = map.xy_idx(vis.x, vis.y);
  25. map.revealed_tiles[idx] = true;
  26. map.visible_tiles[idx] = true;
  27. }
  28. }
  29. }
  30. }
  31. }
  32. }