122 lines
2.4 KiB
Rust
122 lines
2.4 KiB
Rust
#![allow(non_snake_case)]
|
|
#![allow(non_upper_case_globals)]
|
|
#![allow(unused_mut)]
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
pub struct Hex {
|
|
pub s: f64,
|
|
pub r: f64,
|
|
pub q: f64,
|
|
pub y: f64,
|
|
}
|
|
|
|
pub enum HexDirection {
|
|
SPos,
|
|
SNeg,
|
|
RPos,
|
|
RNeg,
|
|
QPos,
|
|
QNeg,
|
|
YPos,
|
|
YNeg,
|
|
}
|
|
|
|
impl Hex {
|
|
pub fn distance_to(&self, other: Hex) -> f64 {
|
|
let ret = *self - other;
|
|
ret.length()
|
|
}
|
|
pub fn length(&self) -> f64 {
|
|
self.q.abs() + self.r.abs() + self.s.abs() / 2.0
|
|
}
|
|
|
|
pub fn round(mut self) -> Hex {
|
|
let q = self.q.round();
|
|
let r = self.r.round();
|
|
let s = self.s.round();
|
|
let y = self.y.round();
|
|
Hex { q, r, s, y }
|
|
}
|
|
pub fn scale(self, k: f64) -> Hex {
|
|
Hex {
|
|
q: self.q * k,
|
|
r: self.r * k,
|
|
s: self.s * k,
|
|
y: self.y * k,
|
|
}
|
|
}
|
|
|
|
pub fn rotate_left(&mut self) {
|
|
self.s = -self.s;
|
|
self.q = -self.q;
|
|
self.r = -self.r;
|
|
}
|
|
pub fn rotate_right(&mut self) {
|
|
self.s = -self.s;
|
|
self.q = -self.q;
|
|
self.r = -self.r;
|
|
}
|
|
pub fn lerp(&self, other: Hex, time: f64) -> Hex {
|
|
return Hex {
|
|
q: self.q * (1.0 - time) + other.q * time,
|
|
r: self.r * (1.0 - time) + other.r * time,
|
|
s: self.s * (1.0 - time) + other.s * time,
|
|
y: self.y * (1.0 - time) + other.y * time,
|
|
};
|
|
}
|
|
}
|
|
|
|
impl Add for Hex {
|
|
type Output = Self;
|
|
|
|
fn add(self, other: Self) -> Self {
|
|
Self {
|
|
s: self.s + other.s,
|
|
r: self.r + other.r,
|
|
q: self.q + other.q,
|
|
y: self.y + other.y,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Sub for Hex {
|
|
type Output = Self;
|
|
|
|
fn sub(self, other: Self) -> Self {
|
|
Self {
|
|
s: self.s - other.s,
|
|
r: self.r - other.r,
|
|
q: self.q - other.q,
|
|
y: self.y - other.y,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Div for Hex {
|
|
type Output = Self;
|
|
|
|
fn div(self, other: Self) -> Self {
|
|
Self {
|
|
s: self.s / other.s,
|
|
r: self.r / other.r,
|
|
q: self.q / other.q,
|
|
y: self.y / other.y,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Mul for Hex {
|
|
type Output = Self;
|
|
|
|
fn mul(self, other: Self) -> Self {
|
|
Self {
|
|
s: self.s * other.s,
|
|
r: self.r * other.r,
|
|
q: self.q * other.q,
|
|
y: self.y * other.y,
|
|
}
|
|
}
|
|
}
|
|
|
|
use std::ops::{Add, Div, Mul, Sub};
|