X-Git-Url: http://git.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fcore%2Flevel%2Fmod.rs;h=cccd2538a4a1a69eafd3ddeed08ea2b3ed00ba60;hb=8065e2645a936275c7d69c6068d80ece598d6e57;hp=ac13e4ebb090468699cb636a457a79df75f8fe00;hpb=d01df1fc6e3bfafd385b2533e6155ddd8714fcfb;p=kaka%2Frust-sdl-test.git diff --git a/src/core/level/mod.rs b/src/core/level/mod.rs index ac13e4e..cccd253 100644 --- a/src/core/level/mod.rs +++ b/src/core/level/mod.rs @@ -1,4 +1,4 @@ -use common::{Point, Dimension, Intersection, supercover_line}; +use common::{Point, Dimension, Intersection, Radians, supercover_line}; use core::render::Renderer; use sprites::SpriteManager; use std::rc::Rc; @@ -14,15 +14,15 @@ pub use self::lvlgen::LevelGenerator; pub struct Level { pub gravity: Point, pub grid: Grid, - walls: Vec>, + walls: Vec, wall_grid: Grid>>, } impl Level { - pub fn new(gravity: Point, grid: Grid, mut walls: Vec>) -> Self { + pub fn new(gravity: Point, grid: Grid, mut walls: Vec) -> 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, @@ -35,11 +35,11 @@ impl Level { fn build_wall_grid(walls: &mut Vec, lvlsize: &Dimension) -> Grid>> { 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 { @@ -56,7 +56,7 @@ impl Level { 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; + 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] { @@ -71,7 +71,7 @@ impl Level { // wall grid renderer.canvas().set_draw_color((0, 32, 0)); - let size = &self.wall_grid.cell_size; + 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() { @@ -89,6 +89,15 @@ impl Level { // walls for wall in &self.walls { for e in &wall.edges { + let c = (e.p1 + e.p2) / 2.0; + let mut rad = (e.p2 - e.p1).to_radians(); + rad.0 += std::f64::consts::FRAC_PI_2; + + renderer.draw_line( + <(i32, i32)>::from(c.to_i32()), + <(i32, i32)>::from((c + Point::from(rad) * 10.0).to_i32()), + (255, 128, 0)); + renderer.draw_line( <(i32, i32)>::from(e.p1.to_i32()), <(i32, i32)>::from(e.p2.to_i32()), @@ -99,15 +108,13 @@ 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: 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) } } } @@ -125,7 +132,7 @@ pub enum IntersectResult<'a> { #[derive(Debug, Default)] pub struct Grid { pub size: Dimension, - pub cell_size: Dimension, + pub scale: Dimension, pub cells: Vec>, } @@ -154,8 +161,7 @@ impl Grid { /// 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) + supercover_line(p1 / self.scale, p2 / self.scale) .iter() .map(|c| self.to_grid_coordinate(*c)) .flatten() @@ -241,4 +247,10 @@ impl<'a> Wall<'a> { edge, } } + + pub fn normal(&self) -> Radians { + let mut rad = (self.edge.p2 - self.edge.p1).to_radians(); + rad.0 += std::f64::consts::FRAC_PI_2; + rad + } }