|
| 1 | +use std::ops::Neg; |
| 2 | + |
| 3 | +use num::{one, zero, One, Zero}; |
| 4 | +use strum::{Display, EnumCount, FromRepr}; |
| 5 | + |
| 6 | +use crate::helpers::pt::Pt; |
| 7 | + |
| 8 | +/// ↓ ↑ ← → |
| 9 | +#[repr(u8)] |
| 10 | +#[derive( |
| 11 | + Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, FromRepr, EnumCount, Display, |
| 12 | +)] |
| 13 | +pub enum Direction { |
| 14 | + Right, |
| 15 | + Down, |
| 16 | + Left, |
| 17 | + Up, |
| 18 | +} |
| 19 | + |
| 20 | +impl Direction { |
| 21 | + /// Turns 90 degrees to the left. |
| 22 | + pub fn turn_left(&self) -> Self { |
| 23 | + Self::from_repr(((*self as u8) + 3) % (Self::COUNT as u8)).unwrap() |
| 24 | + } |
| 25 | + |
| 26 | + /// Turns 90 degrees to the right. |
| 27 | + pub fn turn_right(&self) -> Self { |
| 28 | + Self::from_repr(((*self as u8) + 1) % (Self::COUNT as u8)).unwrap() |
| 29 | + } |
| 30 | + |
| 31 | + /// Turns around (e.g. performs a 180 degrees turn). |
| 32 | + pub fn turn_around(&self) -> Self { |
| 33 | + Self::from_repr(((*self as u8) + 2) % (Self::COUNT as u8)).unwrap() |
| 34 | + } |
| 35 | + |
| 36 | + /// Returns the displacement to apply to move one step in this direction. |
| 37 | + /// The displacement is returned as a [`Pt`]. |
| 38 | + /// |
| 39 | + /// # Notes |
| 40 | + /// |
| 41 | + /// Because this enum is meant to be used to move around a map represented as a series of rows |
| 42 | + /// like on a computer screen, `Up`'s displacement will _subtract_ one from the Y axis, while |
| 43 | + /// `Down`'s will _add_ one to the Y axis. |
| 44 | + pub fn displacement<T>(&self) -> Pt<T> |
| 45 | + where |
| 46 | + T: Zero + One + Neg<Output = T>, |
| 47 | + { |
| 48 | + match self { |
| 49 | + Direction::Right => Pt::new(one(), zero()), |
| 50 | + Direction::Down => Pt::new(zero(), one()), |
| 51 | + Direction::Left => Pt::new(-one::<T>(), zero()), |
| 52 | + Direction::Up => Pt::new(zero(), -one::<T>()), |
| 53 | + } |
| 54 | + } |
| 55 | +} |
0 commit comments