Browse Source

Clean up map drawing code

Getty Ritter 4 years ago
parent
commit
de4964506c

+ 10 - 0
Cargo.lock

@@ -223,6 +223,7 @@ dependencies = [
 name = "chapter-07-damage"
 version = "0.1.0"
 dependencies = [
+ "itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "rltk 0.4.1 (git+https://github.com/thebracket/rltk_rs)",
  "specs 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "specs-derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -926,6 +927,14 @@ dependencies = [
  "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "itertools"
+version = "0.8.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "itoa"
 version = "0.4.4"
@@ -2134,6 +2143,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 "checksum hibitset 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "47e7292fd9f7fe89fa35c98048f2d0a69b79ed243604234d18f6f8a1aa6f408d"
 "checksum image 0.22.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7b4be8aaefbe7545dc42ae925afb55a0098f226a3fe5ef721872806f44f57826"
 "checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08"
+"checksum itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484"
 "checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f"
 "checksum jpeg-decoder 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "c1aae18ffeeae409c6622c3b6a7ee49792a7e5a062eea1b135fbb74e301792ba"
 "checksum js-sys 0.3.28 (registry+https://github.com/rust-lang/crates.io-index)" = "2cc9a97d7cec30128fd8b28a7c1f9df1c001ceb9b441e2b755e24130a6b43c79"

+ 1 - 0
chapter-07-damage/Cargo.toml

@@ -10,6 +10,7 @@ edition = "2018"
 rltk = { git = "https://github.com/thebracket/rltk_rs" }
 specs = "0.15.0"
 specs-derive = "0.4.0"
+itertools = "*"
 
 [target.'cfg(any(target_arch = "wasm32"))'.dependencies]
 web-sys = { version = "0.3", features=["console"] }

+ 2 - 0
chapter-07-damage/src/main.rs

@@ -1,4 +1,6 @@
 #[macro_use]
+extern crate itertools;
+#[macro_use]
 extern crate specs_derive;
 
 mod components;

+ 16 - 0
chapter-07-damage/src/map.rs

@@ -14,6 +14,22 @@ pub enum TileType {
     Floor,
 }
 
+impl TileType {
+    pub fn glyph(&self) -> u8 {
+        match self {
+            TileType::Wall => rltk::to_cp437('#'),
+            TileType::Floor => rltk::to_cp437('.'),
+        }
+    }
+
+    pub fn color(&self) -> rltk::RGB {
+        match self {
+            TileType::Wall => rltk::RGB::from_f32(0., 1.0, 0.),
+            TileType::Floor => rltk::RGB::from_f32(0.0, 0.5, 0.5),
+        }
+    }
+}
+
 #[derive(Default)]
 pub struct Map {
     pub tiles: Vec<TileType>,

+ 14 - 27
chapter-07-damage/src/systems/draw_system.rs

@@ -15,36 +15,23 @@ impl<'a, 'r> System<'a> for DrawRenderables<'r> {
     );
 
     fn run(&mut self, (positions, renderables, map): Self::SystemData) {
-        let mut y = 0;
-        let mut x = 0;
-        for (idx, tile) in map.tiles.iter().enumerate() {
+        let points = iproduct!(0..map::MAPHEIGHT, 0..map::MAPWIDTH);
+        for (idx, (tile, (y, x))) in map.tiles.iter().zip(points).enumerate() {
             // Render a tile depending upon the tile type
 
             if map.revealed_tiles[idx] {
-                let glyph;
-                let mut fg;
-                match tile {
-                    map::TileType::Floor => {
-                        glyph = rltk::to_cp437('.');
-                        fg = rltk::RGB::from_f32(0.0, 0.5, 0.5);
-                    }
-                    map::TileType::Wall => {
-                        glyph = rltk::to_cp437('#');
-                        fg = rltk::RGB::from_f32(0., 1.0, 0.);
-                    }
-                }
-                if !map.visible_tiles[idx] {
-                    fg = fg.to_greyscale()
-                }
-                self.ctx
-                    .set(x, y, fg, rltk::RGB::from_f32(0., 0., 0.), glyph);
-            }
-
-            // Move the coordinates
-            x += 1;
-            if x > map::MAPWIDTH as i32 - 1 {
-                x = 0;
-                y += 1;
+                let fg = if map.visible_tiles[idx] {
+                    tile.color()
+                } else {
+                    tile.color().to_greyscale()
+                };
+                self.ctx.set(
+                    x as i32,
+                    y as i32,
+                    fg,
+                    rltk::RGB::from_f32(0., 0., 0.),
+                    tile.glyph(),
+                );
             }
         }