diff options
| author | Freya Murphy <freya@freyacat.org> | 2025-11-15 01:59:52 -0500 |
|---|---|---|
| committer | Freya Murphy <freya@freyacat.org> | 2025-11-15 11:15:36 -0500 |
| commit | 583b733520171b9182e01bbe90bf3ec5da0de428 (patch) | |
| tree | f2f3a9b12678df0fbfed2ed7632d0cccdd70e373 /dungeon/src/pos.rs | |
| parent | graphics: add text message rendering (and sans ig) (diff) | |
| download | DungeonCrawl-583b733520171b9182e01bbe90bf3ec5da0de428.tar.gz DungeonCrawl-583b733520171b9182e01bbe90bf3ec5da0de428.tar.bz2 DungeonCrawl-583b733520171b9182e01bbe90bf3ec5da0de428.zip | |
dungeon: refactor entity movement
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 { |