#![allow(missing_docs)]
#[cfg(feature = "bevy")]
use bevy::ecs::component::Component;
#[cfg(feature = "reflect")]
use bevy::{ecs::reflect::ReflectComponent, reflect::Reflect};
pub type DirectionIndex = usize;
pub trait DirectionTrait: Into<DirectionIndex> + Copy {
fn opposite(&self) -> Self;
fn rotation_basis(&self) -> &'static [Self];
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "bevy", derive(Component))]
#[cfg_attr(feature = "reflect", derive(Reflect), reflect(Component))]
pub enum Direction {
#[default]
XForward = 0,
YForward = 1,
XBackward = 2,
YBackward = 3,
ZForward = 4,
ZBackward = 5,
}
impl DirectionTrait for Direction {
fn opposite(&self) -> Direction {
match self {
Direction::XForward => Direction::XBackward,
Direction::XBackward => Direction::XForward,
Direction::YForward => Direction::YBackward,
Direction::YBackward => Direction::YForward,
Direction::ZForward => Direction::ZBackward,
Direction::ZBackward => Direction::ZForward,
}
}
fn rotation_basis(&self) -> &'static [Direction] {
match self {
Direction::XForward => X_POS_AXIS,
Direction::XBackward => X_NEG_AXIS,
Direction::YForward => Y_POS_AXIS,
Direction::YBackward => Y_NEG_AXIS,
Direction::ZForward => Z_POS_AXIS,
Direction::ZBackward => Z_NEG_AXIS,
}
}
}
impl From<Direction> for usize {
fn from(value: Direction) -> Self {
value as usize
}
}
const X_POS_AXIS: &[Direction] = &[
Direction::YForward,
Direction::ZForward,
Direction::YBackward,
Direction::ZBackward,
];
const X_NEG_AXIS: &[Direction] = &[
Direction::ZForward,
Direction::YForward,
Direction::ZBackward,
Direction::YBackward,
];
const Y_POS_AXIS: &[Direction] = &[
Direction::ZForward,
Direction::XForward,
Direction::ZBackward,
Direction::XBackward,
];
const Y_NEG_AXIS: &[Direction] = &[
Direction::XForward,
Direction::ZForward,
Direction::XBackward,
Direction::ZBackward,
];
const Z_POS_AXIS: &[Direction] = &[
Direction::XForward,
Direction::YForward,
Direction::XBackward,
Direction::YBackward,
];
const Z_NEG_AXIS: &[Direction] = &[
Direction::YForward,
Direction::XForward,
Direction::YBackward,
Direction::XBackward,
];