Changed hitboxed from an 'is a hitbox' to a 'has a hitbox'
[bug-basher.git] / src / hitbox.rs
index bd4170e..32f432f 100644 (file)
@@ -1,17 +1,40 @@
 use geometry::*;
 
-pub trait CircleHitbox {
-    fn pos(&self) -> Vec2d;
-    fn radius(&self) -> f64;
-
-    /**
-     * True if the point is inside the hitbox
-     */
-    fn touches_point(&self, point: Vec2d) -> bool {
-        self.pos().distance(point) <= self.radius()
-    }
+pub enum Hitbox {
+    Circle(CircleHitbox),
+    Point(PointHitbox)
+}
+
+pub struct CircleHitbox {
+    pub pos: Vec2d,
+    pub radius: f64
+}
+
+pub struct PointHitbox {
+    pub pos: Vec2d
+}
+
+impl Hitbox {
+    pub fn intersect(a: &Hitbox, b: &Hitbox) -> bool {
+        use self::Hitbox::*;
 
-    fn touches_circle(&self, other: &CircleHitbox) -> bool {
-        self.pos().distance_squared(other.pos()) <= (self.radius() + other.radius()).powi(2)
+        match (a, b) {
+            (Circle(x), Circle(y)) => circles_intersect(x, y),
+            (Circle(x), Point(y)) => circle_point_intersect(x, y),
+            (Point(x), Circle(y)) => circle_point_intersect(y, x),
+            (Point(x), Point(y)) => points_intersect(x, y)
+        }
     }
 }
+
+fn circles_intersect(x: &CircleHitbox, y: &CircleHitbox) -> bool {
+    x.pos.distance_squared(y.pos) <= (x.radius + y.radius).powi(2)
+}
+
+fn circle_point_intersect(x: &CircleHitbox, y: &PointHitbox) -> bool {
+    x.pos.distance_squared(y.pos) <= x.radius.powi(2)
+}
+
+fn points_intersect(x: &PointHitbox, y: &PointHitbox) -> bool {
+    x.pos == y.pos
+}