Added hitboxes and something for the bugs to run into
[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 mod asset_id { include!(concat!(env!("OUT_DIR"), "/asset_id.rs")); }
9 use asset_id::*;
10
11 mod geometry;
12 use geometry::*;
13
14 mod hitbox;
15 use hitbox::*;
16
17 mod entities;
18 use entities::bug::Bug;
19 use entities::home::Home;
20
21 struct BugBasherGame {
22     bugs: Vec<Bug>,
23     home: Home,
24     points: i64
25 }
26
27 impl App<AssetId> for BugBasherGame {
28     fn start(&mut self, _audio: &mut Audio<AssetId>) {
29     }
30
31     fn advance(&mut self, seconds: f64, _audio: &mut Audio<AssetId>) -> bool {
32         self.bugs.retain(|b| b.alive);
33         for bug in self.bugs.iter_mut() {
34             bug.advance(seconds);
35             if self.home.touches_circle(bug) {
36                 bug.alive = false;
37                 self.points -= 1;
38             }
39         }
40         
41         true
42     }
43
44     fn input(&mut self, evt: InputEvent, _audio: &mut Audio<AssetId>) -> bool {
45         match evt {
46             InputEvent::MousePressed(MouseButton::Left, x, y) => {
47                 for bug in self.bugs.iter_mut().filter(|bug| bug.touches_point(Vec2d { x, y })) {
48                     bug.alive = false;
49                     self.points += 1;
50                 }
51             },
52             _ => {}
53         }
54         true
55     }
56
57     fn render(&mut self, renderer: &mut Renderer<AssetId>) {
58         let (app_width, app_height) = (renderer.app_width(), renderer.app_height());
59         renderer.clear((255,255,255));
60         {
61             let mut renderer = renderer.tiled_mode(app_width / 2. - 50., -app_height / 2. + 50.);
62             let spacing = 25.;
63             let point_str = format!("{}", self.points);
64             for (i, c) in point_str.chars().enumerate() {
65                 let affine = Affine::translate(i as f64 * spacing, 0.);
66                 let tile = match c {
67                     '-' => TileId::NumberFontR0C0,
68                     '0' => TileId::NumberFontR0C1,
69                     '1' => TileId::NumberFontR0C2,
70                     '2' => TileId::NumberFontR0C3,
71                     '3' => TileId::NumberFontR0C4,
72                     '4' => TileId::NumberFontR0C5,
73                     '5' => TileId::NumberFontR0C6,
74                     '6' => TileId::NumberFontR0C7,
75                     '7' => TileId::NumberFontR0C8,
76                     '8' => TileId::NumberFontR0C9,
77                     '9' => TileId::NumberFontR0C10,
78                     _ => TileId::NumberFontR0C0,
79                 };
80                 renderer.draw(&affine, tile);
81             };
82             
83         }
84         {
85             let mut renderer = renderer.sprite_mode();
86             renderer.draw(
87                 &Affine::translate(self.home.pos.x, self.home.pos.y),
88                 SpriteId::Home
89             );
90             for bug in &self.bugs {
91                 renderer.draw(
92                     &Affine::translate(bug.pos.x, bug.pos.y).pre_rotate(bug.rotation),
93                     SpriteId::Bug
94                 );
95             }
96         }
97     }
98 }
99
100 impl BugBasherGame {
101 }
102
103 fn main() {
104     let info = AppInfo::with_app_height(1000.).title("Bug Basher").build();
105     gate::run(info, BugBasherGame {
106         home: Home::new(0., 0.),
107         bugs: vec!(
108             Bug::new(500., 200., 0.3),
109             Bug::new(-200., -200., 1.5),
110             Bug::new(-1000., 200., 0.),
111             Bug::new(1200., 0., 0.3),
112             Bug::new(-1300., 0., 1.5),
113             Bug::new(0., 1100., 0.),
114             Bug::new(0., -1500., 0.3),
115             Bug::new(300., -1200., 1.5),
116         ),
117         points: 0
118     });
119 }