diff options
| author | Freya Murphy <freya@freyacat.org> | 2025-10-07 11:42:05 -0400 |
|---|---|---|
| committer | Freya Murphy <freya@freyacat.org> | 2025-10-08 19:37:16 -0400 |
| commit | d16fe269aec12c88d22054d87cdef84e2b55b100 (patch) | |
| tree | b7ef2134245e63208671c2cac36e1d902401b19a /dungeon/src | |
| parent | initial baseline (diff) | |
| download | DungeonCrawl-d16fe269aec12c88d22054d87cdef84e2b55b100.tar.gz DungeonCrawl-d16fe269aec12c88d22054d87cdef84e2b55b100.tar.bz2 DungeonCrawl-d16fe269aec12c88d22054d87cdef84e2b55b100.zip | |
impl default to dungeon and its types
Diffstat (limited to 'dungeon/src')
| -rw-r--r-- | dungeon/src/lib.rs | 9 | ||||
| -rw-r--r-- | dungeon/src/map.rs | 20 | ||||
| -rw-r--r-- | dungeon/src/pos.rs | 30 |
3 files changed, 56 insertions, 3 deletions
diff --git a/dungeon/src/lib.rs b/dungeon/src/lib.rs index 66faec3..84fc339 100644 --- a/dungeon/src/lib.rs +++ b/dungeon/src/lib.rs @@ -52,7 +52,10 @@ impl Dungeon { } impl Default for Dungeon { - fn default() -> Self { - Self::new() - } + fn default() -> Self { + let floor = Floor::default(); + let player = Entity::player(floor.player_start()); + + Self { player, floor } + } } diff --git a/dungeon/src/map.rs b/dungeon/src/map.rs index 8c4328c..b6f3356 100644 --- a/dungeon/src/map.rs +++ b/dungeon/src/map.rs @@ -187,3 +187,23 @@ impl Floor { &mut *self.tiles } } +impl Default for Floor { + /// Returns a floor with a single set of walls on the map border + fn default() -> Self { + let mut tiles = Box::new([Tile::Air; TILE_COUNT]); + let player_start = Pos::new_const::<1, 1>(); + let seed = 0u64; + + for pos in Pos::values() { + if pos.is_border() { + tiles[pos.idx()] = Tile::Wall; + } + } + + Self { + tiles, + player_start, + seed, + } + } +} diff --git a/dungeon/src/pos.rs b/dungeon/src/pos.rs index bfd994f..143b0f0 100644 --- a/dungeon/src/pos.rs +++ b/dungeon/src/pos.rs @@ -51,6 +51,25 @@ impl Pos { } } + /// Creates a new position from a given x and y position at compile time. + /// + /// Bounds checks are run at compile time so it is gurenteeed to return a valid `Pos`. + /// + /// # Examples + /// + /// ``` + /// use dungeon::Pos; + /// + /// let pos = Pos::new_const::<1,1>(); + /// assert_eq!(pos.xy(), (1,1)); + /// ``` + #[must_use] + pub const fn new_const<const X: u16, const Y: u16>() -> Self { + assert!(X < MAP_SIZE); + assert!(Y < MAP_SIZE); + Self(X, Y) + } + /// Returns the x and y positions of `Pos`. /// /// # Examples @@ -119,4 +138,15 @@ impl Pos { _ => None, } } + + /// Returns of the given position is on the border of the map + #[must_use] + pub fn is_border(&self) -> bool { + self.0 == 0 || self.0 == MAP_SIZE - 1 || self.1 == 0 || self.1 == MAP_SIZE - 1 + } + + /// Returns an iterator over all possible `Pos` + pub fn values() -> impl Iterator<Item = Self> { + (0..MAP_SIZE).flat_map(|y| (0..MAP_SIZE).filter_map(move |x| Self::new(x, y))) + } } |