Changed hitboxed from an 'is a hitbox' to a 'has a hitbox'
[bug-basher.git] / src / hitbox.rs
1 use geometry::*;
2
3 pub enum Hitbox {
4     Circle(CircleHitbox),
5     Point(PointHitbox)
6 }
7
8 pub struct CircleHitbox {
9     pub pos: Vec2d,
10     pub radius: f64
11 }
12
13 pub struct PointHitbox {
14     pub pos: Vec2d
15 }
16
17 impl Hitbox {
18     pub fn intersect(a: &Hitbox, b: &Hitbox) -> bool {
19         use self::Hitbox::*;
20
21         match (a, b) {
22             (Circle(x), Circle(y)) => circles_intersect(x, y),
23             (Circle(x), Point(y)) => circle_point_intersect(x, y),
24             (Point(x), Circle(y)) => circle_point_intersect(y, x),
25             (Point(x), Point(y)) => points_intersect(x, y)
26         }
27     }
28 }
29
30 fn circles_intersect(x: &CircleHitbox, y: &CircleHitbox) -> bool {
31     x.pos.distance_squared(y.pos) <= (x.radius + y.radius).powi(2)
32 }
33
34 fn circle_point_intersect(x: &CircleHitbox, y: &PointHitbox) -> bool {
35     x.pos.distance_squared(y.pos) <= x.radius.powi(2)
36 }
37
38 fn points_intersect(x: &PointHitbox, y: &PointHitbox) -> bool {
39     x.pos == y.pos
40 }