Added lives string
authorJustin Worthe <justin@worthe-it.co.za>
Thu, 12 Apr 2018 18:58:37 +0000 (20:58 +0200)
committerJustin Worthe <justin@worthe-it.co.za>
Thu, 12 Apr 2018 18:58:37 +0000 (20:58 +0200)
src/main.rs

index 9ba7474..daa36c8 100644 (file)
@@ -21,7 +21,9 @@ use entities::home::Home;
 struct BugBasherGame {
     bugs: Vec<Bug>,
     home: Home,
-    points: i64
+    points: i64,
+    lives: i64,
+    game_over: bool
 }
 
 impl App<AssetId> for BugBasherGame {
@@ -34,7 +36,7 @@ impl App<AssetId> for BugBasherGame {
             bug.advance(seconds);
             if self.home.touches_circle(bug) {
                 bug.alive = false;
-                self.points -= 1;
+                self.lives -= 1;
             }
         }
         
@@ -58,28 +60,10 @@ impl App<AssetId> for BugBasherGame {
         let (app_width, app_height) = (renderer.app_width(), renderer.app_height());
         renderer.clear((255,255,255));
         {
-            let mut renderer = renderer.tiled_mode(app_width / 2. - 50., -app_height / 2. + 50.);
-            let spacing = 25.;
-            let point_str = format!("{}", self.points);
-            for (i, c) in point_str.chars().enumerate() {
-                let affine = Affine::translate(i as f64 * spacing, 0.);
-                let tile = match c {
-                    '-' => TileId::NumberFontR0C0,
-                    '0' => TileId::NumberFontR0C1,
-                    '1' => TileId::NumberFontR0C2,
-                    '2' => TileId::NumberFontR0C3,
-                    '3' => TileId::NumberFontR0C4,
-                    '4' => TileId::NumberFontR0C5,
-                    '5' => TileId::NumberFontR0C6,
-                    '6' => TileId::NumberFontR0C7,
-                    '7' => TileId::NumberFontR0C8,
-                    '8' => TileId::NumberFontR0C9,
-                    '9' => TileId::NumberFontR0C10,
-                    _ => TileId::NumberFontR0C0,
-                };
-                renderer.draw(&affine, tile);
-            };
-            
+            let points_str = format!("{}", self.points);
+            let lives_str = format!("{}", self.lives);
+            BugBasherGame::print_string(renderer, &points_str, - app_width / 2. + 50., app_height / 2. - 50.);
+            BugBasherGame::print_string(renderer, &lives_str, app_width / 2. - 50. - lives_str.len() as f64 * 25., app_height / 2. - 50.);
         }
         {
             let mut renderer = renderer.sprite_mode();
@@ -98,22 +82,49 @@ impl App<AssetId> for BugBasherGame {
 }
 
 impl BugBasherGame {
+    fn new() -> BugBasherGame {
+        let mut game = BugBasherGame {
+            home: Home::new(0., 0.),
+            bugs: Vec::new(),
+            points: 0,
+            lives: 0,
+            game_over: true
+        };
+        game.reset();
+        game
+    }
+
+    fn reset(&mut self) {
+        self.points = 0;
+        self.lives = 3;
+        self.game_over = false;
+    }
+
+    fn print_string(renderer: &mut Renderer<AssetId>, string: &str, x: f64, y: f64) {
+        let mut renderer = renderer.tiled_mode(-x, -y);
+        let spacing = 25.;
+        for (i, c) in string.chars().enumerate() {
+            let affine = Affine::translate(i as f64 * spacing, 0.);
+            let tile = match c {
+                '-' => TileId::NumberFontR0C0,
+                '0' => TileId::NumberFontR0C1,
+                '1' => TileId::NumberFontR0C2,
+                '2' => TileId::NumberFontR0C3,
+                '3' => TileId::NumberFontR0C4,
+                '4' => TileId::NumberFontR0C5,
+                '5' => TileId::NumberFontR0C6,
+                '6' => TileId::NumberFontR0C7,
+                '7' => TileId::NumberFontR0C8,
+                '8' => TileId::NumberFontR0C9,
+                '9' => TileId::NumberFontR0C10,
+                _ => TileId::NumberFontR0C0,
+            };
+            renderer.draw(&affine, tile);
+        };
+    }
 }
 
 fn main() {
     let info = AppInfo::with_app_height(1000.).title("Bug Basher").build();
-    gate::run(info, BugBasherGame {
-        home: Home::new(0., 0.),
-        bugs: vec!(
-            Bug::new(500., 200., 0.3),
-            Bug::new(-200., -200., 1.5),
-            Bug::new(-1000., 200., 0.),
-            Bug::new(1200., 0., 0.3),
-            Bug::new(-1300., 0., 1.5),
-            Bug::new(0., 1100., 0.),
-            Bug::new(0., -1500., 0.3),
-            Bug::new(300., -1200., 1.5),
-        ),
-        points: 0
-    });
+    gate::run(info, BugBasherGame::new());
 }