Render room corners as +

This commit is contained in:
Alex Bethel 2022-01-06 14:32:08 -06:00
parent 72ed107d17
commit 411b8091f9

View file

@ -64,35 +64,39 @@ impl DungeonLevel {
match self.tiles[y][x] { match self.tiles[y][x] {
DungeonTile::Floor => '.', DungeonTile::Floor => '.',
DungeonTile::Wall => { DungeonTile::Wall => {
// Don't render walls with no adjacent floor space, to // Walls are rendered like so:
// keep the screen clear. Aside from that, walls are // - If the wall has any floor tiles to its north or
// '-' by default, unless the only adjacent floor is // south, then it is rendered as '-', because it is
// to the east or west, in which case they are '|'. // the north or south wall of a room.
// - Otherwise, if the wall has any floor tiles to its
// east or west, then it is rendered as '|'.
// - Otherwise, if any floor tiles are diagonally
// adjacent to the wall, then the wall is rendered as
// '+', because it is in the corner of a room.
// - Otherwise, no floor tiles are adjacent to the
// wall, therefore it is surrounded by stone and will
// never be discovered by the player, so we don't
// render it at all.
let neighborhood = (-1..=1) let has_floor = |deltas: &[(i32, i32)]| -> bool {
.flat_map(|row| (-1..=1).map(move |col| (col, row))) deltas
.filter(|&(dx, dy)| !(dx == 0 && dy == 0)) .iter()
.filter_map(|(dx, dy)| { .map(|(dx, dy)| (x as i32 + dx, y as i32 + dy))
let (x, y) = ( .filter(|(x, y)| {
usize::try_from(x as isize + dx).ok()?, (0..LEVEL_SIZE.0 as i32).contains(x)
usize::try_from(y as isize + dy).ok()?, && (0..LEVEL_SIZE.1 as i32).contains(y)
);
Some((x, y, self.tiles.get(y)?.get(x)?))
}) })
.collect::<Vec<(usize, usize, &DungeonTile)>>(); .any(|(x, y)| self.tile(x, y) == &DungeonTile::Floor)
};
if neighborhood if has_floor(&[(0, -1), (0, 1)]) {
.iter()
.all(|(_x, _y, tile)| *tile != &DungeonTile::Floor)
{
' '
} else if neighborhood
.iter()
.any(|(tile_x, _y, tile)| *tile_x == x && *tile == &DungeonTile::Floor)
{
'-' '-'
} else { } else if has_floor(&[(-1, 0), (1, 0)]) {
'|' '|'
} else if has_floor(&[(-1, -1), (-1, 1), (1, -1), (1, 1)]) {
'+'
} else {
' '
} }
} }
DungeonTile::Hallway => '#', DungeonTile::Hallway => '#',