summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/entities/bug.rs15
-rw-r--r--src/entities/home.rs27
-rw-r--r--src/entities/mod.rs1
-rw-r--r--src/hitbox.rs14
-rw-r--r--src/main.rs19
5 files changed, 67 insertions, 9 deletions
diff --git a/src/entities/bug.rs b/src/entities/bug.rs
index 424e195..482e5c4 100644
--- a/src/entities/bug.rs
+++ b/src/entities/bug.rs
@@ -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
index 0000000..6f7f53d
--- /dev/null
+++ b/src/entities/home.rs
@@ -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.
+ }
+}
diff --git a/src/entities/mod.rs b/src/entities/mod.rs
index 0f4281c..90b4a11 100644
--- a/src/entities/mod.rs
+++ b/src/entities/mod.rs
@@ -1 +1,2 @@
pub mod bug;
+pub mod home;
diff --git a/src/hitbox.rs b/src/hitbox.rs
new file mode 100644
index 0000000..df87b68
--- /dev/null
+++ b/src/hitbox.rs
@@ -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()
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index ff8b64f..9ba7474 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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),