summaryrefslogtreecommitdiff
path: root/dungeon/src/pos.rs
diff options
context:
space:
mode:
Diffstat (limited to 'dungeon/src/pos.rs')
-rw-r--r--dungeon/src/pos.rs62
1 files changed, 49 insertions, 13 deletions
diff --git a/dungeon/src/pos.rs b/dungeon/src/pos.rs
index f6c5eb4..0cad387 100644
--- a/dungeon/src/pos.rs
+++ b/dungeon/src/pos.rs
@@ -53,8 +53,15 @@ impl Direction {
Self::iter()
}
- pub fn get_random_dir() -> Self {
- rand::random()
+ /// Returns a random direction provided with a rng
+ #[must_use]
+ pub fn random<R: Rng + ?Sized>(rng: &mut R) -> Self {
+ match rng.random_range(0..4) {
+ 0 => Self::North,
+ 1 => Self::South,
+ 2 => Self::East,
+ _ => Self::West,
+ }
}
}
impl Display for Direction {
@@ -67,16 +74,9 @@ impl Display for Direction {
}
}
}
-
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,
- }
+ Direction::random(rng)
}
}
@@ -314,7 +314,18 @@ impl Pos {
}
/// Returns the manhattan distance between `self` and `other`
- pub fn manhattan(self, other: Self) -> u16 {
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use dungeon::Pos;
+ ///
+ /// let pos1 = Pos::new(3, 6).unwrap();
+ /// let pos2 = Pos::new(5, 1).unwrap();
+ /// assert_eq!(pos1.manhattan(pos2), 7);
+ /// ```
+ #[must_use]
+ pub const fn manhattan(self, other: Self) -> u16 {
let abs_diff = Self::abs_diff(self, other);
abs_diff.0 + abs_diff.1
}
@@ -352,6 +363,14 @@ impl Pos {
pub fn values() -> impl Iterator<Item = Self> {
(0..MAP_SIZE).flat_map(|y| (0..MAP_SIZE).filter_map(move |x| Self::new(x, y)))
}
+
+ /// Returns a random position provided with a rng
+ #[must_use]
+ pub fn random<R: Rng + ?Sized>(rng: &mut R) -> Self {
+ let x = rng.random_range(0..MAP_SIZE);
+ let y = rng.random_range(0..MAP_SIZE);
+ Self(x, y)
+ }
}
impl Default for Pos {
/// Returns a default postion at the origin (0,0)
@@ -375,6 +394,11 @@ impl TryFrom<usize> for Pos {
Self::from_idx(value).ok_or(())
}
}
+impl Distribution<Pos> for StandardUniform {
+ fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Pos {
+ Pos::random(rng)
+ }
+}
/// The `FPos` type represents a floating 2D (temp) position.
///
@@ -512,13 +536,25 @@ impl FPos {
/// ```
///
#[must_use]
- pub fn abs_diff(self, other: Self) -> Self {
+ pub const fn abs_diff(self, other: Self) -> Self {
let x = (self.0 - other.0).abs();
let y = (self.1 - other.1).abs();
Self(x, y)
}
- pub fn manhattan(self, other: Self) -> f32 {
+ /// Returns the manhattan distance between `self` and `other`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use dungeon::FPos;
+ ///
+ /// let fpos1 = FPos::new(1.0, 0.0);
+ /// let fpos2 = FPos::new(1.2, 1.3);
+ /// assert_eq!(fpos1.manhattan(fpos2), 1.5);
+ /// ```
+ #[must_use]
+ pub const fn manhattan(self, other: Self) -> f32 {
let abs_diff = Self::abs_diff(self, other);
abs_diff.0 + abs_diff.1
}