dungeon-game/src/game.rs

91 lines
2.4 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-18 13:06:00 -06:00
for (y, row) in self.tiles.iter().enumerate() {
win.mv(y as _, 0);
for tile in row {
win.addch(match tile {
DungeonTile::Floor => '.',
DungeonTile::Wall => ' ',
DungeonTile::Hallway => '#',
});
}
}
// Leave the cursor at the lower-left.
2021-12-18 13:06:00 -06:00
win.mv(0, 0);
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 {
for row in self.tiles.iter() {
for tile in row {
write!(
f,
"{}",
match tile {
DungeonTile::Floor => '.',
DungeonTile::Wall => ' ',
DungeonTile::Hallway => '#',
}
)?;
}
write!(f, "\n")?;
}
Ok(())
}
}