Bolls bounce on walls
[kaka/rust-sdl-test.git] / src / core / game.rs
index e335798..8474fab 100644 (file)
@@ -1,4 +1,5 @@
 use AppState;
+use sdl2::joystick::PowerLevel;
 use common::Point2D;
 use core::controller::Controller;
 use core::controller::ControllerManager;
@@ -176,6 +177,18 @@ impl Object for Character {
            ctrl.rumble(1.0, dt);
        }
 
+       if ctrl.start.is_pressed && !ctrl.start.was_pressed {
+           match ctrl.device.power_level() {
+               Ok(PowerLevel::Unknown) => { println!("power level unknown"); }
+               Ok(PowerLevel::Empty) => { println!("power level empty"); }
+               Ok(PowerLevel::Low) => { println!("power level low"); }
+               Ok(PowerLevel::Medium) => { println!("power level medium"); }
+               Ok(PowerLevel::Full) => { println!("power level full"); }
+               Ok(PowerLevel::Wired) => { println!("power level wired"); }
+               Err(_) => {}
+           };
+       }
+
        match ctrl.mov.x {
            v if v < -0.9 => { self.vel.x -= 0.5 }
            v if v > 0.9 => { self.vel.x += 0.5 }
@@ -225,7 +238,7 @@ pub struct Boll {
 }
 
 impl Object for Boll {
-    fn update(&mut self, _objects: &mut Objects, lvl: &Level, _dt: Duration) -> ObjectState {
+    fn update(&mut self, objects: &mut Objects, lvl: &Level, _dt: Duration) -> ObjectState {
        self.vel += lvl.gravity;
        self.pos += self.vel;
 
@@ -239,6 +252,18 @@ impl Object for Boll {
            }
        }
 
+       if self.pos.x <= 0.0 || self.pos.x >= 1280.0 { // only for testing
+           self.pos.x = self.pos.x.max(0.0).min(1280.0);
+           self.vel.x = -self.vel.x;
+           self.bounces = 0;
+           use rand::distributions::{Distribution, Normal};
+           let normal = Normal::new(0.5, 0.4);
+           objects.push(Box::new(Boll {
+               vel: self.vel * normal.sample(&mut rand::thread_rng()),
+               ..*self
+           }));
+       }
+
        Alive
     }