| 1 | use core::render::Renderer; |
| 2 | use geometry::{Point, Dimension, Intersection, Angle, ToAngle, supercover_line}; |
| 3 | use sprites::SpriteManager; |
| 4 | use std::rc::Rc; |
| 5 | use {point, dimen}; |
| 6 | |
| 7 | mod lvlgen; |
| 8 | |
| 9 | pub use self::lvlgen::LevelGenerator; |
| 10 | |
| 11 | ////////// LEVEL /////////////////////////////////////////////////////////////// |
| 12 | |
| 13 | #[derive(Default)] |
| 14 | pub struct Level { |
| 15 | pub gravity: Point<f64>, |
| 16 | pub grid: Grid<bool>, |
| 17 | walls: Vec<Rc<WallRegion>>, |
| 18 | wall_grid: Grid<Vec<Rc<WallEdge>>>, |
| 19 | } |
| 20 | |
| 21 | impl Level { |
| 22 | pub fn new(gravity: Point<f64>, grid: Grid<bool>, mut walls: Vec<WallRegion>) -> Self { |
| 23 | let size = (2560, 1440); // TODO: get actual size from walls or something |
| 24 | let wall_grid = Level::build_wall_grid(&mut walls, &size.into()); |
| 25 | dbg!(&wall_grid.scale); |
| 26 | Level { |
| 27 | gravity, |
| 28 | grid, |
| 29 | walls: walls.into_iter().map(|i| Rc::new(i)).collect(), |
| 30 | wall_grid, |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /// Creates a grid of wall edges for fast lookup |
| 35 | fn build_wall_grid(walls: &mut Vec<WallRegion>, lvlsize: &Dimension<usize>) -> Grid<Vec<Rc<WallEdge>>> { |
| 36 | let size = dimen!(lvlsize.width / 20, lvlsize.height / 20); // TODO: make sure all walls fit within the grid bounds |
| 37 | let cs = point!(lvlsize.width / size.width, lvlsize.height / size.height); |
| 38 | //let cs = point!(scale.width as f64, scale.height as f64); |
| 39 | let mut grid = Grid { |
| 40 | cells: vec!(vec!(vec!(); size.height); size.width), |
| 41 | size, |
| 42 | scale: dimen!(cs.x as f64, cs.y as f64), |
| 43 | }; |
| 44 | |
| 45 | for wall in walls { |
| 46 | for edge in &wall.edges { |
| 47 | for c in grid.grid_coordinates_on_line(edge.p1, edge.p2) { |
| 48 | grid.cells[c.x][c.y].push(Rc::clone(edge)); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | grid |
| 54 | } |
| 55 | |
| 56 | pub fn render(&mut self, renderer: &mut Renderer, _sprites: &SpriteManager, debug_mode: bool) { |
| 57 | if debug_mode { |
| 58 | // original grid |
| 59 | renderer.canvas().set_draw_color((64, 64, 64)); |
| 60 | let size = &self.grid.scale; |
| 61 | for x in 0..self.grid.size.width { |
| 62 | for y in 0..self.grid.size.height { |
| 63 | if self.grid.cells[x][y] { |
| 64 | renderer.canvas().fill_rect(sdl2::rect::Rect::new( |
| 65 | x as i32 * size.width as i32, |
| 66 | y as i32 * size.height as i32, |
| 67 | size.width as u32, |
| 68 | size.height as u32)).unwrap(); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // wall grid |
| 74 | renderer.canvas().set_draw_color((0, 32, 0)); |
| 75 | let size = &self.wall_grid.scale; |
| 76 | for x in 0..self.wall_grid.size.width { |
| 77 | for y in 0..self.wall_grid.size.height { |
| 78 | if !self.wall_grid.cells[x][y].is_empty() { |
| 79 | let num = self.wall_grid.cells[x][y].len(); |
| 80 | renderer.canvas().set_draw_color((0, 32*num as u8, 0)); |
| 81 | renderer.canvas().fill_rect(sdl2::rect::Rect::new( |
| 82 | x as i32 * size.width as i32, |
| 83 | y as i32 * size.height as i32, |
| 84 | size.width as u32, |
| 85 | size.height as u32)).unwrap(); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // wall normals |
| 91 | for wall in &self.walls { |
| 92 | for e in &wall.edges { |
| 93 | let c = (e.p1 + e.p2) / 2.0; |
| 94 | let a = (e.p2 - e.p1).to_angle() + std::f64::consts::FRAC_PI_2.radians(); |
| 95 | |
| 96 | renderer.draw_line( |
| 97 | <(i32, i32)>::from(c.to_i32()), |
| 98 | <(i32, i32)>::from((c + Point::from(a) * 10.0).to_i32()), |
| 99 | (0, 128, 255)); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // walls |
| 105 | for wall in &self.walls { |
| 106 | for e in &wall.edges { |
| 107 | if !debug_mode { |
| 108 | let c = (e.p1 + e.p2) / 2.0; |
| 109 | let a = e.normal().reverse(); |
| 110 | |
| 111 | renderer.draw_line( |
| 112 | <(i32, i32)>::from(c.to_i32()), |
| 113 | <(i32, i32)>::from((c + Point::from(a) * 10.0).to_i32()), |
| 114 | (255, 128, 0)); |
| 115 | |
| 116 | renderer.draw_line( |
| 117 | <(i32, i32)>::from(e.p1.to_i32()), |
| 118 | <(i32, i32)>::from((c + Point::from(a) * 20.0).to_i32()), |
| 119 | (96, 48, 0)); |
| 120 | renderer.draw_line( |
| 121 | <(i32, i32)>::from(e.p2.to_i32()), |
| 122 | <(i32, i32)>::from((c + Point::from(a) * 20.0).to_i32()), |
| 123 | (96, 48, 0)); |
| 124 | } |
| 125 | |
| 126 | renderer.draw_line( |
| 127 | <(i32, i32)>::from(e.p1.to_i32()), |
| 128 | <(i32, i32)>::from(e.p2.to_i32()), |
| 129 | (255, 255, 0)); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | pub fn intersect_walls(&self, p1: Point<f64>, p2: Point<f64>) -> IntersectResult { |
| 135 | for c in self.wall_grid.grid_coordinates_on_line(p1, p2) { |
| 136 | for w in &self.wall_grid.cells[c.x][c.y] { |
| 137 | if let Intersection::Point(p) = Intersection::lines(p1, p2, w.p1, w.p2) { |
| 138 | if w.point_is_in_front(p1) { |
| 139 | let wall = Wall { |
| 140 | region: Rc::clone(&self.walls[w.region]), |
| 141 | edge: Rc::clone(w), |
| 142 | }; |
| 143 | return IntersectResult::Intersection(wall, p) |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | IntersectResult::None |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | pub enum IntersectResult { |
| 153 | Intersection(Wall, Point<f64>), |
| 154 | None |
| 155 | } |
| 156 | |
| 157 | ////////// GRID //////////////////////////////////////////////////////////////// |
| 158 | |
| 159 | #[derive(Debug, Default)] |
| 160 | pub struct Grid<T> { |
| 161 | pub size: Dimension<usize>, |
| 162 | pub scale: Dimension<f64>, |
| 163 | pub cells: Vec<Vec<T>>, |
| 164 | } |
| 165 | |
| 166 | impl<T> Grid<T> { |
| 167 | // pub fn at<C>(&self, c: C) -> Option<&T> |
| 168 | // where C: Into<(isize, isize)> |
| 169 | // { |
| 170 | // let c = c.into(); |
| 171 | // if c.0 >= 0 && c.0 < self.size.width as isize && c.1 >= 0 && c.1 < self.size.height as isize { |
| 172 | // Some(&self.cells[c.0 as usize][c.1 as usize]) |
| 173 | // } else { |
| 174 | // None |
| 175 | // } |
| 176 | // } |
| 177 | |
| 178 | pub fn to_grid_coordinate<C>(&self, c: C) -> Option<Point<usize>> |
| 179 | where C: Into<(isize, isize)> |
| 180 | { |
| 181 | let c = c.into(); |
| 182 | if c.0 >= 0 && c.0 < self.size.width as isize && c.1 >= 0 && c.1 < self.size.height as isize { |
| 183 | Some(point!(c.0 as usize, c.1 as usize)) |
| 184 | } else { |
| 185 | None |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /// Returns a list of grid coordinates that a line in world coordinates passes through. |
| 190 | pub fn grid_coordinates_on_line(&self, p1: Point<f64>, p2: Point<f64>) -> Vec<Point<usize>> { |
| 191 | supercover_line(p1 / self.scale, p2 / self.scale) |
| 192 | .iter() |
| 193 | .map(|c| self.to_grid_coordinate(*c)) |
| 194 | .flatten() |
| 195 | .collect() |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | ////////// WALL REGION ///////////////////////////////////////////////////////// |
| 200 | |
| 201 | #[derive(Debug, Default)] |
| 202 | pub struct WallRegion { |
| 203 | edges: Vec<Rc<WallEdge>>, |
| 204 | } |
| 205 | |
| 206 | impl WallRegion { |
| 207 | pub fn new(index: RegionIndex, points: Vec<Point<f64>>) -> Self { |
| 208 | let mut edges = Vec::with_capacity(points.len()); |
| 209 | |
| 210 | for i in 0..points.len() { |
| 211 | let edge = Rc::new(WallEdge { |
| 212 | region: index, |
| 213 | id: i, |
| 214 | p1: points[i], |
| 215 | p2: points[(i + 1) % points.len()], |
| 216 | }); |
| 217 | edges.push(edge); |
| 218 | } |
| 219 | |
| 220 | WallRegion { edges } |
| 221 | } |
| 222 | |
| 223 | fn next(&self, index: EdgeIndex) -> Rc<WallEdge> { |
| 224 | let index = (index + 1) % self.edges.len(); |
| 225 | self.edges[index].clone() |
| 226 | } |
| 227 | |
| 228 | fn previous(&self, index: EdgeIndex) -> Rc<WallEdge> { |
| 229 | let index = (index + self.edges.len() - 1) % self.edges.len(); |
| 230 | self.edges[index].clone() |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | ////////// WALL EDGE /////////////////////////////////////////////////////////// |
| 235 | |
| 236 | type RegionIndex = usize; |
| 237 | type EdgeIndex = usize; |
| 238 | |
| 239 | #[derive(Debug, Default)] |
| 240 | struct WallEdge { |
| 241 | region: RegionIndex, |
| 242 | id: EdgeIndex, |
| 243 | pub p1: Point<f64>, |
| 244 | pub p2: Point<f64>, |
| 245 | } |
| 246 | |
| 247 | impl WallEdge { |
| 248 | fn point_is_in_front(&self, p: Point<f64>) -> bool { |
| 249 | let cross = (self.p2 - self.p1).cross_product(p - self.p1); |
| 250 | cross > 0.0 |
| 251 | } |
| 252 | |
| 253 | fn normal(&self) -> Angle { |
| 254 | self.angle() + std::f64::consts::FRAC_PI_2.radians() |
| 255 | } |
| 256 | |
| 257 | /// Angle from the right to the left point if the normal is up. |
| 258 | fn angle(&self) -> Angle { |
| 259 | (self.p2 - self.p1).to_angle() |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | ////////// WALL //////////////////////////////////////////////////////////////// |
| 264 | |
| 265 | pub struct Wall { |
| 266 | region: Rc<WallRegion>, |
| 267 | edge: Rc<WallEdge>, |
| 268 | } |
| 269 | |
| 270 | impl Wall { |
| 271 | #[allow(dead_code)] |
| 272 | pub fn next(&self) -> Wall { |
| 273 | Wall { |
| 274 | edge: self.region.next(self.edge.id), |
| 275 | region: self.region.clone(), |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | #[allow(dead_code)] |
| 280 | pub fn previous(&self) -> Wall { |
| 281 | Wall { |
| 282 | edge: self.region.previous(self.edge.id), |
| 283 | region: self.region.clone(), |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | pub fn normal(&self) -> Angle { |
| 288 | self.edge.normal() |
| 289 | } |
| 290 | |
| 291 | pub fn angle(&self) -> Angle { |
| 292 | self.edge.angle() |
| 293 | } |
| 294 | |
| 295 | pub fn from_2d(&self, pos: &Point<f64>, vel: &Point<f64>) -> (f64, f64) { |
| 296 | let pos = self.projection_of(*pos - self.edge.p1); |
| 297 | let vel = self.projection_of(*vel); |
| 298 | (pos, vel) |
| 299 | } |
| 300 | |
| 301 | pub fn to_2d(&self, pos: f64, vel: f64) -> (Point<f64>, Point<f64>) { |
| 302 | let a = Point::from(self.edge.angle()); |
| 303 | let pos = self.edge.p1 + a * pos; |
| 304 | let vel = a * vel; |
| 305 | (pos, vel) |
| 306 | } |
| 307 | |
| 308 | /// Returns the 1D position of a point projected onto this wall. |
| 309 | /// This is done by rotating the point using a rotation matrix and then taking the resulting x value. |
| 310 | /// x'=xcosâysin <- only this is used |
| 311 | /// y'=xsin+ycos |
| 312 | fn projection_of(&self, p: Point<f64>) -> f64 { |
| 313 | let r = Point::from(self.edge.angle()); |
| 314 | p.x * r.x + p.y * r.y // r.y is inverted here instead of inverting the angle |
| 315 | } |
| 316 | } |