Replaced Degrees and Radians with a single Angle type
[kaka/rust-sdl-test.git] / src / core / game.rs
index 386057b..2fa7ddd 100644 (file)
@@ -1,6 +1,6 @@
-use ActiveState;
+use teststate::TestState;
 use AppState;
-use common::{Point, Radians};
+use common::{Point, ToAngle};
 use core::app::StateChange;
 use core::controller::Controller;
 use core::controller::ControllerManager;
@@ -58,7 +58,7 @@ impl AppState for GameState {
                return Some(StateChange::Pop)
             }
            Event::KeyDown { keycode: Some(Keycode::Return), .. } => {
-               return Some(StateChange::Push(Box::new(ActiveState::new((800, 600)))))
+               return Some(StateChange::Push(Box::new(TestState::new())))
             }
            Event::KeyDown { keycode: Some(Keycode::Space), .. } => {
                self.lvlgen.seed = std::time::UNIX_EPOCH.elapsed().unwrap().as_secs() as u32;
@@ -173,8 +173,8 @@ impl Object for Character {
     fn update(&mut self, objects: &mut Objects, lvl: &Level, dt: Duration) -> ObjectState {
        let ctrl = self.ctrl.borrow();
 
-       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;
+       let x = (self.pos.x / lvl.grid.scale.width as f64).min(lvl.grid.size.width as f64 - 1.0).max(0.0) as usize;
+       let y = (self.pos.y / lvl.grid.scale.height as f64).min(lvl.grid.size.height as f64 - 1.0).max(0.0) as usize;
        self.vel += lvl.gravity;
        if lvl.grid.cells[x][y] {
            if self.vel.y > 0.0 && !(ctrl.mov.down() && ctrl.jump.is_pressed) {
@@ -275,35 +275,22 @@ impl Object for Boll {
            if self.bounces == 0 {
                return Dead
            }
-           self.pos = pos;
-           self.vel *= -0.25;
-           self.pos += self.vel;
            self.bounces -= 1;
+           let mut a = wall.normal().mirror(self.vel.to_angle()); // TODO interpolera normalen mellan närliggande väggdelar? bollarna studsar väldigt "kantigt" nu
+           self.pos = pos;
+           self.vel = Point::from(a) * self.vel.length() * 0.35;
+           self.pos += self.vel; // TODO det här kan få bollen att åka igenom en närliggande vägg utan att kollisionstestas, men behövs just nu för att inte kollidera med samma vägg bakifrån
+
+           // create another boll
            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));
+           a += Normal::new(0.0, 0.1).sample(&mut rng).radians(); // TODO slumpen kan ge en vinkel som är under tangenten. vinkel-metoder på väggen istället kanske?
+           use rand::Rng;
            objects.push(Box::new(Boll {
-               vel: Point::from(a) * Normal::new(1.0, 0.25).sample(&mut rng) * self.vel.length(),
+               vel: Point::from(a) * Normal::new(1.0, 0.25).sample(&mut rng) * self.vel.length() * rng.gen_range(0.25, 1.0),
                ..*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
     }