use bevy::reflect::Reflect; use core::{fmt, ops}; use super::VectorComponent; #[derive(PartialEq, Eq, Hash, Clone, Copy, Default, Debug, Reflect)] pub struct Vector2 { pub x: T, pub y: T, } impl Vector2 { pub fn x() -> Self { Self { x: T::one(), y: T::zero(), } } pub fn y() -> Self { Self { x: T::zero(), y: T::one(), } } pub fn zero() -> Self { Self { x: T::zero(), y: T::zero(), } } pub fn one() -> Self { Self { x: T::one(), y: T::one(), } } pub fn up() -> Self { Self { x: T::zero(), y: T::one(), } } pub fn down() -> Self { Self { x: T::zero(), y: -T::one(), } } pub fn left() -> Self { Self { x: -T::one(), y: T::zero(), } } pub fn right() -> Self { Self { x: T::one(), y: T::zero(), } } pub fn new(x: T, y: T) -> Vector2 { Vector2 { x, y } } pub fn min(&self, other: &Vector2) -> Vector2 { Vector2 { x: VectorComponent::min(self.x, other.x), y: VectorComponent::min(self.y, other.y), } } pub fn max(&self, other: &Vector2) -> Vector2 { Vector2 { x: VectorComponent::max(self.x, other.x), y: VectorComponent::max(self.y, other.y), } } } impl fmt::Display for Vector2 { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "({}, {})", self.x, self.y) } } impl ops::Add> for Vector2 { type Output = Vector2; fn add(self, rhs: Vector2) -> Self::Output { Vector2 { x: self.x + rhs.x, y: self.y + rhs.y, } } } impl ops::Neg for Vector2 { type Output = Vector2; fn neg(self) -> Self::Output { Vector2 { x: -self.x, y: -self.y, } } } impl ops::Sub> for Vector2 { type Output = Vector2; fn sub(self, rhs: Vector2) -> Self::Output { self + (-rhs) } } impl ops::Mul> for Vector2 { type Output = Vector2; fn mul(self, rhs: Vector2) -> Self::Output { Vector2 { x: self.x * rhs.x, y: self.y * rhs.y, } } } impl ops::Mul for Vector2 { type Output = Vector2; fn mul(self, rhs: T) -> Self::Output { Vector2 { x: self.x * rhs, y: self.y * rhs, } } } impl ops::Div> for Vector2 { type Output = Vector2; fn div(self, rhs: Vector2) -> Self::Output { Vector2 { x: self.x / rhs.x, y: self.y / rhs.y, } } } impl ops::Div for Vector2 { type Output = Vector2; fn div(self, rhs: T) -> Self::Output { Vector2 { x: self.x / rhs, y: self.y / rhs, } } }