rpgtools/map/gridmap/
gridcell.rs

1#[derive(Clone, Debug, PartialEq)]
2pub enum AreaType {
3    Nothing,
4    Entrance,
5    Room,
6    //    Stairs,
7    //    Tested,
8}
9
10#[derive(Clone, Debug)]
11pub enum WallType {
12    Nothing,
13    //    Wall,
14    //    Door,
15    //    SecretDoor,
16}
17
18#[derive(Clone, Debug)]
19pub enum PointType {
20    Nothing,
21    //    Pillar,
22}
23
24/// Representation of a GridCell, which is a single unit in a grid.
25#[derive(Clone, Debug)]
26pub struct GridCell {
27    /// The type of area contained within this sell
28    pub area: AreaType,
29    vert_wall: WallType,
30    horiz_wall: WallType,
31    point: PointType,
32}
33
34impl GridCell {
35    /// Construct a new GridCell
36    pub fn new() -> GridCell {
37        GridCell {
38            area: AreaType::Nothing,
39            vert_wall: WallType::Nothing,
40            horiz_wall: WallType::Nothing,
41            point: PointType::Nothing,
42        }
43    }
44
45    /// Check if a GridCell is 'empty', meaning that all drawing-related
46    /// fields have a value of 'Nothing'.
47    pub fn is_empty(&self) -> bool {
48        matches!(
49            *self,
50            GridCell {
51                area: AreaType::Nothing,
52                vert_wall: WallType::Nothing,
53                horiz_wall: WallType::Nothing,
54                point: PointType::Nothing,
55                ..
56            }
57        )
58    }
59
60    //    /// Check whether the GridCell represents a dungeon entrance
61    //    pub fn is_entrance(&self) -> bool {
62    //        match self {
63    //            &GridCell { area: AreaType::Entrance, ..} => true,
64    //            _ => false
65    //        }
66    //    }
67
68    /// Check whether the GridCell is part of a dungeon room
69    pub fn is_room(&self) -> bool {
70        !matches!(
71            *self,
72            GridCell {
73                area: AreaType::Nothing,
74                ..
75            }
76        )
77    }
78
79    /// Get the type of area that this cell represents
80    pub fn area(&self) -> &AreaType {
81        &self.area
82    }
83
84    /// Set the type of area that this cell represents
85    pub fn set_area(&mut self, area: AreaType) {
86        self.area = area;
87    }
88}
89
90impl Default for GridCell {
91    fn default() -> Self {
92        Self::new()
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99
100    #[test]
101    fn new() {
102        let cell = GridCell::new();
103        assert_eq!(AreaType::Nothing, cell.area);
104    }
105
106    #[test]
107    fn is_empty() {
108        let mut cell = GridCell::new();
109        // Do checks
110        cell.area = AreaType::Nothing;
111        assert_eq!(true, cell.is_empty());
112        cell.area = AreaType::Entrance;
113        assert_eq!(false, cell.is_empty());
114        cell.area = AreaType::Room;
115        assert_eq!(false, cell.is_empty());
116    }
117
118    #[test]
119    fn is_room() {
120        let mut cell = GridCell::new();
121        // Do checks
122        cell.area = AreaType::Nothing;
123        assert_eq!(false, cell.is_room());
124        cell.area = AreaType::Entrance;
125        assert_eq!(true, cell.is_room());
126        cell.area = AreaType::Room;
127        assert_eq!(true, cell.is_room());
128    }
129}