WIP: Wall intersection
[kaka/rust-sdl-test.git] / src / core / game.rs
index 554bde7..386057b 100644 (file)
@@ -4,7 +4,7 @@ use common::{Point, Radians};
 use core::app::StateChange;
 use core::controller::Controller;
 use core::controller::ControllerManager;
-use core::level::{Level, LevelGenerator};
+use core::level::{Level, LevelGenerator, Wall, IntersectResult::Intersection};
 use core::render::Renderer;
 use point;
 use sdl2::event::Event;
@@ -271,12 +271,11 @@ impl Object for Boll {
        self.vel += lvl.gravity;
        self.pos += self.vel;
 
-       let x = (self.pos.x / lvl.grid.cell_size.width as f64).min(lvl.grid.size.width as f64 - 1.0).max(0.0) as usize;
-       let y = (self.pos.y / lvl.grid.cell_size.height as f64).min(lvl.grid.size.height as f64 - 1.0).max(0.0) as usize;
-       if lvl.grid.cells[x][y] {
+       if let Intersection(wall, pos) = lvl.intersect_walls(self.pos - self.vel, self.pos) {
            if self.bounces == 0 {
                return Dead
            }
+           self.pos = pos;
            self.vel *= -0.25;
            self.pos += self.vel;
            self.bounces -= 1;
@@ -288,6 +287,23 @@ impl Object for Boll {
                ..*self
            }));
        }
+       // let x = (self.pos.x / lvl.grid.cell_size.width as f64).min(lvl.grid.size.width as f64 - 1.0).max(0.0) as usize;
+       // let y = (self.pos.y / lvl.grid.cell_size.height as f64).min(lvl.grid.size.height as f64 - 1.0).max(0.0) as usize;
+       // if lvl.grid.cells[x][y] {
+       //     if self.bounces == 0 {
+       //      return Dead
+       //     }
+       //     self.vel *= -0.25;
+       //     self.pos += self.vel;
+       //     self.bounces -= 1;
+       //     use rand::distributions::{Distribution, Normal};
+       //     let mut rng = rand::thread_rng();
+       //     let a = Radians(self.vel.to_radians().0 + Normal::new(0.0, 0.75).sample(&mut rng));
+       //     objects.push(Box::new(Boll {
+       //      vel: Point::from(a) * Normal::new(1.0, 0.25).sample(&mut rng) * self.vel.length(),
+       //      ..*self
+       //     }));
+       // }
 
        Alive
     }