From aaf194e209ebe55cafbf6ff266ca4567faa09e6f Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Thu, 16 Oct 2025 11:06:01 -0400 Subject: dungeon: add more getters and lib fns --- dungeon/src/lib.rs | 24 ++++++++++++++++++++++++ dungeon/src/pos.rs | 22 ++++++++++++++++++++++ 2 files changed, 46 insertions(+) (limited to 'dungeon') diff --git a/dungeon/src/lib.rs b/dungeon/src/lib.rs index 84fc339..cc15455 100644 --- a/dungeon/src/lib.rs +++ b/dungeon/src/lib.rs @@ -49,6 +49,30 @@ impl Dungeon { Self { player, floor } } + + /// Returns a reference to the player + #[must_use] + pub fn player(&self) -> &Entity { + &self.player + } + + /// Returns a mutable reference to the player + #[must_use] + pub fn player_mut(&mut self) -> &mut Entity { + &mut self.player + } + + /// Returns a reference to the current floor + #[must_use] + pub fn floor(&self) -> &Floor { + &self.floor + } + + /// Returns a mutable reference to the current floor + #[must_use] + pub fn floor_mut(&mut self) -> &mut Floor { + &mut self.floor + } } impl Default for Dungeon { diff --git a/dungeon/src/pos.rs b/dungeon/src/pos.rs index 8695d0b..a1de65e 100644 --- a/dungeon/src/pos.rs +++ b/dungeon/src/pos.rs @@ -188,6 +188,28 @@ impl Pos { } } + /// Computes the absolute difference between to positions + /// + /// Both values are gurenteed to be less than MAP_SIZE + /// + /// # Examples + /// + /// ``` + /// use dungeon::Pos; + /// + /// let pos1 = Pos::new(2,7).unwrap(); + /// let pos2 = Pos::new(1,17).unwrap(); + /// let diff = pos1.abs_diff(pos2); + /// assert_eq!(diff.xy(), (1, 10)); + /// ``` + /// + #[must_use] + pub const fn abs_diff(self, other: Self) -> Self { + let x = self.0.abs_diff(other.0); + let y = self.1.abs_diff(other.1); + Self(x, y) + } + /// Returns of the given position is on the border of the map /// /// ``` -- cgit v1.2.3-freya