Toggle debug mode with keypad enter
authorTomas Wenström <tomas.wenstrom@gmail.com>
Wed, 10 Feb 2021 18:50:42 +0000 (19:50 +0100)
committerTomas Wenström <tomas.wenstrom@gmail.com>
Wed, 10 Feb 2021 18:50:42 +0000 (19:50 +0100)
src/core/game.rs
src/core/level/mod.rs

index 2fa7ddd..9d51e18 100644 (file)
@@ -22,6 +22,7 @@ use time::Duration;
 pub struct GameState {
     world: World,
     lvlgen: LevelGenerator,
+    debug_mode: bool,
 }
 
 impl GameState {
@@ -30,6 +31,7 @@ impl GameState {
        GameState {
            world: World::new(lvlgen.generate()),
            lvlgen,
+           ..Default::default()
        }
     }
 }
@@ -49,7 +51,7 @@ impl AppState for GameState {
     }
 
     fn render(&mut self, renderer: &mut Renderer, sprites: &SpriteManager) {
-       self.world.render(renderer, sprites);
+       self.world.render(renderer, sprites, self.debug_mode);
     }
 
     fn handle_event(&mut self, event: Event) -> Option<StateChange> {
@@ -60,6 +62,9 @@ impl AppState for GameState {
            Event::KeyDown { keycode: Some(Keycode::Return), .. } => {
                return Some(StateChange::Push(Box::new(TestState::new())))
             }
+           Event::KeyDown { keycode: Some(Keycode::KpEnter), .. } => {
+               self.debug_mode = !self.debug_mode;
+           }
            Event::KeyDown { keycode: Some(Keycode::Space), .. } => {
                self.lvlgen.seed = std::time::UNIX_EPOCH.elapsed().unwrap().as_secs() as u32;
                self.world.level = self.lvlgen.generate();
@@ -122,8 +127,8 @@ impl World {
        println!("\x1b[Kobject count: {}\x1b[1A", self.objects.len()); // clear line, print, move cursor up
     }
 
-    pub fn render(&mut self, renderer: &mut Renderer, sprites: &SpriteManager) {
-       self.level.render(renderer, sprites);
+    pub fn render(&mut self, renderer: &mut Renderer, sprites: &SpriteManager, debug_mode: bool) {
+       self.level.render(renderer, sprites, debug_mode);
        for o in &mut self.objects {
            o.render(renderer, sprites);
        }
index 00f3009..578a94e 100644 (file)
@@ -53,35 +53,50 @@ impl Level {
        grid
     }
 
-    pub fn render(&mut self, renderer: &mut Renderer, _sprites: &SpriteManager) {
-       // original grid
-       renderer.canvas().set_draw_color((64, 64, 64));
-       let size = &self.grid.scale;
-       for x in 0..self.grid.size.width {
-           for y in 0..self.grid.size.height {
-               if self.grid.cells[x][y] {
-                   renderer.canvas().fill_rect(sdl2::rect::Rect::new(
-                       x as i32 * size.width as i32,
-                       y as i32 * size.height as i32,
-                       size.width as u32,
-                       size.height as u32)).unwrap();
+    pub fn render(&mut self, renderer: &mut Renderer, _sprites: &SpriteManager, debug_mode: bool) {
+       if debug_mode {
+           // original grid
+           renderer.canvas().set_draw_color((64, 64, 64));
+           let size = &self.grid.scale;
+           for x in 0..self.grid.size.width {
+               for y in 0..self.grid.size.height {
+                   if self.grid.cells[x][y] {
+                       renderer.canvas().fill_rect(sdl2::rect::Rect::new(
+                           x as i32 * size.width as i32,
+                           y as i32 * size.height as i32,
+                           size.width as u32,
+                           size.height as u32)).unwrap();
+                   }
+               }
+           }
+
+           // wall grid
+           renderer.canvas().set_draw_color((0, 32, 0));
+           let size = &self.wall_grid.scale;
+           for x in 0..self.wall_grid.size.width {
+               for y in 0..self.wall_grid.size.height {
+                   if !self.wall_grid.cells[x][y].is_empty() {
+                       let num = self.wall_grid.cells[x][y].len();
+                       renderer.canvas().set_draw_color((0, 32*num as u8, 0));
+                       renderer.canvas().fill_rect(sdl2::rect::Rect::new(
+                           x as i32 * size.width as i32,
+                           y as i32 * size.height as i32,
+                           size.width as u32,
+                           size.height as u32)).unwrap();
+                   }
                }
            }
-       }
 
-       // wall grid
-       renderer.canvas().set_draw_color((0, 32, 0));
-       let size = &self.wall_grid.scale;
-       for x in 0..self.wall_grid.size.width {
-           for y in 0..self.wall_grid.size.height {
-               if !self.wall_grid.cells[x][y].is_empty() {
-                   let num = self.wall_grid.cells[x][y].len();
-                   renderer.canvas().set_draw_color((0, 32*num as u8, 0));
-                   renderer.canvas().fill_rect(sdl2::rect::Rect::new(
-                       x as i32 * size.width as i32,
-                       y as i32 * size.height as i32,
-                       size.width as u32,
-                       size.height as u32)).unwrap();
+           // wall normals
+           for wall in &self.walls {
+               for e in &wall.edges {
+                   let c = (e.p1 + e.p2) / 2.0;
+                   let a = (e.p2 - e.p1).to_angle() + std::f64::consts::FRAC_PI_2.radians();
+
+                   renderer.draw_line(
+                       <(i32, i32)>::from(c.to_i32()),
+                       <(i32, i32)>::from((c + Point::from(a) * 10.0).to_i32()),
+                       (0, 128, 255));
                }
            }
        }
@@ -89,13 +104,24 @@ impl Level {
        // walls
        for wall in &self.walls {
            for e in &wall.edges {
-               let c = (e.p1 + e.p2) / 2.0;
-               let a = (e.p2 - e.p1).to_angle() + std::f64::consts::FRAC_PI_2.radians();
-
-               renderer.draw_line(
-                   <(i32, i32)>::from(c.to_i32()),
-                   <(i32, i32)>::from((c + Point::from(a) * 10.0).to_i32()),
-                   (255, 128, 0));
+               if !debug_mode {
+                   let c = (e.p1 + e.p2) / 2.0;
+                   let a = (e.p2 - e.p1).to_angle() - std::f64::consts::FRAC_PI_2.radians();
+
+                   renderer.draw_line(
+                       <(i32, i32)>::from(c.to_i32()),
+                       <(i32, i32)>::from((c + Point::from(a) * 10.0).to_i32()),
+                       (255, 128, 0));
+
+                   renderer.draw_line(
+                       <(i32, i32)>::from(e.p1.to_i32()),
+                       <(i32, i32)>::from((c + Point::from(a) * 20.0).to_i32()),
+                       (96, 48, 0));
+                   renderer.draw_line(
+                       <(i32, i32)>::from(e.p2.to_i32()),
+                       <(i32, i32)>::from((c + Point::from(a) * 20.0).to_i32()),
+                       (96, 48, 0));
+               }
 
                renderer.draw_line(
                    <(i32, i32)>::from(e.p1.to_i32()),