X-Git-Url: http://git.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fcore%2Flevel%2Fmod.rs;h=bdc6cb0e292bad415ad694d05fcc8eaf3c61391b;hb=b1075e661c2ce2f107350b8d4f5f9a92e047b93e;hp=2a9073918a1eea528d14222923e9ec2c27288aeb;hpb=60058b918569190f437fe996dfc79daf5a431b91;p=kaka%2Frust-sdl-test.git diff --git a/src/core/level/mod.rs b/src/core/level/mod.rs index 2a90739..bdc6cb0 100644 --- a/src/core/level/mod.rs +++ b/src/core/level/mod.rs @@ -1,5 +1,5 @@ -use common::{Point, Dimension, Intersection}; use core::render::Renderer; +use geometry::{Point, Dimension, Intersection, Angle, ToAngle, supercover_line}; use sprites::SpriteManager; use std::rc::Rc; use {point, dimen}; @@ -19,69 +19,84 @@ pub struct Level { } 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, - walls, + walls: walls.into_iter().map(|i| Rc::new(i)).collect(), wall_grid, } } /// Creates a grid of wall edges for fast lookup - fn build_wall_grid(walls: &mut Vec>, lvlsize: &Dimension) -> Grid>> { + 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 mut grid = vec!(vec!(vec!(); size.height); size.width); + //let cs = point!(scale.width as f64, scale.height as f64); + let mut grid = Grid { + cells: vec!(vec!(vec!(); size.height); size.width), + size, + scale: dimen!(cs.x as f64, cs.y as f64), + }; 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) { - // 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.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() { - 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 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)); } } } @@ -89,6 +104,25 @@ impl Level { // 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()), @@ -98,14 +132,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 { + for c in self.wall_grid.grid_coordinates_on_line(p1, p2) { + 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 { - edge: Rc::clone(&w), - }; - return IntersectResult::Intersection(wall, p) + if w.point_is_in_front(p1) { + let wall = Wall { + region: Rc::clone(&self.walls[w.region]), + edge: Rc::clone(w), + }; + return IntersectResult::Intersection(wall, p) + } } } } @@ -123,71 +159,124 @@ pub enum IntersectResult { #[derive(Debug, Default)] pub struct Grid { pub size: Dimension, - pub cell_size: Dimension, + pub scale: Dimension, pub cells: Vec>, } impl Grid { - pub fn at(&self, c: C) -> Option<&T> + // 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(&self.cells[c.0 as usize][c.1 as usize]) + 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> { + supercover_line(p1 / self.scale, p2 / self.scale) + .iter() + .map(|c| self.to_grid_coordinate(*c)) + .flatten() + .collect() + } } ////////// WALL REGION ///////////////////////////////////////////////////////// -#[derive(Debug)] +#[derive(Debug, Default)] pub struct WallRegion { edges: Vec>, } impl WallRegion { - pub fn new(points: Vec>) -> Rc { + pub fn new(points: Vec>) -> Self { + let index: RegionIndex = 0; // use as param let mut edges = Vec::with_capacity(points.len()); for i in 0..points.len() { let edge = Rc::new(WallEdge { - index: i, + region: index, + id: i, p1: points[i], p2: points[(i + 1) % points.len()], }); edges.push(edge); } - Rc::new(WallRegion { edges }) + WallRegion { edges } } - #[allow(dead_code)] - fn next(&self, index: usize) -> Rc { - let index = (index + 1) % self.edges.len(); - Rc::clone(&self.edges[index]) + fn next(&self, index: EdgeIndex) -> Rc { + let index = (index + 1) % self.edges.len(); + Rc::clone(&self.edges[index]) } - #[allow(dead_code)] - fn previous(&self, index: usize) -> Rc { - let index = (index + self.edges.len() + 1) % self.edges.len(); - Rc::clone(&self.edges[index]) + fn previous(&self, index: EdgeIndex) -> Rc { + let index = (index + self.edges.len() + 1) % self.edges.len(); + Rc::clone(&self.edges[index]) } } ////////// WALL EDGE /////////////////////////////////////////////////////////// +type RegionIndex = usize; +type EdgeIndex = usize; + #[derive(Debug, Default)] struct WallEdge { - index: usize, + region: RegionIndex, + id: EdgeIndex, pub p1: Point, pub p2: Point, } +impl WallEdge { + fn point_is_in_front(&self, p: Point) -> bool { + let cross = (self.p2 - self.p1).cross_product(p - self.p1); + cross > 0.0 + } +} + ////////// WALL //////////////////////////////////////////////////////////////// pub struct Wall { -// region: Rc, + region: Rc, edge: Rc, } + +impl Wall { + #[allow(dead_code)] + pub fn next(self) -> Wall { + Wall { + edge: self.region.next(self.edge.id), + region: self.region, + } + } + + #[allow(dead_code)] + pub fn previous(self) -> Wall { + Wall { + edge: self.region.previous(self.edge.id), + region: self.region, + } + } + + pub fn normal(&self) -> Angle { + (self.edge.p2 - self.edge.p1).to_angle() + std::f64::consts::FRAC_PI_2.radians() + } +}