From 9e1a836f663c03e462fbf1bcff444a20aaf56eaf Mon Sep 17 00:00:00 2001 From: metamuffin Date: Wed, 15 Nov 2023 20:03:59 +0100 Subject: a --- framework/src/vector.rs | 128 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 framework/src/vector.rs (limited to 'framework/src/vector.rs') diff --git a/framework/src/vector.rs b/framework/src/vector.rs new file mode 100644 index 0000000..5227d04 --- /dev/null +++ b/framework/src/vector.rs @@ -0,0 +1,128 @@ +pub type UVec2 = Vec2; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct Vec2 { + pub x: T, + pub y: T, +} + +impl From> for Vec2 { + fn from(value: Vec2) -> Self { + Self { + x: value.x as u16, + y: value.y as u16, + } + } +} +impl From> for Vec2 { + fn from(value: Vec2) -> Self { + Self { + x: value.x as isize, + y: value.y as isize, + } + } +} +impl From> for Vec2 { + fn from(value: Vec2) -> Self { + Self { + x: value.x as isize, + y: value.y as isize, + } + } +} + +impl Vec2 { + pub const ZERO: Vec2 = Vec2 { x: 0, y: 0 }; + pub const UP: Vec2 = Vec2 { x: 0, y: -1 }; + pub const LEFT: Vec2 = Vec2 { x: -1, y: 0 }; +} +impl Vec2 { + pub const ZERO: Vec2 = Vec2 { x: 0.0, y: 0.0 }; + pub const UP: Vec2 = Vec2 { x: 0.0, y: -1.0 }; + pub const LEFT: Vec2 = Vec2 { x: -1.0, y: 0.0 }; +} + +impl + Copy> Vec2 { + pub fn downscale(&self, f: T) -> Self { + Self { + x: self.x / f, + y: self.y / f, + } + } +} + +impl + Copy> Vec2 { + pub fn scale(&self, f: T) -> Self { + Self { + x: self.x * f, + y: self.y * f, + } + } + pub fn area(&self) -> T { + self.x * self.y + } +} + +impl Vec2 { + pub fn x_only(&self) -> Self { + Self { x: self.x, y: 0 } + } + pub fn y_only(&self) -> Self { + Self { x: 0, y: self.y } + } +} + +impl Into> for Vec2 { + fn into(self) -> Vec2 { + Vec2 { + x: self.x as f32, + y: self.y as f32, + } + } +} +impl From<(isize, isize)> for Vec2 { + fn from((x, y): (isize, isize)) -> Self { + Vec2 { + x: x as f32, + y: y as f32, + } + } +} + +impl std::ops::Add for Vec2 { + type Output = Vec2; + #[inline] + fn add(self, rhs: Self) -> Self::Output { + Vec2 { + x: self.x + rhs.x, + y: self.y + rhs.y, + } + } +} +impl std::ops::Sub for Vec2 { + type Output = Vec2; + #[inline] + fn sub(self, rhs: Self) -> Self::Output { + Vec2 { + x: self.x - rhs.x, + y: self.y - rhs.y, + } + } +} +impl std::ops::Mul for Vec2 { + type Output = Vec2; + #[inline] + fn mul(self, rhs: Self) -> Self::Output { + Vec2 { + x: self.x * rhs.x, + y: self.y * rhs.y, + } + } +} + +impl From<(T, T)> for Vec2 { + #[inline] + fn from((x, y): (T, T)) -> Self { + Vec2 { x, y } + } +} -- cgit v1.2.3-70-g09d2