summaryrefslogtreecommitdiffstats
path: root/src/structure/building.rs
blob: 55961dd125e3afaf8aa73b9bd45d531dfcd5201a (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::collections::HashMap;

use crate::{
    physics::{Collider, Point},
    Render,
};

#[derive(Debug, PartialEq, Eq)]
pub struct Building {
    pub position: Point,
    pub width: u16,
    pub height: u16,
    render_map: HashMap<Point, char>,
}

impl Building {
    pub fn builder() -> BuildingBuilder {
        BuildingBuilder::default()
    }
}

#[derive(Debug, Default)]
pub struct BuildingBuilder {
    position: Option<Point>,
    width: Option<u16>,
    height: Option<u16>,
}

impl BuildingBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn position<P: Into<Point>>(mut self, position: P) -> BuildingBuilder {
        self.position = Some(position.into());
        self
    }

    pub fn width(mut self, width: u16) -> BuildingBuilder {
        self.width = Some(width);
        self
    }

    pub fn height(mut self, height: u16) -> BuildingBuilder {
        self.height = Some(height);
        self
    }

    pub fn build(self) -> Building {
        let position = self.position.unwrap_or_default();
        let width = self.width.unwrap_or_default();
        let height = self.height.unwrap_or_default();
        let mut render_map = HashMap::new();

        render_map.insert(position, '┌');
        render_map.insert((position.x, height).into(), '└');
        render_map.insert((width, position.y).into(), '┐');
        render_map.insert((width, height).into(), '┘');

        for x in position.x + 1..width {
            render_map.insert((x, position.y).into(), '─');
            render_map.insert((x, height).into(), '─');
        }

        for y in position.y + 1..height {
            render_map.insert((position.x, y).into(), '│');
            render_map.insert((width, y).into(), '│');
        }

        Building {
            position,
            width,
            height,
            render_map,
        }
    }
}

impl Render for Building {
    fn render_map(&self) -> &std::collections::HashMap<crate::physics::Point, char> {
        &self.render_map
    }
}

impl Collider for Building {
    fn collision_map(&self) -> &Vec<Point> {
        let collision_map = Vec::new();
        for x in self.position.x..self.width {
            collision_map.push((x, self.position.y).into());
            collision_map.push((x, self.height).into());
        }
        for y in self.position.y..self.height {
            collision_map.push((self.position.x, y).into());
            collision_map.push((self.width, y).into());
        }
        &collision_map
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use super::*;
    use crate::physics::Point;

    #[test]
    fn building_builder() {
        let position = Point { x: 1, y: 1 };
        let width = 3u16;
        let height = 3u16;
        let render_map = HashMap::from([
            (position, '┌'),
            ((position.x, height).into(), '└'),
            ((width, position.y).into(), '┐'),
            ((width, height).into(), '┘'),
            ((position.x + 1, position.y).into(), '─'),
            ((position.x + 1, height).into(), '─'),
            ((position.x, position.y + 1).into(), '│'),
            ((width, position.y + 1).into(), '│'),
        ]);

        let building = Building {
            position,
            width,
            height,
            render_map,
        };

        let building_from_builder = BuildingBuilder::new()
            .width(width)
            .height(height)
            .position(position)
            .build();

        assert_eq!(building, building_from_builder);
    }
}