X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fcore%2Flevel%2Fmod.rs;h=488cae16885769960ae2996215a359eb137283b1;hb=8012f86b052f550fe6644ec33a2713f381fc4347;hp=2a9073918a1eea528d14222923e9ec2c27288aeb;hpb=40c949e573ea85720e1fa9972a1e16f5f1216799;p=kaka%2Frust-sdl-test.git diff --git a/src/core/level/mod.rs b/src/core/level/mod.rs index 2a90739..488cae1 100644 --- a/src/core/level/mod.rs +++ b/src/core/level/mod.rs @@ -1,4 +1,4 @@ -use common::{Point, Dimension, Intersection}; +use common::{Point, Dimension, Intersection, supercover_line}; use core::render::Renderer; use sprites::SpriteManager; use std::rc::Rc; @@ -36,23 +36,21 @@ impl Level { 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) { @@ -77,6 +75,8 @@ impl Level { 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, @@ -98,14 +98,16 @@ impl Level { } pub fn intersect_walls(&self, p1: Point, p2: Point) -> 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) + } } } } @@ -138,6 +140,27 @@ impl Grid { None } } + + pub fn to_grid_coordinate(&self, c: C) -> Option> + 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, p2: Point) -> Vec> { + 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 /////////////////////////////////////////////////////////