diff options
| author | Freya Murphy <freya@freyacat.org> | 2025-10-27 15:36:18 -0400 |
|---|---|---|
| committer | Freya Murphy <freya@freyacat.org> | 2025-10-27 15:37:02 -0400 |
| commit | 878cddccd42f3b4251983f96d98119ecae1a514a (patch) | |
| tree | 6999eda7f6ef257ee5827ae2027c02eea811310f /dungeon/src | |
| parent | graphics: use atlas texture when rendering (and lots of refactoring) (diff) | |
| download | DungeonCrawl-878cddccd42f3b4251983f96d98119ecae1a514a.tar.gz DungeonCrawl-878cddccd42f3b4251983f96d98119ecae1a514a.tar.bz2 DungeonCrawl-878cddccd42f3b4251983f96d98119ecae1a514a.zip | |
dungeon: add step_by function for Pos, and rename step to step_by for FPos
Diffstat (limited to 'dungeon/src')
| -rw-r--r-- | dungeon/src/pos.rs | 44 |
1 files changed, 36 insertions, 8 deletions
diff --git a/dungeon/src/pos.rs b/dungeon/src/pos.rs index e2d449c..8f1f8dd 100644 --- a/dungeon/src/pos.rs +++ b/dungeon/src/pos.rs @@ -219,13 +219,41 @@ impl Pos { /// ``` #[must_use] pub const fn step(self, dir: Direction) -> Option<Self> { - use Direction as D; + self.step_by(dir, 1) + } + + /// Steps `Pos` a given ammount in the `Direction` `dir`. + /// + /// Returns `None` if the position goes out of the map. + /// + /// # Examples + /// + /// ``` + /// use dungeon::{Direction, Pos}; + /// + /// let pos = Pos::new(0, 1).unwrap(); + /// let new_pos = pos.step_by(Direction::South, 3); + /// assert_eq!(new_pos, Pos::new(0, 4)); + /// ``` + /// + /// ``` + /// use dungeon::{Direction, Pos}; + /// + /// let pos = Pos::new(1, 1).unwrap(); + /// let new_pos = pos.step_by(Direction::West, 2); + /// assert!(new_pos.is_none()); + /// ``` + #[must_use] + pub const fn step_by(self, dir: Direction, amt: u16) -> Option<Self> { let (x, y) = self.xy(); + if amt > MAP_SIZE { + return None; + } match dir { - D::North if y > 0 => Self::new(x, y - 1), - D::South => Self::new(x, y + 1), - D::East => Self::new(x + 1, y), - D::West if x > 0 => Self::new(x - 1, y), + Direction::North if y >= amt => Self::new(x, y - amt), + Direction::South => Self::new(x, y + amt), + Direction::East => Self::new(x + amt, y), + Direction::West if x >= amt => Self::new(x - amt, y), _ => None, } } @@ -399,7 +427,7 @@ impl FPos { /// use dungeon::{Direction, FPos}; /// /// let fpos = FPos::new(0.0, 1.0); - /// let new_fpos = fpos.step(Direction::North, 0.3); + /// let new_fpos = fpos.step_by(Direction::North, 0.3); /// assert!(new_fpos.is_some()); /// ``` /// @@ -407,11 +435,11 @@ impl FPos { /// use dungeon::{Direction, FPos}; /// /// let fpos = FPos::new(0.0, 0.0); - /// let new_fpos = fpos.step(Direction::North, 5.0); + /// let new_fpos = fpos.step_by(Direction::North, 5.0); /// assert!(new_fpos.is_none()); /// ``` #[must_use] - pub fn step(self, dir: Direction, amt: f32) -> Option<Self> { + pub fn step_by(self, dir: Direction, amt: f32) -> Option<Self> { use Direction as D; let (mut x, mut y) = self.xy(); match dir { |