-use common::{Point, Dimension, Intersection, supercover_line};
+use common::{Point, Dimension, Intersection, Angle, ToAngle, supercover_line};
use core::render::Renderer;
use sprites::SpriteManager;
use std::rc::Rc;
pub struct Level {
pub gravity: Point<f64>,
pub grid: Grid<bool>,
- walls: Vec<Rc<WallRegion>>,
+ walls: Vec<WallRegion>,
wall_grid: Grid<Vec<Rc<WallEdge>>>,
}
impl Level {
- pub fn new(gravity: Point<f64>, grid: Grid<bool>, mut walls: Vec<Rc<WallRegion>>) -> Self {
+ pub fn new(gravity: Point<f64>, grid: Grid<bool>, mut walls: Vec<WallRegion>) -> Self {
let size = (2560, 1440); // TODO: get actual size from walls or something
let wall_grid = Level::build_wall_grid(&mut walls, &size.into());
- dbg!(&wall_grid.cell_size);
+ dbg!(&wall_grid.scale);
Level {
gravity,
grid,
fn build_wall_grid(walls: &mut Vec<WallRegion>, lvlsize: &Dimension<usize>) -> Grid<Vec<Rc<WallEdge>>> {
let size = dimen!(lvlsize.width / 20, lvlsize.height / 20); // TODO: make sure all walls fit within the grid bounds
let cs = point!(lvlsize.width / size.width, lvlsize.height / size.height);
- //let cs = point!(cell_size.width as f64, cell_size.height as f64);
+ //let cs = point!(scale.width as f64, scale.height as f64);
let mut grid = Grid {
cells: vec!(vec!(vec!(); size.height); size.width),
size,
- cell_size: dimen!(cs.x, cs.y),
+ scale: dimen!(cs.x as f64, cs.y as f64),
};
for wall in walls {
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.cell_size;
- 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.cell_size;
- 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 {
+ 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()),
<(i32, i32)>::from(e.p2.to_i32()),
pub fn intersect_walls(&self, p1: Point<f64>, p2: Point<f64>) -> IntersectResult {
for c in self.wall_grid.grid_coordinates_on_line(p1, p2) {
- if let walls = &self.wall_grid.cells[c.x][c.y] {
- for w in walls {
- if let Intersection::Point(p) = Intersection::lines(p1, p2, w.p1, w.p2) {
- let wall = Wall {
- region: &self.walls[w.region],
- edge: w,
- };
- return IntersectResult::Intersection(wall, p)
- }
+ for w in &self.wall_grid.cells[c.x][c.y] {
+ if let Intersection::Point(p) = Intersection::lines(p1, p2, w.p1, w.p2) {
+ let wall = Wall {
+ region: &self.walls[w.region],
+ edge: w,
+ };
+ return IntersectResult::Intersection(wall, p)
}
}
}
#[derive(Debug, Default)]
pub struct Grid<T> {
pub size: Dimension<usize>,
- pub cell_size: Dimension<usize>,
+ pub scale: Dimension<f64>,
pub cells: Vec<Vec<T>>,
}
/// Returns a list of grid coordinates that a line in world coordinates passes through.
pub fn grid_coordinates_on_line(&self, p1: Point<f64>, p2: Point<f64>) -> Vec<Point<usize>> {
- let scale = (self.cell_size.width as f64, self.cell_size.height as f64);
- supercover_line(p1 / scale, p2 / scale)
+ supercover_line(p1 / self.scale, p2 / self.scale)
.iter()
.map(|c| self.to_grid_coordinate(*c))
.flatten()
edge,
}
}
+
+ pub fn normal(&self) -> Angle {
+ (self.edge.p2 - self.edge.p1).to_angle() + std::f64::consts::FRAC_PI_2.radians()
+ }
}