Added hitboxes and something for the bugs to run into
authorJustin Worthe <justin@worthe-it.co.za>
Mon, 2 Apr 2018 13:36:01 +0000 (15:36 +0200)
committerJustin Worthe <justin@worthe-it.co.za>
Mon, 2 Apr 2018 13:36:01 +0000 (15:36 +0200)
src/entities/bug.rs
src/entities/home.rs [new file with mode: 0644]
src/entities/mod.rs
src/hitbox.rs [new file with mode: 0644]
src/main.rs
src_assets/sprites/Home.png [new file with mode: 0644]
src_assets/sprites/Home.svg [new file with mode: 0644]

index 424e195..482e5c4 100644 (file)
@@ -1,5 +1,7 @@
 use geometry::*;
+use hitbox::*;
 
+#[derive(Debug, Clone)]
 pub struct Bug {
     pub pos: Vec2d,
     pub rotation: f64,
@@ -29,14 +31,13 @@ impl Bug {
         };
         self.pos = self.pos + delta_pos;
     }
+}
 
-    pub fn click(&mut self, point: Vec2d) {
-        if self.touches(point) {
-            self.alive = false;
-        }
+impl CircleHitbox for Bug {
+    fn pos(&self) -> Vec2d {
+        self.pos
     }
-
-    fn touches(&self, point: Vec2d) -> bool {
-        self.pos.distance(point) <= 75. // Some better hit box modelling might be nice?
+    fn radius(&self) -> f64 {
+        75.
     }
 }
diff --git a/src/entities/home.rs b/src/entities/home.rs
new file mode 100644 (file)
index 0000000..6f7f53d
--- /dev/null
@@ -0,0 +1,27 @@
+use geometry::*;
+use hitbox::*;
+
+#[derive(Debug, Clone)]
+pub struct Home {
+    pub pos: Vec2d
+}
+
+impl Home {
+    pub fn new(x: f64, y: f64) -> Home {
+        Home {
+            pos: Vec2d {
+                x: x,
+                y: y
+            }
+        }
+    }
+}
+
+impl CircleHitbox for Home {
+    fn pos(&self) -> Vec2d {
+        self.pos
+    }
+    fn radius(&self) -> f64 {
+        100.
+    }
+}
index 0f4281c..90b4a11 100644 (file)
@@ -1 +1,2 @@
 pub mod bug;
+pub mod home;
diff --git a/src/hitbox.rs b/src/hitbox.rs
new file mode 100644 (file)
index 0000000..df87b68
--- /dev/null
@@ -0,0 +1,14 @@
+use geometry::*;
+
+pub trait CircleHitbox {
+    fn pos(&self) -> Vec2d;
+    fn radius(&self) -> f64;
+    
+    fn touches_point(&self, point: Vec2d) -> bool {
+        self.pos().distance(point) <= self.radius()
+    }
+
+    fn touches_circle(&self, other: &CircleHitbox) -> bool {
+        self.pos().distance(other.pos()) <= self.radius() + other.radius()
+    }
+}
index ff8b64f..9ba7474 100644 (file)
@@ -11,11 +11,16 @@ use asset_id::*;
 mod geometry;
 use geometry::*;
 
+mod hitbox;
+use hitbox::*;
+
 mod entities;
 use entities::bug::Bug;
+use entities::home::Home;
 
 struct BugBasherGame {
     bugs: Vec<Bug>,
+    home: Home,
     points: i64
 }
 
@@ -27,6 +32,10 @@ impl App<AssetId> for BugBasherGame {
         self.bugs.retain(|b| b.alive);
         for bug in self.bugs.iter_mut() {
             bug.advance(seconds);
+            if self.home.touches_circle(bug) {
+                bug.alive = false;
+                self.points -= 1;
+            }
         }
         
         true
@@ -35,8 +44,9 @@ impl App<AssetId> for BugBasherGame {
     fn input(&mut self, evt: InputEvent, _audio: &mut Audio<AssetId>) -> bool {
         match evt {
             InputEvent::MousePressed(MouseButton::Left, x, y) => {
-                for bug in self.bugs.iter_mut() {
-                    bug.click(Vec2d { x, y });
+                for bug in self.bugs.iter_mut().filter(|bug| bug.touches_point(Vec2d { x, y })) {
+                    bug.alive = false;
+                    self.points += 1;
                 }
             },
             _ => {}
@@ -73,6 +83,10 @@ impl App<AssetId> for BugBasherGame {
         }
         {
             let mut renderer = renderer.sprite_mode();
+            renderer.draw(
+                &Affine::translate(self.home.pos.x, self.home.pos.y),
+                SpriteId::Home
+            );
             for bug in &self.bugs {
                 renderer.draw(
                     &Affine::translate(bug.pos.x, bug.pos.y).pre_rotate(bug.rotation),
@@ -89,6 +103,7 @@ impl BugBasherGame {
 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),
diff --git a/src_assets/sprites/Home.png b/src_assets/sprites/Home.png
new file mode 100644 (file)
index 0000000..2b4c2c7
Binary files /dev/null and b/src_assets/sprites/Home.png differ
diff --git a/src_assets/sprites/Home.svg b/src_assets/sprites/Home.svg
new file mode 100644 (file)
index 0000000..e26e9ad
--- /dev/null
@@ -0,0 +1,79 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!-- Created with Inkscape (http://www.inkscape.org/) -->
+
+<svg
+   xmlns:dc="http://purl.org/dc/elements/1.1/"
+   xmlns:cc="http://creativecommons.org/ns#"
+   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+   xmlns:svg="http://www.w3.org/2000/svg"
+   xmlns="http://www.w3.org/2000/svg"
+   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
+   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
+   width="200"
+   height="200"
+   viewBox="0 0 52.916665 52.916668"
+   version="1.1"
+   id="svg4589"
+   inkscape:version="0.92.2 2405546, 2018-03-11"
+   sodipodi:docname="Home.svg"
+   inkscape:export-filename="/home/justin/proj/bug_basher/src_assets/sprites/Home.png"
+   inkscape:export-xdpi="96"
+   inkscape:export-ydpi="96">
+  <defs
+     id="defs4583" />
+  <sodipodi:namedview
+     id="base"
+     pagecolor="#ffffff"
+     bordercolor="#666666"
+     borderopacity="1.0"
+     inkscape:pageopacity="0.0"
+     inkscape:pageshadow="2"
+     inkscape:zoom="2.8"
+     inkscape:cx="64.613287"
+     inkscape:cy="105.34332"
+     inkscape:document-units="mm"
+     inkscape:current-layer="layer1"
+     showgrid="false"
+     inkscape:window-width="1916"
+     inkscape:window-height="1055"
+     inkscape:window-x="0"
+     inkscape:window-y="21"
+     inkscape:window-maximized="0"
+     units="px" />
+  <metadata
+     id="metadata4586">
+    <rdf:RDF>
+      <cc:Work
+         rdf:about="">
+        <dc:format>image/svg+xml</dc:format>
+        <dc:type
+           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
+        <dc:title></dc:title>
+      </cc:Work>
+    </rdf:RDF>
+  </metadata>
+  <g
+     inkscape:label="Layer 1"
+     inkscape:groupmode="layer"
+     id="layer1"
+     transform="translate(0,-244.08332)">
+    <circle
+       style="opacity:1;fill:#00a18a;fill-opacity:1;stroke:#000000;stroke-width:0.79374999;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
+       id="path5138"
+       cx="26.458334"
+       cy="270.54166"
+       r="26.061459" />
+    <path
+       style="opacity:1;fill:#000000;fill-opacity:1;stroke:#000000;stroke-width:0.79374999;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
+       d="m 15.875,267.42327 h 21.166667 v 24.56842 H 15.875 Z"
+       id="rect5144"
+       inkscape:connector-curvature="0"
+       sodipodi:nodetypes="ccccc" />
+    <path
+       style="fill:#000000;stroke:#000000;stroke-width:0.79374999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;fill-opacity:1"
+       d="m 16.630951,267.42334 -11.3392843,0.0945 21.1666663,-15.02447 21.166667,14.9299 -10.866819,7e-5"
+       id="path5146"
+       inkscape:connector-curvature="0"
+       sodipodi:nodetypes="ccccc" />
+  </g>
+</svg>