X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fcore%2Flevel%2Fmod.rs;h=488cae16885769960ae2996215a359eb137283b1;hb=5d42eba1124851e61be30fde5e26dab1a3e086a5;hp=3945776f998f6df729f3ea16e2f8c61671fa350a;hpb=1f42d724d84ed1c014ff40ccc91058472391be0c;p=kaka%2Frust-sdl-test.git diff --git a/src/core/level/mod.rs b/src/core/level/mod.rs index 3945776..488cae1 100644 --- a/src/core/level/mod.rs +++ b/src/core/level/mod.rs @@ -1,4 +1,4 @@ -use common::{Point, Dimension}; +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, @@ -96,6 +96,28 @@ impl Level { } } } + + pub fn intersect_walls(&self, p1: Point, p2: Point) -> 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: Rc::clone(&w), + }; + return IntersectResult::Intersection(wall, p) + } + } + } + } + IntersectResult::None + } +} + +pub enum IntersectResult { + Intersection(Wall, Point), + None } ////////// GRID //////////////////////////////////////////////////////////////// @@ -107,6 +129,40 @@ pub struct Grid { pub cells: Vec>, } +impl Grid { + pub fn at(&self, c: C) -> Option<&T> + 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(&self.cells[c.0 as usize][c.1 as usize]) + } else { + 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 ///////////////////////////////////////////////////////// #[derive(Debug)] @@ -116,7 +172,7 @@ pub struct WallRegion { impl WallRegion { pub fn new(points: Vec>) -> Rc { - let mut edges = vec!(); + let mut edges = Vec::with_capacity(points.len()); for i in 0..points.len() { let edge = Rc::new(WallEdge { @@ -151,3 +207,10 @@ struct WallEdge { pub p1: Point, pub p2: Point, } + +////////// WALL //////////////////////////////////////////////////////////////// + +pub struct Wall { +// region: Rc, + edge: Rc, +}