Skip to content

Commit

Permalink
Fix CubeVec component order
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Sep 22, 2023
1 parent 3c587b3 commit e6d1c0f
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions src/game/cube_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use super::CubeDir;
/// (see https://www.redblobgames.com/grids/hexagons/#coordinates-cube).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CubeVec {
r: i32,
q: i32,
r: i32,
s: i32,
}

Expand All @@ -31,19 +31,19 @@ impl CubeVec {

/// Creates a new vector from the given cube components.
#[inline]
pub const fn new(r: i32, q: i32, s: i32) -> Self {
Self { r, q, s }
pub const fn new(q: i32, r: i32, s: i32) -> Self {
Self { q, r, s }
}

/// Creates a new vector from the given r/q components.
#[inline]
pub const fn rq(r: i32, q: i32) -> Self {
Self { r, q, s: -q - r }
pub const fn qr(q: i32, r: i32) -> Self {
Self { q, r, s: -q - r }
}

/// The squared length of this vector.
#[inline]
pub fn squared_length(self) -> i32 { self.r * self.r + self.q * self.q + self.s * self.s }
pub fn squared_length(self) -> i32 { self.q * self.q + self.r * self.r + self.s * self.s }

/// The length of this vector.
#[inline]
Expand All @@ -59,11 +59,11 @@ impl CubeVec {

/// The first component of this vector.
#[inline]
pub fn r(self) -> i32 { self.r }
pub fn q(self) -> i32 { self.q }

/// The second component of this vector.
#[inline]
pub fn q(self) -> i32 { self.q }
pub fn r(self) -> i32 { self.r }

/// The third component of this vector.
#[inline]
Expand Down Expand Up @@ -94,7 +94,7 @@ impl Add for CubeVec {
type Output = Self;

fn add(self, rhs: Self) -> Self {
Self::new(self.r + rhs.r, self.q + rhs.q, self.s + rhs.s)
Self::new(self.q + rhs.q, self.r + rhs.r, self.s + rhs.s)
}
}

Expand All @@ -108,8 +108,8 @@ impl Add<CubeDir> for CubeVec {

impl AddAssign<CubeVec> for CubeVec {
fn add_assign(&mut self, rhs: Self) {
self.r += rhs.r;
self.q += rhs.q;
self.r += rhs.r;
self.s += rhs.s;
}
}
Expand All @@ -124,7 +124,7 @@ impl Sub for CubeVec {
type Output = Self;

fn sub(self, rhs: Self) -> Self {
Self::new(self.r - rhs.r, self.q - rhs.q, self.s - rhs.s)
Self::new(self.q - rhs.q, self.r - rhs.r, self.s - rhs.s)
}
}

Expand All @@ -138,8 +138,8 @@ impl Sub<CubeDir> for CubeVec {

impl SubAssign<CubeVec> for CubeVec {
fn sub_assign(&mut self, rhs: Self) {
self.r -= rhs.r;
self.q -= rhs.q;
self.r -= rhs.r;
self.s -= rhs.s;
}
}
Expand All @@ -154,22 +154,22 @@ impl Mul<i32> for CubeVec {
type Output = Self;

fn mul(self, rhs: i32) -> Self {
Self::new(self.r * rhs, self.q * rhs, self.s * rhs)
Self::new(self.q * rhs, self.r * rhs, self.s * rhs)
}
}

impl Mul<CubeVec> for i32 {
type Output = CubeVec;

fn mul(self, rhs: CubeVec) -> CubeVec {
CubeVec::new(self * rhs.r, self * rhs.q, self * rhs.s)
CubeVec::new(self * rhs.q, self * rhs.r, self * rhs.s)
}
}

impl MulAssign<i32> for CubeVec {
fn mul_assign(&mut self, rhs: i32) {
self.r *= rhs;
self.q *= rhs;
self.r *= rhs;
self.s *= rhs;
}
}
Expand All @@ -178,14 +178,14 @@ impl Div<i32> for CubeVec {
type Output = Self;

fn div(self, rhs: i32) -> Self {
Self::new(self.r / rhs, self.q / rhs, self.s / rhs)
Self::new(self.q / rhs, self.r / rhs, self.s / rhs)
}
}

impl DivAssign<i32> for CubeVec {
fn div_assign(&mut self, rhs: i32) {
self.r /= rhs;
self.q /= rhs;
self.r /= rhs;
self.s /= rhs;
}
}
Expand All @@ -194,25 +194,25 @@ impl Neg for CubeVec {
type Output = Self;

fn neg(self) -> Self {
Self::new(-self.r, -self.q, -self.s)
Self::new(-self.q, -self.r, -self.s)
}
}

impl fmt::Display for CubeVec {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}, {}, {})", self.r, self.q, self.s)
write!(f, "({}, {}, {})", self.q, self.r, self.s)
}
}

impl From<CubeDir> for CubeVec {
fn from(dir: CubeDir) -> Self {
match dir {
CubeDir::Right => Self::rq(1, 0),
CubeDir::DownRight => Self::rq(0, 1),
CubeDir::DownLeft => Self::rq(-1, 1),
CubeDir::Left => Self::rq(-1, 0),
CubeDir::UpLeft => Self::rq(0, -1),
CubeDir::UpRight => Self::rq(1, -1),
CubeDir::Right => Self::qr(1, 0),
CubeDir::DownRight => Self::qr(0, 1),
CubeDir::DownLeft => Self::qr(-1, 1),
CubeDir::Left => Self::qr(-1, 0),
CubeDir::UpLeft => Self::qr(0, -1),
CubeDir::UpRight => Self::qr(1, -1),
}
}
}
Expand All @@ -223,25 +223,25 @@ impl<T> From<Vec2<T>> for CubeVec where T: Into<i32> {
let x: i32 = vec.x.into();
let y: i32 = vec.y.into();
let r = y - 2;
CubeVec::rq(x - 1 - r.max(0), r)
CubeVec::qr(x - 1 - r.max(0), r)
}
}

impl From<CubeVec> for [i32; 3] {
fn from(vec: CubeVec) -> Self {
[vec.r, vec.q, vec.s]
[vec.q, vec.r, vec.s]
}
}

impl TryFrom<&Element> for CubeVec {
type Error = Error;

fn try_from(elem: &Element) -> Result<Self> {
Ok(CubeVec::new(
elem.attribute("r")?.parse()?,
elem.attribute("q")?.parse()?,
elem.attribute("s")?.parse()?,
))
Ok(CubeVec {
q: elem.attribute("q")?.parse()?,
r: elem.attribute("r")?.parse()?,
s: elem.attribute("s")?.parse()?,
})
}
}

Expand All @@ -252,7 +252,7 @@ mod tests {
#[test]
fn test_xml_parses() {
assert_xml_parse!(
r#"<position r="23" q="0" s="-2" />"#,
r#"<position q="23" r="0" s="-2" />"#,
CubeVec::new(23, 0, -2)
);
}
Expand Down

0 comments on commit e6d1c0f

Please sign in to comment.