From 1c3cf1cc0dd2cb592c6535379b61688d4b90b430 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Thu, 12 Apr 2018 21:48:06 +0200 Subject: Added random bug generation --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0eb28fa..b03a888 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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]] diff --git a/Cargo.toml b/Cargo.toml index fa8d4bd..5b2b4ed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ build = "build.rs" [dependencies] gate = { path = "../gate/gate" } +rand = "0.4" [build-dependencies] gate_build = { path = "../gate/gate_build" } 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, home: Home, points: i64, lives: i64, - game_over: bool + game_over: bool, + time_to_next_bug: f64 } impl App for BugBasherGame { @@ -31,12 +39,32 @@ impl App for BugBasherGame { } fn advance(&mut self, seconds: f64, _audio: &mut Audio) -> 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 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 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; -- cgit v1.2.3