Added random bug generation
[bug-basher.git] / src / main.rs
1 extern crate gate;
2
3 use gate::{App, Audio};
4 use gate::app_info::AppInfo;
5 use gate::input::*;
6 use gate::renderer::{Renderer, Affine};
7
8 extern crate rand;
9
10 use rand::distributions::IndependentSample;
11 use rand::Isaac64Rng;
12 use std::f64::consts::PI;
13                           
14 mod asset_id { include!(concat!(env!("OUT_DIR"), "/asset_id.rs")); }
15 use asset_id::*;
16
17 mod geometry;
18 use geometry::*;
19
20 mod hitbox;
21 use hitbox::*;
22
23 mod entities;
24 use entities::bug::Bug;
25 use entities::home::Home;
26
27 struct BugBasherGame {
28     rng: Isaac64Rng,
29     bugs: Vec<Bug>,
30     home: Home,
31     points: i64,
32     lives: i64,
33     game_over: bool,
34     time_to_next_bug: f64
35 }
36
37 impl App<AssetId> for BugBasherGame {
38     fn start(&mut self, _audio: &mut Audio<AssetId>) {
39     }
40
41     fn advance(&mut self, seconds: f64, _audio: &mut Audio<AssetId>) -> bool {
42         if !self.game_over {
43             self.bugs.retain(|b| b.alive);
44             for bug in self.bugs.iter_mut() {
45                 bug.advance(seconds);
46                 if self.home.touches_circle(bug) {
47                     bug.alive = false;
48                     self.lives -= 1;
49                 }
50             }
51             if self.lives <= 0 {
52                 self.game_over = true;
53             }
54         
55             self.time_to_next_bug -= seconds;
56             if self.time_to_next_bug <= 0. {
57                 let time_dist = rand::distributions::Normal::new(2.0, 1.0);
58                 self.time_to_next_bug = time_dist.ind_sample(&mut self.rng);
59
60                 let angle_dist = rand::distributions::Range::new(0., 2.*PI);
61                 let angle = angle_dist.ind_sample(&mut self.rng);
62                 self.bugs.push(Bug::new(
63                     angle.cos()*1000.,
64                     angle.sin()*1000.,
65                     angle + PI
66                 ));
67                 
68             }
69         }
70         
71         true
72     }
73
74     fn input(&mut self, evt: InputEvent, _audio: &mut Audio<AssetId>) -> bool {
75         match evt {
76             InputEvent::MousePressed(MouseButton::Left, x, y) => {
77                 for bug in self.bugs.iter_mut().filter(|bug| bug.touches_point(Vec2d { x, y })) {
78                     if !self.game_over && bug.alive == true {
79                         self.points += 1;
80                     }
81
82                     bug.alive = false;
83                 }
84             },
85             InputEvent::KeyPressed(KeyCode::Return) => {
86                 self.reset();
87             },
88             _ => {}
89         }
90         true
91     }
92
93     fn render(&mut self, renderer: &mut Renderer<AssetId>) {
94         let (app_width, app_height) = (renderer.app_width(), renderer.app_height());
95         renderer.clear((255,255,255));
96         {
97             let points_str = format!("{}", self.points);
98             let lives_str = format!("{}", self.lives);
99             BugBasherGame::print_string(renderer, &points_str, - app_width / 2. + 50., app_height / 2. - 50.);
100             BugBasherGame::print_string(renderer, &lives_str, app_width / 2. - 50. - lives_str.len() as f64 * 25., app_height / 2. - 50.);
101         }
102         {
103             let mut renderer = renderer.sprite_mode();
104             renderer.draw(
105                 &Affine::translate(self.home.pos.x, self.home.pos.y),
106                 SpriteId::Home
107             );
108             for bug in &self.bugs {
109                 renderer.draw(
110                     &Affine::translate(bug.pos.x, bug.pos.y).pre_rotate(bug.rotation),
111                     SpriteId::Bug
112                 );
113             }
114         }
115     }
116 }
117
118 impl BugBasherGame {
119     fn new() -> BugBasherGame {
120         let mut game = BugBasherGame {
121             rng: Isaac64Rng::new_unseeded(),
122             home: Home::new(0., 0.),
123             bugs: Vec::new(),
124             points: 0,
125             lives: 0,
126             game_over: true,
127             time_to_next_bug: 0.
128         };
129         game.reset();
130         game
131     }
132
133     fn reset(&mut self) {
134         self.bugs = Vec::new();
135         self.points = 0;
136         self.lives = 3;
137         self.game_over = false;
138     }
139
140     fn print_string(renderer: &mut Renderer<AssetId>, string: &str, x: f64, y: f64) {
141         let mut renderer = renderer.tiled_mode(-x, -y);
142         let spacing = 25.;
143         for (i, c) in string.chars().enumerate() {
144             let affine = Affine::translate(i as f64 * spacing, 0.);
145             let tile = match c {
146                 '-' => TileId::NumberFontR0C0,
147                 '0' => TileId::NumberFontR0C1,
148                 '1' => TileId::NumberFontR0C2,
149                 '2' => TileId::NumberFontR0C3,
150                 '3' => TileId::NumberFontR0C4,
151                 '4' => TileId::NumberFontR0C5,
152                 '5' => TileId::NumberFontR0C6,
153                 '6' => TileId::NumberFontR0C7,
154                 '7' => TileId::NumberFontR0C8,
155                 '8' => TileId::NumberFontR0C9,
156                 '9' => TileId::NumberFontR0C10,
157                 _ => TileId::NumberFontR0C0,
158             };
159             renderer.draw(&affine, tile);
160         };
161     }
162 }
163
164 fn main() {
165     let info = AppInfo::with_app_height(1000.).title("Bug Basher").build();
166     gate::run(info, BugBasherGame::new());
167 }