diff options
Diffstat (limited to 'dungeon/src/pos.rs')
| -rw-r--r-- | dungeon/src/pos.rs | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/dungeon/src/pos.rs b/dungeon/src/pos.rs index 7ee97eb..8b6d199 100644 --- a/dungeon/src/pos.rs +++ b/dungeon/src/pos.rs @@ -11,6 +11,7 @@ use rand::{ }; use std::{ + cmp::Ordering, fmt::Display, ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}, }; @@ -663,6 +664,21 @@ impl FPos { let abs_diff = Self::abs_diff(self, other); abs_diff.0 + abs_diff.1 } + + /// Returns the direction the position is from the other + #[must_use] + pub fn dir_to(self, other: Self) -> Option<Direction> { + let x_ord = self.x().partial_cmp(&other.x())?; + let y_ord = self.y().partial_cmp(&other.y())?; + + match (x_ord, y_ord) { + (Ordering::Less, Ordering::Equal) => Some(Direction::East), + (Ordering::Greater, Ordering::Equal) => Some(Direction::West), + (Ordering::Equal, Ordering::Less) => Some(Direction::South), + (Ordering::Equal, Ordering::Greater) => Some(Direction::North), + _ => None, + } + } } impl AddAssign<Self> for FPos { |