summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs55
1 files changed, 46 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs
index daa36c8..939c414 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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;