dungeon-game/src/game.rs

118 lines
3.5 KiB
Rust
Raw Normal View History

2021-12-28 20:12:45 -06:00
use std::fmt::Display;
2021-12-18 12:22:46 -06:00
use pancurses::Window;
use crate::rooms;
2021-12-18 12:29:07 -06:00
/// A dungeon root.
2021-12-18 12:22:46 -06:00
pub struct Dungeon {
main_branch: DungeonBranch,
}
2021-12-18 12:29:07 -06:00
/// A single branch of a dungeon, which has a number of levels and
/// which can potentially contain passages to other branches.
2021-12-18 12:22:46 -06:00
pub struct DungeonBranch {
config: BranchConfig,
levels: Vec<DungeonLevel>,
}
2021-12-18 12:29:07 -06:00
/// The parameters that characterize a particular dungeon branch.
/// Currently a unit struct because there's only one type of branch,
/// but will later include e.g. architectural styles, good vs. evil &
/// lawful vs. chaotic weights, etc.
2021-12-18 12:22:46 -06:00
pub struct BranchConfig;
2021-12-18 12:29:07 -06:00
/// The size of a dungeon level, in tiles.
2021-12-18 12:22:46 -06:00
pub const LEVEL_SIZE: (usize, usize) = (80, 24);
2021-12-18 12:29:07 -06:00
/// A single level of the dungeon.
2021-12-18 12:22:46 -06:00
pub struct DungeonLevel {
2021-12-18 12:29:07 -06:00
/// The tiles at every position in the level.
2021-12-18 13:06:00 -06:00
tiles: [[DungeonTile; LEVEL_SIZE.0]; LEVEL_SIZE.1],
2021-12-18 12:22:46 -06:00
}
/// The smallest measurable independent location in the dungeon,
2021-12-18 12:29:07 -06:00
/// corresponding to a single character on the screen.
2021-12-28 20:12:45 -06:00
#[derive(Debug, Clone, Copy, PartialEq)]
2021-12-18 12:22:46 -06:00
pub enum DungeonTile {
Floor,
Wall,
Hallway,
}
impl DungeonLevel {
2021-12-18 12:29:07 -06:00
/// Creates a new level in a branch that has the given
/// configuration.
2021-12-28 20:12:45 -06:00
pub fn new(_cfg: &BranchConfig) -> Self {
2021-12-18 13:06:00 -06:00
Self {
2021-12-28 20:12:45 -06:00
tiles: rooms::generate_level(100, &mut rand::thread_rng()),
2021-12-18 13:06:00 -06:00
}
2021-12-18 12:22:46 -06:00
}
2021-12-18 12:29:07 -06:00
/// Draws a level on the display window.
2021-12-18 12:22:46 -06:00
pub fn draw(&self, win: &Window) {
2021-12-28 20:54:01 -06:00
for y in 0..LEVEL_SIZE.1 {
2021-12-18 13:06:00 -06:00
win.mv(y as _, 0);
2021-12-28 20:54:01 -06:00
for x in 0..LEVEL_SIZE.0 {
win.addch(self.render_tile(x, y));
2021-12-18 13:06:00 -06:00
}
}
2021-12-18 12:22:46 -06:00
}
2021-12-28 20:54:01 -06:00
/// Renders the tile at the given coordinates.
pub fn render_tile(&self, x: usize, y: usize) -> char {
match self.tiles[y][x] {
DungeonTile::Floor => '.',
DungeonTile::Wall => {
// Don't render walls with no adjacent floor space, to
// keep the screen clear. Aside from that, walls are
// '-' by default, unless the only adjacent floor is
// to the east or west, in which case they are '|'.
let neighborhood = (-1..=1)
.flat_map(|row| (-1..=1).map(move |col| (col, row)))
.filter(|&(dx, dy)| !(dx == 0 && dy == 0))
.filter_map(|(dx, dy)| {
let (x, y) = (
usize::try_from(x as isize + dx).ok()?,
usize::try_from(y as isize + dy).ok()?,
);
Some((x, y, self.tiles.get(y)?.get(x)?))
})
.collect::<Vec<(usize, usize, &DungeonTile)>>();
if neighborhood
.iter()
.all(|(_x, _y, tile)| *tile != &DungeonTile::Floor)
{
' '
} else {
if neighborhood
.iter()
.any(|(tile_x, _y, tile)| *tile_x == x && *tile == &DungeonTile::Floor)
{
'-'
} else {
'|'
}
}
}
DungeonTile::Hallway => '#',
}
}
2021-12-18 12:22:46 -06:00
}
2021-12-28 20:12:45 -06:00
impl Display for DungeonLevel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2021-12-28 20:54:01 -06:00
for y in 0..LEVEL_SIZE.1 {
for x in 0..LEVEL_SIZE.0 {
2022-01-01 14:57:33 -06:00
write!(f, "{}", self.render_tile(x, y))?;
2021-12-28 20:12:45 -06:00
}
write!(f, "\n")?;
}
Ok(())
}
}