use common::{Point, Dimension, Intersection}; use core::render::Renderer; use sprites::SpriteManager; use std::rc::Rc; use {point, dimen}; mod lvlgen; pub use self::lvlgen::LevelGenerator; ////////// LEVEL /////////////////////////////////////////////////////////////// #[derive(Default)] pub struct Level { pub gravity: Point, pub grid: Grid, walls: Vec>, wall_grid: Grid>>, } impl Level { 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); Level { gravity, grid, walls, wall_grid, } } /// Creates a grid of wall edges for fast lookup 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); 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)); } } } Grid { size, cell_size: dimen!(cs.x, cs.y), cells: 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(); } } } // 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(); } } } // walls for wall in &self.walls { for e in &wall.edges { renderer.draw_line( <(i32, i32)>::from(e.p1.to_i32()), <(i32, i32)>::from(e.p2.to_i32()), (255, 255, 0)); } } } 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) } } } IntersectResult::None } } pub enum IntersectResult { Intersection(Wall, Point), None } ////////// GRID //////////////////////////////////////////////////////////////// #[derive(Debug, Default)] pub struct Grid { pub size: Dimension, pub cell_size: Dimension, 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 } } } ////////// WALL REGION ///////////////////////////////////////////////////////// #[derive(Debug)] pub struct WallRegion { edges: Vec>, } impl WallRegion { pub fn new(points: Vec>) -> Rc { let mut edges = Vec::with_capacity(points.len()); for i in 0..points.len() { let edge = Rc::new(WallEdge { index: i, p1: points[i], p2: points[(i + 1) % points.len()], }); edges.push(edge); } Rc::new(WallRegion { edges }) } #[allow(dead_code)] fn next(&self, index: usize) -> 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]) } } ////////// WALL EDGE /////////////////////////////////////////////////////////// #[derive(Debug, Default)] struct WallEdge { index: usize, pub p1: Point, pub p2: Point, } ////////// WALL //////////////////////////////////////////////////////////////// pub struct Wall { // region: Rc, edge: Rc, }