marble-machinations/src/marble_engine/pos.rs

72 lines
1.1 KiB
Rust

use std::ops::Add;
use raylib::prelude::*;
use serde::{Deserialize, Serialize};
pub type PosInt = i16;
#[derive(Debug, Default, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Pos {
pub x: PosInt,
pub y: PosInt,
}
impl Pos {
pub const fn to_vec(self) -> Vector2 {
Vector2 {
x: self.x as f32,
y: self.y as f32,
}
}
pub fn min(self, other: Self) -> Self {
Self {
x: self.x.min(other.x),
y: self.y.min(other.y),
}
}
pub fn max(self, other: Self) -> Self {
Self {
x: self.x.max(other.x),
y: self.y.max(other.y),
}
}
}
impl From<(usize, usize)> for Pos {
fn from(value: (usize, usize)) -> Self {
Self {
x: value.0 as PosInt,
y: value.1 as PosInt,
}
}
}
impl From<(i32, i32)> for Pos {
fn from(value: (i32, i32)) -> Self {
Self {
x: value.0 as PosInt,
y: value.1 as PosInt,
}
}
}
impl From<Vector2> for Pos {
fn from(vec: Vector2) -> Self {
Self {
x: vec.x as PosInt,
y: vec.y as PosInt,
}
}
}
impl Add for Pos {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
x: self.x + rhs.x,
y: self.y + rhs.y,
}
}
}