Replaced reference with smart pointer in Wall
[kaka/rust-sdl-test.git] / src / core / level / mod.rs
CommitLineData
79914631 1use core::render::Renderer;
953b4c96 2use geometry::{Point, Dimension, Intersection, Angle, ToAngle, supercover_line};
79914631 3use sprites::SpriteManager;
1f42d724
TW
4use std::rc::Rc;
5use {point, dimen};
79914631
TW
6
7mod lvlgen;
8
9pub use self::lvlgen::LevelGenerator;
10
11////////// LEVEL ///////////////////////////////////////////////////////////////
12
13#[derive(Default)]
14pub struct Level {
e570927a 15 pub gravity: Point<f64>,
37f3e1ed 16 pub grid: Grid<bool>,
2e679e6f 17 walls: Vec<Rc<WallRegion>>,
1f42d724 18 wall_grid: Grid<Vec<Rc<WallEdge>>>,
79914631
TW
19}
20
21impl Level {
d59c7f04 22 pub fn new(gravity: Point<f64>, grid: Grid<bool>, mut walls: Vec<WallRegion>) -> Self {
1f42d724
TW
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());
d59c7f04 25 dbg!(&wall_grid.scale);
1f42d724
TW
26 Level {
27 gravity,
28 grid,
2e679e6f 29 walls: walls.into_iter().map(|i| Rc::new(i)).collect(),
1f42d724
TW
30 wall_grid,
31 }
32 }
33
34 /// Creates a grid of wall edges for fast lookup
d01df1fc 35 fn build_wall_grid(walls: &mut Vec<WallRegion>, lvlsize: &Dimension<usize>) -> Grid<Vec<Rc<WallEdge>>> {
1f42d724
TW
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);
d59c7f04 38 //let cs = point!(scale.width as f64, scale.height as f64);
8012f86b
TW
39 let mut grid = Grid {
40 cells: vec!(vec!(vec!(); size.height); size.width),
41 size,
d59c7f04 42 scale: dimen!(cs.x as f64, cs.y as f64),
8012f86b 43 };
1f42d724
TW
44
45 for wall in walls {
46 for edge in &wall.edges {
8012f86b
TW
47 for c in grid.grid_coordinates_on_line(edge.p1, edge.p2) {
48 grid.cells[c.x][c.y].push(Rc::clone(edge));
1f42d724
TW
49 }
50 }
51 }
52
8012f86b 53 grid
1f42d724 54 }
79914631 55
7b724ff3
TW
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 }
1f42d724
TW
87 }
88 }
1f42d724 89
7b724ff3
TW
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));
79914631
TW
100 }
101 }
102 }
103
1f42d724 104 // walls
79914631 105 for wall in &self.walls {
1f42d724 106 for e in &wall.edges {
7b724ff3
TW
107 if !debug_mode {
108 let c = (e.p1 + e.p2) / 2.0;
109 let a = (e.p2 - e.p1).to_angle() - std::f64::consts::FRAC_PI_2.radians();
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 }
8065e264 125
1f42d724
TW
126 renderer.draw_line(
127 <(i32, i32)>::from(e.p1.to_i32()),
128 <(i32, i32)>::from(e.p2.to_i32()),
129 (255, 255, 0));
79914631 130 }
79914631
TW
131 }
132 }
60058b91
TW
133
134 pub fn intersect_walls(&self, p1: Point<f64>, p2: Point<f64>) -> IntersectResult {
8012f86b 135 for c in self.wall_grid.grid_coordinates_on_line(p1, p2) {
b5332019
TW
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 let wall = Wall {
2e679e6f
TW
139 region: Rc::clone(&self.walls[w.region]),
140 edge: Rc::clone(w),
b5332019
TW
141 };
142 return IntersectResult::Intersection(wall, p)
60058b91
TW
143 }
144 }
145 }
146 IntersectResult::None
147 }
148}
149
2e679e6f
TW
150pub enum IntersectResult {
151 Intersection(Wall, Point<f64>),
60058b91 152 None
79914631
TW
153}
154
155////////// GRID ////////////////////////////////////////////////////////////////
156
1f42d724 157#[derive(Debug, Default)]
37f3e1ed 158pub struct Grid<T> {
1f42d724 159 pub size: Dimension<usize>,
d59c7f04 160 pub scale: Dimension<f64>,
37f3e1ed 161 pub cells: Vec<Vec<T>>,
79914631 162}
1f42d724 163
60058b91 164impl<T> Grid<T> {
2e679e6f
TW
165 // pub fn at<C>(&self, c: C) -> Option<&T>
166 // where C: Into<(isize, isize)>
167 // {
168 // let c = c.into();
169 // if c.0 >= 0 && c.0 < self.size.width as isize && c.1 >= 0 && c.1 < self.size.height as isize {
170 // Some(&self.cells[c.0 as usize][c.1 as usize])
171 // } else {
172 // None
173 // }
174 // }
8012f86b
TW
175
176 pub fn to_grid_coordinate<C>(&self, c: C) -> Option<Point<usize>>
177 where C: Into<(isize, isize)>
178 {
179 let c = c.into();
180 if c.0 >= 0 && c.0 < self.size.width as isize && c.1 >= 0 && c.1 < self.size.height as isize {
181 Some(point!(c.0 as usize, c.1 as usize))
182 } else {
183 None
184 }
185 }
186
187 /// Returns a list of grid coordinates that a line in world coordinates passes through.
188 pub fn grid_coordinates_on_line(&self, p1: Point<f64>, p2: Point<f64>) -> Vec<Point<usize>> {
d59c7f04 189 supercover_line(p1 / self.scale, p2 / self.scale)
8012f86b
TW
190 .iter()
191 .map(|c| self.to_grid_coordinate(*c))
192 .flatten()
193 .collect()
194 }
60058b91
TW
195}
196
1f42d724
TW
197////////// WALL REGION /////////////////////////////////////////////////////////
198
2e679e6f 199#[derive(Debug, Default)]
1f42d724
TW
200pub struct WallRegion {
201 edges: Vec<Rc<WallEdge>>,
202}
203
204impl WallRegion {
d01df1fc
TW
205 pub fn new(points: Vec<Point<f64>>) -> Self {
206 let index: RegionIndex = 0; // use as param
60058b91 207 let mut edges = Vec::with_capacity(points.len());
1f42d724
TW
208
209 for i in 0..points.len() {
210 let edge = Rc::new(WallEdge {
d01df1fc
TW
211 region: index,
212 id: i,
1f42d724
TW
213 p1: points[i],
214 p2: points[(i + 1) % points.len()],
215 });
216 edges.push(edge);
217 }
218
d01df1fc 219 WallRegion { edges }
1f42d724
TW
220 }
221
2e679e6f
TW
222 fn next(&self, index: EdgeIndex) -> Rc<WallEdge> {
223 let index = (index + 1) % self.edges.len();
224 Rc::clone(&self.edges[index])
225 }
d01df1fc 226
2e679e6f
TW
227 fn previous(&self, index: EdgeIndex) -> Rc<WallEdge> {
228 let index = (index + self.edges.len() + 1) % self.edges.len();
229 Rc::clone(&self.edges[index])
230 }
1f42d724
TW
231}
232
233////////// WALL EDGE ///////////////////////////////////////////////////////////
234
d01df1fc
TW
235type RegionIndex = usize;
236type EdgeIndex = usize;
237
1f42d724
TW
238#[derive(Debug, Default)]
239struct WallEdge {
d01df1fc
TW
240 region: RegionIndex,
241 id: EdgeIndex,
1f42d724
TW
242 pub p1: Point<f64>,
243 pub p2: Point<f64>,
244}
60058b91
TW
245
246////////// WALL ////////////////////////////////////////////////////////////////
247
2e679e6f
TW
248pub struct Wall {
249 region: Rc<WallRegion>,
250 edge: Rc<WallEdge>,
d01df1fc
TW
251}
252
2e679e6f
TW
253impl Wall {
254 #[allow(dead_code)]
255 pub fn next(self) -> Wall {
d01df1fc 256 Wall {
2e679e6f 257 edge: self.region.next(self.edge.id),
d01df1fc 258 region: self.region,
d01df1fc
TW
259 }
260 }
261
2e679e6f
TW
262 #[allow(dead_code)]
263 pub fn previous(self) -> Wall {
d01df1fc 264 Wall {
2e679e6f 265 edge: self.region.previous(self.edge.id),
d01df1fc 266 region: self.region,
d01df1fc
TW
267 }
268 }
8065e264 269
40742678
TW
270 pub fn normal(&self) -> Angle {
271 (self.edge.p2 - self.edge.p1).to_angle() + std::f64::consts::FRAC_PI_2.radians()
8065e264 272 }
60058b91 273}