-use common::{Point, Dimension, Intersection};
+use common::{Point, Dimension, Intersection, supercover_line};
use core::render::Renderer;
use sprites::SpriteManager;
use std::rc::Rc;
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 mut grid = vec!(vec!(vec!(); size.height); size.width);
+ let mut grid = Grid {
+ cells: vec!(vec!(vec!(); size.height); size.width),
+ size,
+ cell_size: dimen!(cs.x, cs.y),
+ };
for wall in walls {
for edge in &wall.edges {
- // TODO: include cells that this edge overlaps
- for p in &[edge.p1, edge.p2] {
- let p = point!(p.x as usize, p.y as usize) / cs;
- grid[0.max(p.x as usize).min(size.width - 1)][0.max(p.y as usize).min(size.height - 1)].push(Rc::clone(edge));
+ for c in grid.grid_coordinates_on_line(edge.p1, edge.p2) {
+ grid.cells[c.x][c.y].push(Rc::clone(edge));
}
}
}
- Grid {
- size,
- cell_size: dimen!(cs.x, cs.y),
- cells: grid,
- }
+ grid
}
pub fn render(&mut self, renderer: &mut Renderer, _sprites: &SpriteManager) {
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,
}
pub fn intersect_walls(&self, p1: Point<f64>, p2: Point<f64>) -> IntersectResult {
- let c = point!(p2.x as isize / self.wall_grid.cell_size.width as isize, p2.y as isize / self.wall_grid.cell_size.height as isize);
- if let Some(walls) = self.wall_grid.at(c) {
- for w in walls {
- if let Intersection::Point(p) = Intersection::lines(p1, p2, w.p1, w.p2) {
- let wall = Wall {
- edge: Rc::clone(&w),
- };
- return IntersectResult::Intersection(wall, p)
+ 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: Rc::clone(&w),
+ };
+ return IntersectResult::Intersection(wall, p)
+ }
}
}
}
None
}
}
+
+ pub fn to_grid_coordinate<C>(&self, c: C) -> Option<Point<usize>>
+ where C: Into<(isize, isize)>
+ {
+ let c = c.into();
+ if c.0 >= 0 && c.0 < self.size.width as isize && c.1 >= 0 && c.1 < self.size.height as isize {
+ Some(point!(c.0 as usize, c.1 as usize))
+ } else {
+ None
+ }
+ }
+
+ /// 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)
+ .iter()
+ .map(|c| self.to_grid_coordinate(*c))
+ .flatten()
+ .collect()
+ }
}
////////// WALL REGION /////////////////////////////////////////////////////////