use bevy::prelude::{Reflect, Vec2};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)]
#[must_use]
pub enum AxisDirection {
Negative,
Positive,
}
impl AxisDirection {
#[must_use]
#[inline]
pub fn full_active_value(&self) -> f32 {
match self {
Self::Negative => -1.0,
Self::Positive => 1.0,
}
}
#[must_use]
#[inline]
pub fn is_active(&self, value: f32, threshold: f32) -> bool {
assert!(threshold >= 0.0);
match self {
Self::Negative => value < -threshold,
Self::Positive => value > threshold,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)]
#[must_use]
pub enum DualAxisType {
X,
Y,
}
impl DualAxisType {
#[inline]
pub const fn axes() -> [Self; 2] {
[Self::X, Self::Y]
}
#[inline]
pub const fn directions(&self) -> [DualAxisDirection; 2] {
[self.negative(), self.positive()]
}
#[inline]
pub const fn negative(&self) -> DualAxisDirection {
match self {
Self::X => DualAxisDirection::Left,
Self::Y => DualAxisDirection::Down,
}
}
#[inline]
pub const fn positive(&self) -> DualAxisDirection {
match self {
Self::X => DualAxisDirection::Right,
Self::Y => DualAxisDirection::Up,
}
}
#[must_use]
#[inline]
pub const fn get_value(&self, value: Vec2) -> f32 {
match self {
Self::X => value.x,
Self::Y => value.y,
}
}
#[must_use]
#[inline]
pub const fn dual_axis_value(&self, value: f32) -> Vec2 {
match self {
Self::X => Vec2::new(value, 0.0),
Self::Y => Vec2::new(0.0, value),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)]
#[must_use]
pub enum DualAxisDirection {
Up,
Down,
Left,
Right,
}
impl DualAxisDirection {
#[inline]
pub fn axis(&self) -> DualAxisType {
match self {
Self::Up => DualAxisType::Y,
Self::Down => DualAxisType::Y,
Self::Left => DualAxisType::X,
Self::Right => DualAxisType::X,
}
}
#[inline]
pub fn axis_direction(&self) -> AxisDirection {
match self {
Self::Up => AxisDirection::Positive,
Self::Down => AxisDirection::Negative,
Self::Left => AxisDirection::Negative,
Self::Right => AxisDirection::Positive,
}
}
#[must_use]
#[inline]
pub fn full_active_value(&self) -> Vec2 {
match self {
Self::Up => Vec2::Y,
Self::Down => Vec2::NEG_Y,
Self::Left => Vec2::NEG_X,
Self::Right => Vec2::X,
}
}
#[must_use]
#[inline]
pub fn is_active(&self, value: Vec2, threshold: f32) -> bool {
let axis_value = self.axis().get_value(value);
self.axis_direction().is_active(axis_value, threshold)
}
}