1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use crate::geometry::{self, Location};

// Design decision: avoid embedding methods within entities --
// we will go with a very poor version of ECS pattern (entity component system)
#[derive(Debug, Copy, Clone)]
pub enum GameEntityType {
    Player,
    Enemy,
    Structure,
    Zone,
    Projectile,
}

#[derive(Debug)]
pub struct GameEntity {
    pub location: geometry::Point,
    pub entity_type: GameEntityType,
}

impl Location for GameEntity {
    fn get_center_point(&self) -> geometry::Point {
        self.location
    }
}

impl GameEntity {
    fn can_take_damage(&self) -> bool {
        match self.entity_type {
            GameEntityType::Player => true,
            GameEntityType::Enemy => true,
            GameEntityType::Structure => true,
            GameEntityType::Zone => false,
            GameEntityType::Projectile => false,
        }
    }
}

pub struct Zone {
    bounding_box: geometry::BoundingBox,
}

impl Zone {
    pub fn entity_inside<'a>(&self, entity: &'a GameEntity) -> bool {
        entity.location.inside(self.get_bounding_box())
    }

    pub fn get_bounding_box(&self) -> geometry::BoundingBox {
        self.bounding_box
    }
}

#[cfg(test)]
mod tests {
    use crate::game::entities::*;
    use crate::geometry::{BoundingBox, Point};

    #[test]
    fn entities_tests() {
        let player = GameEntity {
            location: Point::new(5, 5),
            entity_type: GameEntityType::Player,
        };

        let zone = Zone {
            bounding_box: BoundingBox::new(Point::new(0, 0), Point::new(10, 10)),
        };

        assert!(zone.entity_inside(&player));
    }
}