This commit is contained in:
unintendedfraud
2024-01-27 22:47:22 +01:00
parent 227c0021fe
commit 6e7ab861c9
5 changed files with 95 additions and 54 deletions
+57
View File
@@ -0,0 +1,57 @@
pub use crate::prelude::*;
const NUM_TILES: usize = (SCREEN_WIDTH * SCREEN_HEIGHT) as usize;
#[derive(Copy, Clone, PartialEq)]
pub enum TileType {
Floor,
Wall,
}
pub struct Map {
pub tiles: Vec<TileType>,
}
pub fn map_idx(x: i32, y: i32) -> usize {
return ((y * SCREEN_WIDTH) + x) as usize;
}
impl Map {
pub fn new() -> Self {
return Self {
tiles: vec![TileType::Floor; NUM_TILES],
}
}
pub fn in_bounds(&self, point: Point) -> bool {
return point.x >= 0 && point.x < SCREEN_WIDTH && point.y >= 0 && point.y < SCREEN_HEIGHT;
}
pub fn can_enter_tile(&self, point: Point) -> bool {
return self.in_bounds(point) && self.tiles[map_idx(point.x, point.y)] == TileType::Floor;
}
pub fn try_idx(&self, point: Point) -> Option<usize> {
if self.in_bounds(point) {
return Some(map_idx(point.x, point.y));
} else {
return None;
}
}
pub fn render(&self, ctx: &mut BTerm) {
for y in 0..SCREEN_HEIGHT {
for x in 0..SCREEN_WIDTH {
let idx = map_idx(x, y);
match self.tiles[idx] {
TileType::Floor => {
ctx.set(x, y, YELLOW, BLACK, to_cp437('.'));
}
TileType::Wall => {
ctx.set(x, y, GREEN, BLACK, to_cp437('#'));
}
}
}
}
}
}