Browse Source

implement having-seen-ness

Getty Ritter 1 year ago
parent
commit
1d448034df
3 changed files with 29 additions and 13 deletions
  1. 1 1
      ch5/src/main.rs
  2. 20 11
      ch5/src/map.rs
  3. 8 1
      ch5/src/systems.rs

+ 1 - 1
ch5/src/main.rs

@@ -38,7 +38,7 @@ fn main() -> Result<(), GameError> {
         .first()
         .map(|r| r.center())
         .unwrap_or_else(|| [40, 25].into());
-    let vs = carpet::Viewshed::create(&map.tiles, map::TileType::blocks_view);
+    let vs = carpet::Viewshed::create(&map.tiles, map::Cell::blocks_view);
     game.insert(map::Viewshed { vs, dirty: true });
     game.insert(map);
 

+ 20 - 11
ch5/src/map.rs

@@ -3,29 +3,35 @@ use rand::Rng;
 use specs::prelude::*;
 
 #[derive(PartialEq, Clone, Copy, Debug)]
-pub enum TileType {
-    Wall,
-    Floor,
+pub struct Cell {
+    pub tile: TileType,
+    pub seen: bool,
 }
 
-impl TileType {
+impl Cell {
     pub fn glyph(&self) -> carpet::CP437 {
-        match self {
+        match self.tile {
             TileType::Wall => carpet::CP437::from_char('#'),
             TileType::Floor => carpet::CP437::from_u8(0),
         }
     }
 
     pub fn blocks_view(&self) -> bool {
-        match self {
+        match self.tile {
             TileType::Wall => true,
             TileType::Floor => false,
         }
     }
 }
 
+#[derive(PartialEq, Clone, Copy, Debug)]
+pub enum TileType {
+    Wall,
+    Floor,
+}
+
 pub struct Map {
-    pub tiles: carpet::Board<TileType>,
+    pub tiles: carpet::Board<Cell>,
     pub rooms: Vec<carpet::Rect>,
 }
 
@@ -33,7 +39,7 @@ impl Map {
     pub fn new() -> Map {
         let mut rng = rand::thread_rng();
         let mut map = Map {
-            tiles: carpet::Board::new_with_default(80, 50, TileType::Wall),
+            tiles: carpet::Board::new_with_default(80, 50, Cell { tile: TileType::Wall, seen: false }),
             rooms: Vec::new(),
         };
 
@@ -75,17 +81,20 @@ impl Map {
             .window_iter_mut(rect)
             .unwrap_or_else(|| panic!("Rect {:?} of map bounds", rect));
         for (_, _, t) in iter {
-            *t = TileType::Floor;
+            t.tile = TileType::Floor;
         }
     }
 
     pub fn passable(&self, (x, y): (usize, usize)) -> bool {
-        Some(&TileType::Floor) == self.tiles.get(x, y)
+        if let Some(cell) = self.tiles.get(x, y) {
+            return cell.tile == TileType::Floor;
+        }
+        false
     }
 }
 
 #[derive(Component)]
 pub struct Viewshed {
-    pub vs: carpet::Viewshed<TileType>,
+    pub vs: carpet::Viewshed<Cell>,
     pub dirty: bool,
 }

+ 8 - 1
ch5/src/systems.rs

@@ -13,6 +13,8 @@ system_impl! {
         for (x, y, t) in map.tiles.iter() {
             if vs.vs.visibility([x, y]) {
                 game_board.set([x, y], t.glyph());
+            } else if t.seen {
+                game_board.set_with_color([x, y], t.glyph(), (0.5, 0.5, 0.5));
             }
         }
         for (p, r) in (&pos, &renderable).join() {
@@ -42,13 +44,18 @@ system! {
 
 system! {
     Visibility(
-        resource map: Map,
+        resource mut map: Map,
         resource mut viewshed: Viewshed,
         pos: carpet::Coord,
         _player: Player,
     ) {
         if viewshed.dirty {
             viewshed.vs.calculate_from(&map.tiles, *pos);
+            for (x, y, vis) in viewshed.vs.vis.iter() {
+                if *vis {
+                    map.tiles[(x, y)].seen = true;
+                }
+            }
             viewshed.dirty = false;
         }
     }