use glam::IVec2; /// The four cardinal directions. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum Direction { Up, Down, Left, Right, } impl Direction { /// The four cardinal directions. /// This is just a convenience constant for iterating over the directions. pub const DIRECTIONS: [Direction; 4] = [Direction::Up, Direction::Down, Direction::Left, Direction::Right]; /// Returns the opposite direction. Constant time. pub const fn opposite(self) -> Direction { match self { Direction::Up => Direction::Down, Direction::Down => Direction::Up, Direction::Left => Direction::Right, Direction::Right => Direction::Left, } } /// Returns the direction as an IVec2. pub fn as_ivec2(self) -> IVec2 { self.into() } /// Returns the direction as a usize (0-3). Constant time. /// This is useful for indexing into arrays. pub const fn as_usize(self) -> usize { match self { Direction::Up => 0, Direction::Down => 1, Direction::Left => 2, Direction::Right => 3, } } } impl From for IVec2 { fn from(dir: Direction) -> Self { match dir { Direction::Up => -IVec2::Y, Direction::Down => IVec2::Y, Direction::Left => -IVec2::X, Direction::Right => IVec2::X, } } }