pub struct GameState {
world: World,
lvlgen: LevelGenerator,
+ debug_mode: bool,
}
impl GameState {
GameState {
world: World::new(lvlgen.generate()),
lvlgen,
+ ..Default::default()
}
}
}
}
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> {
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();
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);
}
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));
}
}
}
// 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()),