diff options
Diffstat (limited to 'dungeon/src/pos.rs')
| -rw-r--r-- | dungeon/src/pos.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/dungeon/src/pos.rs b/dungeon/src/pos.rs index 2682261..a7b6080 100644 --- a/dungeon/src/pos.rs +++ b/dungeon/src/pos.rs @@ -5,6 +5,11 @@ use strum::IntoEnumIterator; use strum_macros::EnumIter; +use rand::{ + Rng, + distr::{Distribution, StandardUniform}, +}; + use std::ops::{AddAssign, SubAssign}; use crate::{MAP_SIZE_USIZE, map::MAP_SIZE}; @@ -44,6 +49,22 @@ impl Direction { pub fn values() -> impl Iterator<Item = Self> { Self::iter() } + + pub fn get_random_dir() -> Self { + rand::random() + } +} + +impl Distribution<Direction> for StandardUniform { + fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Direction { + // match rng.gen_range(0, 3) { // rand 0.5, 0.6, 0.7 + match rng.random_range(0..=3) { + 0 => Direction::North, + 1 => Direction::East, + 2 => Direction::South, + _ => Direction::West, + } + } } /// The `Pos` type represents a 2D position inside the dungeon grid. @@ -280,6 +301,11 @@ impl Pos { Self(x, y) } + pub fn manhattan(self, other: Self) -> u16 { + let abs_diff = Self::abs_diff(self, other); + abs_diff.0 + abs_diff.1 + } + /// Returns of the given position is on the border of the map /// /// ``` @@ -473,6 +499,11 @@ impl FPos { let y = (self.1 - other.1).abs(); Self(x, y) } + + pub fn manhattan(self, other: Self) -> f32 { + let abs_diff = Self::abs_diff(self, other); + abs_diff.0 + abs_diff.1 + } } impl Default for FPos { /// Returns a default postion at the origin (0,0) |