Added escalating difficulty
authorJustin Worthe <justin@worthe-it.co.za>
Tue, 1 May 2018 09:15:14 +0000 (11:15 +0200)
committerJustin Worthe <justin@worthe-it.co.za>
Tue, 1 May 2018 09:15:14 +0000 (11:15 +0200)
src/geometry.rs
src/hitbox.rs
src/main.rs

index be903d7..c49fe2a 100644 (file)
@@ -8,8 +8,12 @@ pub struct Vec2d {
 
 impl Vec2d {
     pub fn distance(&self, other: Vec2d) -> f64 {
-        ((other.x-self.x).powi(2) + (other.y-self.y).powi(2)).sqrt()
+        self.distance_squared(other).sqrt()
     }
+    pub fn distance_squared(&self, other: Vec2d) -> f64 {
+        ((other.x-self.x).powi(2) + (other.y-self.y).powi(2))
+    }
+
     pub fn angle(&self) -> f64 {
         self.y.atan2(self.x)
     }
index df87b68..04de484 100644 (file)
@@ -9,6 +9,6 @@ pub trait CircleHitbox {
     }
 
     fn touches_circle(&self, other: &CircleHitbox) -> bool {
-        self.pos().distance(other.pos()) <= self.radius() + other.radius()
+        self.pos().distance_squared(other.pos()) <= (self.radius() + other.radius()).powi(2)
     }
 }
index 5547479..98dd39f 100644 (file)
@@ -10,13 +10,13 @@ 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::*;
 
 mod geometry;
 use geometry::*;
-
+    
 mod hitbox;
 use hitbox::*;
 
@@ -31,7 +31,8 @@ struct BugBasherGame {
     points: i64,
     lives: i64,
     game_over: bool,
-    time_to_next_bug: f64
+    time_to_next_bug: f64,
+    total_time: f64
 }
 
 impl App<AssetId> for BugBasherGame {
@@ -53,8 +54,12 @@ impl App<AssetId> for BugBasherGame {
             }
         
             self.time_to_next_bug -= seconds;
+            self.total_time += seconds;
+            
             if self.time_to_next_bug <= 0. {
-                let time_dist = rand::distributions::Normal::new(2.0, 1.0);
+                let mean = f64::max(4. - (self.total_time as f64 * 0.25), 0.5);
+                let sd = mean / 3.;
+                let time_dist = rand::distributions::Normal::new(mean, sd);
                 self.time_to_next_bug = time_dist.ind_sample(&mut self.rng);
 
                 let angle_dist = rand::distributions::Range::new(0., 2.*PI);
@@ -140,25 +145,28 @@ impl BugBasherGame {
         let mut game = BugBasherGame {
             rng: Isaac64Rng::new_unseeded(),
             home: Home::new(0., 0.),
-            bugs: Vec::new(),
+            bugs: Vec::with_capacity(1000),
             points: 0,
             lives: 0,
             game_over: true,
-            time_to_next_bug: 0.
+            time_to_next_bug: 0.,
+            total_time: 0.
         };
         game.reset();
         game
     }
 
     fn reset(&mut self) {
-        self.bugs = Vec::new();
+        self.bugs = Vec::with_capacity(1000);
         self.points = 0;
         self.lives = 3;
         self.game_over = false;
+        self.time_to_next_bug = 0.;
+        self.total_time = 0.;
     }
 
     fn print_string(renderer: &mut Renderer<AssetId>, string: &str, alignment: Alignment, x: f64, y: f64) {
-        let letter_spacing = 25.;
+        let letter_spacing = 45.;
         let left = match alignment {
             Alignment::Left => x,
             Alignment::Right => x - string.len() as f64 * letter_spacing,