WIP: Wall intersection
[kaka/rust-sdl-test.git] / src / core / level / mod.rs
1 use common::{Point, Dimension, Intersection};
2 use core::render::Renderer;
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<Rc<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.cell_size);
26         Level {
27             gravity,
28             grid,
29             walls,
30             wall_grid,
31         }
32     }
33
34     /// Creates a grid of wall edges for fast lookup
35     fn build_wall_grid(walls: &mut Vec<Rc<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!(cell_size.width as f64, cell_size.height as f64);
39         let mut grid = vec!(vec!(vec!(); size.height); size.width);
40
41         for wall in walls {
42             for edge in &wall.edges {
43                 // TODO: include cells that this edge overlaps
44                 for p in &[edge.p1, edge.p2] {
45                     let p = point!(p.x as usize, p.y as usize) / cs;
46                     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));
47                 }
48             }
49         }
50
51         Grid {
52             size,
53             cell_size: dimen!(cs.x, cs.y),
54             cells: grid,
55         }
56     }
57
58     pub fn render(&mut self, renderer: &mut Renderer, _sprites: &SpriteManager) {
59         // original grid
60         renderer.canvas().set_draw_color((64, 64, 64));
61         let size = &self.grid.cell_size;
62         for x in 0..self.grid.size.width {
63             for y in 0..self.grid.size.height {
64                 if self.grid.cells[x][y] {
65                     renderer.canvas().fill_rect(sdl2::rect::Rect::new(
66                         x as i32 * size.width as i32,
67                         y as i32 * size.height as i32,
68                         size.width as u32,
69                         size.height as u32)).unwrap();
70                 }
71             }
72         }
73
74         // wall grid
75         renderer.canvas().set_draw_color((0, 32, 0));
76         let size = &self.wall_grid.cell_size;
77         for x in 0..self.wall_grid.size.width {
78             for y in 0..self.wall_grid.size.height {
79                 if !self.wall_grid.cells[x][y].is_empty() {
80                     renderer.canvas().fill_rect(sdl2::rect::Rect::new(
81                         x as i32 * size.width as i32,
82                         y as i32 * size.height as i32,
83                         size.width as u32,
84                         size.height as u32)).unwrap();
85                 }
86             }
87         }
88
89         // walls
90         for wall in &self.walls {
91             for e in &wall.edges {
92                 renderer.draw_line(
93                     <(i32, i32)>::from(e.p1.to_i32()),
94                     <(i32, i32)>::from(e.p2.to_i32()),
95                     (255, 255, 0));
96             }
97         }
98     }
99
100     pub fn intersect_walls(&self, p1: Point<f64>, p2: Point<f64>) -> IntersectResult {
101         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);
102         if let Some(walls) = self.wall_grid.at(c) {
103             for w in walls {
104                 if let Intersection::Point(p) = Intersection::lines(p1, p2, w.p1, w.p2) {
105                     let wall = Wall {
106                         edge: Rc::clone(&w),
107                     };
108                     return IntersectResult::Intersection(wall, p)
109                 }
110             }
111         }
112         IntersectResult::None
113     }
114 }
115
116 pub enum IntersectResult {
117     Intersection(Wall, Point<f64>),
118     None
119 }
120
121 ////////// GRID ////////////////////////////////////////////////////////////////
122
123 #[derive(Debug, Default)]
124 pub struct Grid<T> {
125     pub size: Dimension<usize>,
126     pub cell_size: Dimension<usize>,
127     pub cells: Vec<Vec<T>>,
128 }
129
130 impl<T> Grid<T> {
131     pub fn at<C>(&self, c: C) -> Option<&T>
132     where C: Into<(isize, isize)>
133     {
134         let c = c.into();
135         if c.0 >= 0 && c.0 < self.size.width as isize && c.1 >= 0 && c.1 < self.size.height as isize {
136             Some(&self.cells[c.0 as usize][c.1 as usize])
137         } else {
138             None
139         }
140     }
141 }
142
143 ////////// WALL REGION /////////////////////////////////////////////////////////
144
145 #[derive(Debug)]
146 pub struct WallRegion {
147     edges: Vec<Rc<WallEdge>>,
148 }
149
150 impl WallRegion {
151     pub fn new(points: Vec<Point<f64>>) -> Rc<Self> {
152         let mut edges = Vec::with_capacity(points.len());
153
154         for i in 0..points.len() {
155             let edge = Rc::new(WallEdge {
156                 index: i,
157                 p1: points[i],
158                 p2: points[(i + 1) % points.len()],
159             });
160             edges.push(edge);
161         }
162
163         Rc::new(WallRegion { edges })
164     }
165
166     #[allow(dead_code)]
167     fn next(&self, index: usize) -> Rc<WallEdge> {
168         let index = (index + 1) % self.edges.len();
169         Rc::clone(&self.edges[index])
170     }
171
172     #[allow(dead_code)]
173     fn previous(&self, index: usize) -> Rc<WallEdge> {
174         let index = (index + self.edges.len() + 1) % self.edges.len();
175         Rc::clone(&self.edges[index])
176     }
177 }
178
179 ////////// WALL EDGE ///////////////////////////////////////////////////////////
180
181 #[derive(Debug, Default)]
182 struct WallEdge {
183     index: usize,
184     pub p1: Point<f64>,
185     pub p2: Point<f64>,
186 }
187
188 ////////// WALL ////////////////////////////////////////////////////////////////
189
190 pub struct Wall {
191 //    region: Rc<WallRegion>,
192     edge: Rc<WallEdge>,
193 }