Skip to content
Snippets Groups Projects
Verified Commit 2f58dce5 authored by Louis's avatar Louis :fire:
Browse files

Simplify directionality

parent d5074edf
No related branches found
No related tags found
No related merge requests found
use std::fmt::{Display, Formatter};
use bevy::math::{Vec2, Vec3};
use bevy::prelude::Component;
use std::fmt::{Display, Formatter};
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Default)]
#[derive(Component, Copy, Clone, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Horizontal {
Left,
......@@ -11,6 +9,15 @@ pub enum Horizontal {
Right,
}
impl From<Horizontal> for Directionality {
fn from(value: Horizontal) -> Self {
match value {
Horizontal::Left => Directionality::Left,
Horizontal::Right => Directionality::Right,
}
}
}
impl From<f32> for Horizontal {
fn from(other: f32) -> Self {
if other < 0.0 {
......@@ -30,7 +37,7 @@ impl Display for Horizontal {
}
}
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Default)]
#[derive(Component, Copy, Clone, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Vertical {
Up,
......@@ -38,6 +45,15 @@ pub enum Vertical {
Down,
}
impl From<Vertical> for Directionality {
fn from(value: Vertical) -> Self {
match value {
Vertical::Up => Directionality::Up,
Vertical::Down => Directionality::Down,
}
}
}
impl From<f32> for Vertical {
fn from(other: f32) -> Self {
if other < 0.0 {
......@@ -57,33 +73,63 @@ impl Display for Vertical {
}
}
#[derive(Clone, Debug, Component, PartialEq, Eq, Ord, PartialOrd, Default)]
#[derive(Component, Copy, Clone, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Directionality {
pub vertical: Vertical,
pub horizontal: Horizontal,
pub enum Directionality {
Up,
Down,
Left,
Right,
RightUp,
LeftUp,
LeftDown,
RightDown,
}
impl From<Vec2> for Directionality {
fn from(other: Vec2) -> Self {
Self {
horizontal: other.x.into(),
vertical: other.y.into(),
impl Directionality {
pub fn with_horizontal(&mut self, horizontal: Horizontal) {
*self = match self {
Self::Up | Self::Down | Self::Left | Self::Right => horizontal.into(),
Self::RightUp | Self::LeftUp => match horizontal {
Horizontal::Right => Self::RightUp,
Horizontal::Left => Self::LeftUp,
},
Self::LeftDown | Self::RightDown => match horizontal {
Horizontal::Right => Self::RightDown,
Horizontal::Left => Self::LeftDown,
},
}
}
}
impl From<Vec3> for Directionality {
fn from(other: Vec3) -> Self {
Self {
horizontal: other.x.into(),
vertical: other.y.into(),
pub fn with_vertical(&mut self, vertical: Vertical) {
*self = match self {
Self::Up | Self::Down | Self::Left | Self::Right => vertical.into(),
Self::RightUp | Self::RightDown => match vertical {
Vertical::Up => Self::RightUp,
Vertical::Down => Self::RightDown,
},
Self::LeftDown | Self::LeftUp => match vertical {
Vertical::Up => Self::LeftUp,
Vertical::Down => Self::LeftDown,
},
}
}
}
impl Display for Directionality {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}_{}", self.horizontal, self.vertical)
write!(
f,
"{}",
match self {
Self::Up => "up",
Self::Down => "down",
Self::Left => "left",
Self::Right => "right",
Self::RightUp => "right_up",
Self::LeftUp => "left_up",
Self::RightDown => "right_down",
Self::LeftDown => "left_down",
}
)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment