Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293use crate::prelude::*;
#[derive(Clone, Copy, PartialEq, Eq)]pub enum TileType { Floor, Wall,}
impl Into<Glyph> for TileType { fn into(self) -> Glyph { match self { TileType::Floor => Glyph { char: 9, background: Color::BLACK, foreground: color::GRAY_900.into(), }, TileType::Wall => Glyph { char: '#' as u8, background: Color::BLACK, foreground: color::GREEN_500.into(), }, } }}
#[derive(Resource)]pub struct Map { width: usize, height: usize, tiles: Vec<TileType>,}
impl Map { pub fn new_empty(width: usize, height: usize) -> Self { Map { width, height, tiles: vec![TileType::Floor; width * height], } }
pub fn new_random(width: usize, height: usize) -> Self { let mut map = Map::new_empty(width, height);
// Boundaries for x in 0..width { let idx = map.idx(x as i32, 0); map.tiles[idx] = TileType::Wall; let idx = map.idx(x as i32, height as i32 - 1); map.tiles[idx] = TileType::Wall; }
for y in 0..height { let idx = map.idx(0, y as i32); map.tiles[idx] = TileType::Wall; let idx = map.idx(width as i32 - 1, y as i32); map.tiles[idx] = TileType::Wall; }
// Random walls for _ in 0..(width * height / 10) { let x = rand::random_range(1..width) as i32; let y = rand::random_range(1..height) as i32; let idx = map.idx(x, y); if idx != map.idx(40, 25) { map.tiles[idx] = TileType::Wall; } }
map }
pub fn iter_idx(&self) -> impl Iterator<Item = (i32, i32, TileType)> { let mut x = 0; let mut y = 0; self.tiles.iter().map(move |tile| { let item = (x, y, *tile);
x += 1; if x >= self.width as i32 { x = 0; y += 1; }
item }) }
fn idx(&self, x: i32, y: i32) -> usize { (y as usize * self.width) + x as usize }}