Bounce bolls off of walls in a proper angle
[kaka/rust-sdl-test.git] / src / core / level / mod.rs
index 5df8ba6..cccd253 100644 (file)
@@ -1,4 +1,4 @@
-use common::{Point, Dimension, Intersection, supercover_line};
+use common::{Point, Dimension, Intersection, Radians, supercover_line};
 use core::render::Renderer;
 use sprites::SpriteManager;
 use std::rc::Rc;
@@ -89,6 +89,15 @@ impl Level {
        // walls
        for wall in &self.walls {
            for e in &wall.edges {
+               let c = (e.p1 + e.p2) / 2.0;
+               let mut rad = (e.p2 - e.p1).to_radians();
+               rad.0 += std::f64::consts::FRAC_PI_2;
+
+               renderer.draw_line(
+                   <(i32, i32)>::from(c.to_i32()),
+                   <(i32, i32)>::from((c + Point::from(rad) * 10.0).to_i32()),
+                   (255, 128, 0));
+
                renderer.draw_line(
                    <(i32, i32)>::from(e.p1.to_i32()),
                    <(i32, i32)>::from(e.p2.to_i32()),
@@ -99,15 +108,13 @@ impl Level {
 
     pub fn intersect_walls(&self, p1: Point<f64>, p2: Point<f64>) -> IntersectResult {
        for c in self.wall_grid.grid_coordinates_on_line(p1, p2) {
-           if let walls = &self.wall_grid.cells[c.x][c.y] {
-               for w in walls {
-                   if let Intersection::Point(p) = Intersection::lines(p1, p2, w.p1, w.p2) {
-                       let wall = Wall {
-                           region: &self.walls[w.region],
-                           edge: w,
-                       };
-                       return IntersectResult::Intersection(wall, p)
-                   }
+           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 {
+                       region: &self.walls[w.region],
+                       edge: w,
+                   };
+                   return IntersectResult::Intersection(wall, p)
                }
            }
        }
@@ -240,4 +247,10 @@ impl<'a> Wall<'a> {
            edge,
        }
     }
+
+    pub fn normal(&self) -> Radians {
+       let mut rad = (self.edge.p2 - self.edge.p1).to_radians();
+       rad.0 += std::f64::consts::FRAC_PI_2;
+       rad
+    }
 }