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