2020-08-31 09:45:59 +00:00
|
|
|
#[derive(PartialEq, Copy, Clone, Eq, Hash, Debug)]
|
|
|
|
pub enum TileType {
|
|
|
|
Wall, Floor
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, Clone)]
|
|
|
|
pub struct Map {
|
|
|
|
pub tiles : Vec<TileType>,
|
2020-08-31 12:13:52 +00:00
|
|
|
pub width : usize,
|
|
|
|
pub height : usize,
|
2020-08-31 09:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Map {
|
|
|
|
|
|
|
|
/// Generates an empty map, consisting entirely of solid walls
|
2020-08-31 12:13:52 +00:00
|
|
|
pub fn new(width: usize, height: usize) -> Map {
|
|
|
|
let map_tile_count = width*height;
|
2020-08-31 09:45:59 +00:00
|
|
|
Map{
|
|
|
|
tiles : vec![TileType::Wall; map_tile_count],
|
|
|
|
width,
|
|
|
|
height
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 20:03:48 +00:00
|
|
|
/// Get TileType at the given location
|
2020-08-31 12:13:52 +00:00
|
|
|
pub fn at(&self, x: usize, y: usize) -> TileType {
|
|
|
|
let idx = y * self.width + x;
|
2020-08-31 09:45:59 +00:00
|
|
|
self.tiles[idx]
|
|
|
|
}
|
|
|
|
|
2020-08-31 20:03:48 +00:00
|
|
|
/// Modify tile at the given location
|
|
|
|
pub fn set_tile(&mut self, x: usize, y: usize, tile: TileType) {
|
|
|
|
let idx = y * self.width + x;
|
|
|
|
self.tiles[idx] = tile;
|
|
|
|
}
|
2020-08-31 09:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ------------------------------------------------------------------------------------------------
|
|
|
|
/// Module unit tests
|
|
|
|
/// ------------------------------------------------------------------------------------------------
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new_map() {
|
|
|
|
let map = Map::new(10, 10);
|
|
|
|
for i in 0..10 {
|
|
|
|
for j in 0..10 {
|
|
|
|
assert_eq!(map.at(i, j), TileType::Wall);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|