From 2f58dce55af96990996d38fffff0ef5cbfe01883 Mon Sep 17 00:00:00 2001 From: Louis Capitanchik <contact@louiscap.co> Date: Sun, 5 Nov 2023 16:53:50 +0000 Subject: [PATCH] Simplify directionality --- src/directionality.rs | 90 ++++++++++++++++++++++++++++++++----------- 1 file changed, 68 insertions(+), 22 deletions(-) diff --git a/src/directionality.rs b/src/directionality.rs index 5592f50..1597a80 100644 --- a/src/directionality.rs +++ b/src/directionality.rs @@ -1,9 +1,7 @@ -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", + } + ) } } -- GitLab