aboutsummaryrefslogtreecommitdiff
path: root/src/tiling.rs
blob: e21c02a050caeb650f626b0ed7c196792d2b78bd (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use std::convert::From;

#[derive(Copy, Clone, Debug)]
pub enum TileType {
    Empty,
    Wall,
    Floor,
    StairsUp,
    StairsDown,
    Character(&'static str),
    Player,
    Door,
}

#[derive(Clone, Debug)]
pub struct Tile {
    tile_type: TileType,
    visible: bool,
    opaque: bool,
    lit: bool,
    open: bool,
}

impl Tile {
    pub fn new(tile_type: TileType, visible: bool, opaque: bool, open: bool, lit: bool) -> Self {
        Tile {
            tile_type,
            visible,
            opaque,
	    lit,
	    open,
        }
    }

    pub fn get_type(&self) -> &TileType {
        &self.tile_type
    }

    pub fn is_visible(&self) -> bool {
        self.visible
    }

    pub fn visibility(&mut self, visible: bool) {
        self.visible = visible;
    }

    pub fn is_lit(&self) -> bool {
	self.lit
    }

    pub fn lit(&mut self, lit: bool) {
	self.lit = lit;
    }

    pub fn is_opaque(&self) -> bool {
        self.opaque
    }

    pub fn opacity(&mut self, opaque: bool) {
        self.opaque = opaque
    }

    pub fn is_open(&self) -> bool {
	self.open
    }

    pub fn open(&mut self) {
	self.open = true;
    }

    pub fn clos(&mut self) {
	self.open = false;
    }
}

impl From<TileType> for Tile {
    fn from(tile_type: TileType) -> Self {
        Tile {
            tile_type,
            visible: false, // <--- TODO: this set the default beaviour
            //            - true: all tiles of world and entities will be drawn
            //            - false: only draw tiles visible for the player
            opaque: match tile_type {
                TileType::Empty => true,
                TileType::Wall => true,
                _ => false,
            },
	    lit: false,
	    open: false,
        }
    }
}

pub struct TileGrid {
    grid: Vec<Vec<Tile>>,
    xsize: usize,
    ysize: usize,
}

impl TileGrid {
    pub fn new(xsize: usize, ysize: usize) -> TileGrid {
        let mut grid = TileGrid {
            grid: Vec::with_capacity(ysize),
            xsize,
            ysize,
        };

        for _ in 0..ysize {
            let mut subvec = Vec::with_capacity(xsize);
            for _ in 0..xsize {
                subvec.push(Tile::from(TileType::Empty));
            }
            grid.grid.push(subvec);
        }

        grid
    }

    pub fn set_tile(&mut self, x: usize, y: usize, tile: Tile) {
        self.grid[y][x] = tile;
    }

    /// Sets a tile if nothing lies underneath it.
    pub fn set_empty_tile(&mut self, x: usize, y: usize, tile: Tile) {
        self.set_tile(
            x,
            y,
            match self.grid[y][x].tile_type {
                TileType::Empty => tile,
                _ => self.grid[y][x].clone(),
            },
        )
    }

    pub fn raw_data(&self) -> &Vec<Vec<Tile>> {
        &self.grid
    }

    pub fn tile_at(&self, x: usize, y: usize) -> &Tile {
        &self.grid[y][x]
    }

    pub fn block_at(&self, x: usize, y: usize) -> &Tile {
        //Needed to integrate with the terminal numbering
        &self.grid[y + 1][x]
    }

    pub fn xsize(&self) -> usize {
        self.xsize
    }

    pub fn ysize(&self) -> usize {
        self.ysize
    }

    fn reveal(&mut self, x: usize, y: usize) {
        self.grid[y + 1][x].visibility(true);
    }

    fn light(&mut self, x: usize, y: usize) {
	self.grid[y + 1][x].lit(true);
    }

    /// Walk around the perimeter of the line of sight and ray-trace to clear tiles
    /// up to the nearest obstacle.
    pub fn clear_fog_of_war(&mut self, center: &(usize, usize), radius: usize) {
	// Unlight everything first.
	for x in 0..self.xsize {
	    for y in 0..self.ysize {
		self.grid[y][x].lit(false)
	    }
	}

	let start: (usize, usize) = (center.0.saturating_sub(radius), center.1.saturating_sub(radius));
	let end: (usize, usize) = (center.0.saturating_add(radius), center.1.saturating_add(radius));

	println!("From {:?} to {:?}", start, end);

	for x in start.0..=end.0 {
	    for y in start.1..=end.1 {
		self.reveal(x, y);
		self.light(x, y);
	    }
	}
    }
}

pub fn tile_to_str(tile: &Tile) -> &str {
    if tile.is_visible() {
        match tile.tile_type {
            TileType::Floor => match tile.is_lit() {
		true => ".",
		false => " "
	    },
            TileType::Wall => "#",
            TileType::Empty => " ",
            TileType::StairsDown => ">",
            TileType::StairsUp => "<",
            TileType::Player => "@",
            TileType::Character(t) => t,
	    TileType::Door => match tile.is_open() {
		true => "'",
		false => "+",
	    },
        }
    } else {
        " "
    }
}

pub trait Tileable {
    fn tile(&self, grid: &mut TileGrid) -> Result<(), String>;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new_tilegrid_is_populated_by_empty_invisible_tiles() {
        const GRID_SIZE: usize = 2;
        let grid = TileGrid::new(GRID_SIZE, GRID_SIZE);
        for x in 0..2 {
            for y in 0..2 {
                assert!(match grid.tile_at(x, y).tile_type {
                    TileType::Empty => true,
                    _ => false,
                });
                assert_eq!(grid.tile_at(x, y).is_visible(), false);
            }
        }
    }

    #[test]
    fn tiles_can_be_revealed() {
        let mut tile = Tile::from(TileType::Wall);
        assert_eq!(tile.visible, false);
        assert_eq!(tile.is_visible(), false);
        tile.visibility(true);
        assert_eq!(tile.visible, true);
        assert_eq!(tile.is_visible(), true);
    }

    #[test]
    fn tilegrid_can_reveal_tiles() {
        let mut grid = TileGrid::new(1, 1);
        grid.reveal(0, 0);
        assert_eq!(grid.grid[0][0].is_visible(), true);
        assert_eq!(grid.tile_at(0, 0).is_visible(), true);
    }

    #[test]
    fn test_clear_fog_of_war() {
	let mut grid = TileGrid::new(5, 5);
	
	grid.clear_fog_of_war(&(2, 2), 1);

	println!("test");

	for x in 0..5 {
	    for y in 0..5 {
		if grid.tile_at(x, y).is_visible() {
		    print!("x");
		} else {
		    print!(" ");
		}
	    }
	    print!("\n");
	}
	
	assert_eq!(grid.tile_at(0, 0).is_visible(), false);
	assert_eq!(grid.tile_at(1, 0).is_visible(), false);
	assert_eq!(grid.tile_at(2, 0).is_visible(), false);
	assert_eq!(grid.tile_at(3, 0).is_visible(), false);
	assert_eq!(grid.tile_at(4, 0).is_visible(), false);

	assert_eq!(grid.tile_at(0, 1).is_visible(), false);
	assert_eq!(grid.tile_at(1, 1).is_visible(), true);
	assert_eq!(grid.tile_at(2, 1).is_visible(), true);
	assert_eq!(grid.tile_at(3, 1).is_visible(), true);
	assert_eq!(grid.tile_at(4, 1).is_visible(), false);

	assert_eq!(grid.tile_at(0, 2).is_visible(), false);
	assert_eq!(grid.tile_at(1, 2).is_visible(), true);
	assert_eq!(grid.tile_at(2, 2).is_visible(), true);
	assert_eq!(grid.tile_at(3, 2).is_visible(), true);
	assert_eq!(grid.tile_at(4, 2).is_visible(), false);

	assert_eq!(grid.tile_at(0, 3).is_visible(), false);
	assert_eq!(grid.tile_at(1, 3).is_visible(), true);
	assert_eq!(grid.tile_at(2, 3).is_visible(), true);
	assert_eq!(grid.tile_at(3, 3).is_visible(), true);
	assert_eq!(grid.tile_at(4, 3).is_visible(), false);

	assert_eq!(grid.tile_at(0, 4).is_visible(), false);
	assert_eq!(grid.tile_at(1, 4).is_visible(), false);
	assert_eq!(grid.tile_at(2, 4).is_visible(), false);
	assert_eq!(grid.tile_at(3, 4).is_visible(), false);
	assert_eq!(grid.tile_at(4, 4).is_visible(), false);
    }
}