pacman/src/lib.rs
2022-04-29 14:05:01 -05:00

190 lines
3.6 KiB
Rust

use std::ops::{Add, Sub, Mul, Div, AddAssign, SubAssign, MulAssign, DivAssign};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Direction {
North,
South,
East,
West
}
impl Direction {
pub fn flipped(self) -> Self {
use Direction::*;
match self {
North => South,
South => North,
East => West,
West => East,
}
}
}
impl From<Direction> for Vec2 {
fn from(v: Direction) -> Self {
use Direction::*;
match v {
North => (0, -1).into(),
South => (0, 1).into(),
East => (1, 0).into(),
West => (-1, 0).into(),
}
}
}
impl Mul<i32> for Direction {
type Output = Vec2;
fn mul(self, rhs: i32) -> Self::Output {
Vec2::from(self) * rhs
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct Vec2 {
pub x: i32,
pub y: i32
}
impl Vec2 {
pub fn sq_magnitude(self) -> i32 {
self.x * self.x + self.y * self.y
}
pub fn magnitude(self) -> f32 {
(self.sq_magnitude() as f32).sqrt()
}
pub fn sq_dist(self, rhs: Self) -> i32 {
(self - rhs).sq_magnitude()
}
pub fn dist(self, rhs: Self) -> f32 {
(self - rhs).magnitude()
}
}
impl From<(i32, i32)> for Vec2 {
fn from(v: (i32, i32)) -> Self {
Self { x: v.0, y: v.1 }
}
}
impl From<Vec2> for (i32, i32) {
fn from(v: Vec2) -> Self {
(v.x, v.y)
}
}
impl Add for Vec2 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self { x: self.x + rhs.x, y: self.y + rhs.y }
}
}
impl Add<(i32, i32)> for Vec2 {
type Output = Self;
fn add(self, rhs: (i32, i32)) -> Self::Output {
self + Self::from(rhs)
}
}
impl Add<Direction> for Vec2 {
type Output = Self;
fn add(self, rhs: Direction) -> Self::Output {
self + Self::from(rhs)
}
}
impl Sub for Vec2 {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self { x: self.x - rhs.x, y: self.y - rhs.y }
}
}
impl Sub<(i32, i32)> for Vec2 {
type Output = Self;
fn sub(self, rhs: (i32, i32)) -> Self::Output {
self - Self::from(rhs)
}
}
impl Sub<Direction> for Vec2 {
type Output = Self;
fn sub(self, rhs: Direction) -> Self::Output {
self - Self::from(rhs)
}
}
impl Mul<i32> for Vec2 {
type Output = Self;
fn mul(self, rhs: i32) -> Self::Output {
Self { x: self.x * rhs, y: self.y * rhs }
}
}
impl Div<i32> for Vec2 {
type Output = Self;
fn div(self, rhs: i32) -> Self::Output {
Self { x: self.x / rhs, y: self.y / rhs }
}
}
impl AddAssign for Vec2 {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl AddAssign<(i32, i32)> for Vec2 {
fn add_assign(&mut self, rhs: (i32, i32)) {
*self = *self + rhs
}
}
impl AddAssign<Direction> for Vec2 {
fn add_assign(&mut self, rhs: Direction) {
*self = *self + rhs
}
}
impl SubAssign for Vec2 {
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs
}
}
impl SubAssign<(i32, i32)> for Vec2 {
fn sub_assign(&mut self, rhs: (i32, i32)) {
*self = *self - rhs
}
}
impl SubAssign<Direction> for Vec2 {
fn sub_assign(&mut self, rhs: Direction) {
*self = *self - rhs
}
}
impl MulAssign<i32> for Vec2 {
fn mul_assign(&mut self, rhs: i32) {
*self = *self * rhs
}
}
impl DivAssign<i32> for Vec2 {
fn div_assign(&mut self, rhs: i32) {
*self = *self / rhs
}
}