refactor: move DIRECTIONS constant into direction, add as_u8() const fn for array indexing

This commit is contained in:
2025-08-11 11:03:46 -05:00
parent f6e7228f75
commit 6702b3723a
3 changed files with 59 additions and 7 deletions

View File

@@ -1,5 +1,6 @@
use glam::IVec2;
/// The four cardinal directions.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Direction {
Up,
@@ -9,7 +10,12 @@ pub enum Direction {
}
impl Direction {
pub fn opposite(&self) -> 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,
@@ -18,8 +24,20 @@ impl Direction {
}
}
pub fn as_ivec2(&self) -> IVec2 {
(*self).into()
/// Returns the direction as an IVec2.
pub fn as_ivec2(self) -> IVec2 {
self.into()
}
/// Returns the direction as a u8 (0-3). Constant time.
/// This is useful for indexing into arrays.
pub const fn as_u8(self) -> u8 {
match self {
Direction::Up => 0,
Direction::Down => 1,
Direction::Left => 2,
Direction::Right => 3,
}
}
}