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