Added random bug generation
authorJustin Worthe <justin@worthe-it.co.za>
Thu, 12 Apr 2018 19:48:06 +0000 (21:48 +0200)
committerJustin Worthe <justin@worthe-it.co.za>
Thu, 12 Apr 2018 19:48:06 +0000 (21:48 +0200)
Cargo.lock
Cargo.toml
src/main.rs

index 0eb28fa..b03a888 100644 (file)
@@ -40,6 +40,7 @@ version = "0.1.0"
 dependencies = [
  "gate 0.2.1",
  "gate_build 0.2.1",
+ "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
index fa8d4bd..5b2b4ed 100644 (file)
@@ -7,6 +7,7 @@ build = "build.rs"
 
 [dependencies]
 gate = { path = "../gate/gate" }
+rand = "0.4"
 
 [build-dependencies]
 gate_build = { path = "../gate/gate_build" }
index daa36c8..939c414 100644 (file)
@@ -5,6 +5,12 @@ use gate::app_info::AppInfo;
 use gate::input::*;
 use gate::renderer::{Renderer, Affine};
 
+extern crate rand;
+
+use rand::distributions::IndependentSample;
+use rand::Isaac64Rng;
+use std::f64::consts::PI;
+                          
 mod asset_id { include!(concat!(env!("OUT_DIR"), "/asset_id.rs")); }
 use asset_id::*;
 
@@ -19,11 +25,13 @@ use entities::bug::Bug;
 use entities::home::Home;
 
 struct BugBasherGame {
+    rng: Isaac64Rng,
     bugs: Vec<Bug>,
     home: Home,
     points: i64,
     lives: i64,
-    game_over: bool
+    game_over: bool,
+    time_to_next_bug: f64
 }
 
 impl App<AssetId> for BugBasherGame {
@@ -31,12 +39,32 @@ impl App<AssetId> for BugBasherGame {
     }
 
     fn advance(&mut self, seconds: f64, _audio: &mut Audio<AssetId>) -> bool {
-        self.bugs.retain(|b| b.alive);
-        for bug in self.bugs.iter_mut() {
-            bug.advance(seconds);
-            if self.home.touches_circle(bug) {
-                bug.alive = false;
-                self.lives -= 1;
+        if !self.game_over {
+            self.bugs.retain(|b| b.alive);
+            for bug in self.bugs.iter_mut() {
+                bug.advance(seconds);
+                if self.home.touches_circle(bug) {
+                    bug.alive = false;
+                    self.lives -= 1;
+                }
+            }
+            if self.lives <= 0 {
+                self.game_over = true;
+            }
+        
+            self.time_to_next_bug -= seconds;
+            if self.time_to_next_bug <= 0. {
+                let time_dist = rand::distributions::Normal::new(2.0, 1.0);
+                self.time_to_next_bug = time_dist.ind_sample(&mut self.rng);
+
+                let angle_dist = rand::distributions::Range::new(0., 2.*PI);
+                let angle = angle_dist.ind_sample(&mut self.rng);
+                self.bugs.push(Bug::new(
+                    angle.cos()*1000.,
+                    angle.sin()*1000.,
+                    angle + PI
+                ));
+                
             }
         }
         
@@ -47,10 +75,16 @@ impl App<AssetId> for BugBasherGame {
         match evt {
             InputEvent::MousePressed(MouseButton::Left, x, y) => {
                 for bug in self.bugs.iter_mut().filter(|bug| bug.touches_point(Vec2d { x, y })) {
+                    if !self.game_over && bug.alive == true {
+                        self.points += 1;
+                    }
+
                     bug.alive = false;
-                    self.points += 1;
                 }
             },
+            InputEvent::KeyPressed(KeyCode::Return) => {
+                self.reset();
+            },
             _ => {}
         }
         true
@@ -84,17 +118,20 @@ impl App<AssetId> for BugBasherGame {
 impl BugBasherGame {
     fn new() -> BugBasherGame {
         let mut game = BugBasherGame {
+            rng: Isaac64Rng::new_unseeded(),
             home: Home::new(0., 0.),
             bugs: Vec::new(),
             points: 0,
             lives: 0,
-            game_over: true
+            game_over: true,
+            time_to_next_bug: 0.
         };
         game.reset();
         game
     }
 
     fn reset(&mut self) {
+        self.bugs = Vec::new();
         self.points = 0;
         self.lives = 3;
         self.game_over = false;