diff options
Diffstat (limited to 'dungeon/src/pos.rs')
| -rw-r--r-- | dungeon/src/pos.rs | 30 |
1 files changed, 30 insertions, 0 deletions
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))) + } } |