Browse Source

Move map rendering into draw system as well

Getty Ritter 4 years ago
parent
commit
3c547835ea

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

@@ -67,7 +67,6 @@ impl GameState for State {
         *self.ecs.write_resource() = newrunstate;
         systems::CleanupDead.run_now(&self.ecs);
 
-        map::draw_map(&self.ecs, ctx);
         systems::DrawRenderables { ctx }.run_now(&self.ecs);
     }
 }

+ 4 - 40
chapter-07-damage/src/map.rs

@@ -1,12 +1,12 @@
 use crate::rect::Rect;
 use std::cmp::{max, min};
 
-use rltk::{Algorithm2D, BaseMap, Console, Point, RandomNumberGenerator, Rltk, RGB};
+use rltk::{Algorithm2D, BaseMap, Point, RandomNumberGenerator};
 use specs::prelude::*;
 
-const MAPWIDTH: usize = 80;
-const MAPHEIGHT: usize = 43;
-const MAPCOUNT: usize = MAPHEIGHT * MAPWIDTH;
+pub const MAPWIDTH: usize = 80;
+pub const MAPHEIGHT: usize = 43;
+pub const MAPCOUNT: usize = MAPHEIGHT * MAPWIDTH;
 
 #[derive(PartialEq, Copy, Clone)]
 pub enum TileType {
@@ -206,39 +206,3 @@ impl Algorithm2D for Map {
         }
     }
 }
-
-pub fn draw_map(ecs: &World, ctx: &mut Rltk) {
-    let map = ecs.fetch::<Map>();
-
-    let mut y = 0;
-    let mut x = 0;
-    for (idx, tile) in map.tiles.iter().enumerate() {
-        // Render a tile depending upon the tile type
-
-        if map.revealed_tiles[idx] {
-            let glyph;
-            let mut fg;
-            match tile {
-                TileType::Floor => {
-                    glyph = rltk::to_cp437('.');
-                    fg = RGB::from_f32(0.0, 0.5, 0.5);
-                }
-                TileType::Wall => {
-                    glyph = rltk::to_cp437('#');
-                    fg = RGB::from_f32(0., 1.0, 0.);
-                }
-            }
-            if !map.visible_tiles[idx] {
-                fg = fg.to_greyscale()
-            }
-            ctx.set(x, y, fg, RGB::from_f32(0., 0., 0.), glyph);
-        }
-
-        // Move the coordinates
-        x += 1;
-        if x > MAPWIDTH as i32 - 1 {
-            x = 0;
-            y += 1;
-        }
-    }
-}

+ 35 - 2
chapter-07-damage/src/systems/draw_system.rs

@@ -1,5 +1,5 @@
 use crate::components::{Position, Renderable};
-use crate::map::Map;
+use crate::map;
 use rltk::{Console, Rltk};
 use specs::{Join, ReadExpect, ReadStorage, System};
 
@@ -11,10 +11,43 @@ impl<'a, 'r> System<'a> for DrawRenderables<'r> {
     type SystemData = (
         ReadStorage<'a, Position>,
         ReadStorage<'a, Renderable>,
-        ReadExpect<'a, Map>,
+        ReadExpect<'a, map::Map>,
     );
 
     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() {
+            // 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;
+            }
+        }
+
         for (pos, render) in (&positions, &renderables).join() {
             let idx = map.xy_idx(pos.x, pos.y);
             if map.visible_tiles[idx] {