c98c80ec39d1bbfa8d0bca341f18b7a7aa4f2d8e
[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     total_time: f64,
36     cursor_pos: Vec2d
37 }
38
39 impl App<AssetId> for BugBasherGame {
40     fn start(&mut self, _audio: &mut Audio<AssetId>) {
41     }
42
43     fn advance(&mut self, seconds: f64, _audio: &mut Audio<AssetId>) -> bool {
44         if !self.game_over {
45             self.bugs.retain(|b| b.alive);
46             for bug in self.bugs.iter_mut() {
47                 bug.advance(seconds);
48                 if self.home.touches_circle(bug) {
49                     bug.alive = false;
50                     self.lives -= 1;
51                 }
52             }
53             if self.lives <= 0 {
54                 self.game_over = true;
55             }
56         
57             self.time_to_next_bug -= seconds;
58             self.total_time += seconds;
59             
60             if self.time_to_next_bug <= 0. {
61                 let mean = f64::max(4. - (self.total_time as f64 * 0.25), 0.5);
62                 let sd = mean / 3.;
63                 let time_dist = rand::distributions::Normal::new(mean, sd);
64                 self.time_to_next_bug = time_dist.ind_sample(&mut self.rng);
65
66                 let angle_dist = rand::distributions::Range::new(0., 2.*PI);
67                 let angle = angle_dist.ind_sample(&mut self.rng);
68                 self.bugs.push(Bug::new(
69                     angle.cos()*1000.,
70                     angle.sin()*1000.
71                 ));
72                 
73             }
74         }
75         
76         true
77     }
78
79     fn input(&mut self, evt: InputEvent, _audio: &mut Audio<AssetId>) -> bool {
80         match evt {
81             InputEvent::MousePressed(MouseButton::Left, x, y) => {
82                 for bug in self.bugs.iter_mut().filter(|bug| bug.touches_point(Vec2d { x, y })) {
83                     if !self.game_over && bug.alive == true {
84                         self.points += 1;
85                     }
86
87                     bug.alive = false;
88                 }
89             },
90             InputEvent::MouseMotion(x, y) => {
91                 self.cursor_pos = Vec2d::new(x,y);
92             },
93             InputEvent::KeyPressed(KeyCode::Return) => {
94                 self.reset();
95             },
96             _ => {}
97         }
98         true
99     }
100
101     fn render(&mut self, renderer: &mut Renderer<AssetId>) {
102         let (app_width, app_height) = (renderer.app_width(), renderer.app_height());
103         renderer.clear((255,255,255));
104         {
105             let points_str = format!("{}", self.points);
106             let lives_str = format!("{}", self.lives);
107             BugBasherGame::print_string(renderer, &points_str, Alignment::Left, - app_width / 2. + 50., app_height / 2. - 50.);
108             BugBasherGame::print_string(renderer, &lives_str, Alignment::Right, app_width / 2. - 50., app_height / 2. - 50.);
109         }
110         {
111             let mut renderer = renderer.sprite_mode();
112             renderer.draw(
113                 &Affine::translate(self.home.pos.x, self.home.pos.y),
114                 SpriteId::Home
115             );
116             for bug in &self.bugs {
117                 renderer.draw(
118                     &Affine::translate(bug.pos.x, bug.pos.y).pre_rotate(bug.rotation),
119                     SpriteId::Bug
120                 );
121             }
122         }
123         
124         if self.game_over {
125             {
126                 let mut renderer = renderer.sprite_mode();
127                 renderer.draw(
128                     &Affine::translate(0.,0.),
129                     SpriteId::Gameover
130                 );
131             }
132             {
133                 let points_str = format!("{}", self.points);
134                 BugBasherGame::print_string(renderer, &points_str, Alignment::Center, 0., -25.);
135             }
136         }
137
138         {
139             let mut renderer = renderer.sprite_mode();
140             renderer.draw(
141                 &Affine::translate(self.cursor_pos.x, self.cursor_pos.y),
142                 SpriteId::Cursor
143             );
144         }
145     }
146 }
147
148 enum Alignment {
149     Left,
150     Right,
151     Center
152 }
153
154 impl BugBasherGame {
155     fn new() -> BugBasherGame {
156         let mut game = BugBasherGame {
157             rng: Isaac64Rng::new_unseeded(),
158             home: Home::new(0., 0.),
159             bugs: Vec::with_capacity(1000),
160             points: 0,
161             lives: 0,
162             game_over: true,
163             time_to_next_bug: 0.,
164             total_time: 0.,
165             cursor_pos: Vec2d::new(0.,0.)
166         };
167         game.reset();
168         game
169     }
170
171     fn reset(&mut self) {
172         self.bugs = Vec::with_capacity(1000);
173         self.points = 0;
174         self.lives = 3;
175         self.game_over = false;
176         self.time_to_next_bug = 0.;
177         self.total_time = 0.;
178     }
179
180     fn print_string(renderer: &mut Renderer<AssetId>, string: &str, alignment: Alignment, x: f64, y: f64) {
181         let letter_spacing = 45.;
182         let left = match alignment {
183             Alignment::Left => x,
184             Alignment::Right => x - string.len() as f64 * letter_spacing,
185             Alignment::Center => x - string.len() as f64 * letter_spacing / 2.
186         };
187         
188         let mut renderer = renderer.tiled_mode(-left, -y);
189
190         for (i, c) in string.chars().enumerate() {
191             let affine = Affine::translate(i as f64 * letter_spacing, 0.);
192             let tile = match c {
193                 '-' => TileId::NumberFontR0C0,
194                 '0' => TileId::NumberFontR0C1,
195                 '1' => TileId::NumberFontR0C2,
196                 '2' => TileId::NumberFontR0C3,
197                 '3' => TileId::NumberFontR0C4,
198                 '4' => TileId::NumberFontR0C5,
199                 '5' => TileId::NumberFontR0C6,
200                 '6' => TileId::NumberFontR0C7,
201                 '7' => TileId::NumberFontR0C8,
202                 '8' => TileId::NumberFontR0C9,
203                 '9' => TileId::NumberFontR0C10,
204                 _ => TileId::NumberFontR0C0,
205             };
206             renderer.draw(&affine, tile);
207         };
208     }
209 }
210
211 fn main() {
212     let info = AppInfo::with_app_height(1000.).title("Bug Basher").build();
213     gate::run(info, BugBasherGame::new());
214 }