diff --git a/crates/border-wars/src/hex.rs b/crates/border-wars/src/hex.rs index a5523b0..ce313d3 100644 --- a/crates/border-wars/src/hex.rs +++ b/crates/border-wars/src/hex.rs @@ -1,6 +1,7 @@ //! This file contains utilities for working with hex coordinates. use std::collections::HashSet; +use std::ops; /// Contains all ``HexPosition`` neighbors of a ``HexPosition`` pub struct GroupHexPosition(HashSet); @@ -97,3 +98,71 @@ impl HexPosition { GroupHexPosition(positions) } } + +impl ops::Add for HexPosition { + type Output = HexPosition; + + fn add(self, other: HexPosition) -> HexPosition { + HexPosition { + q: self.q + other.q, + r: self.r + other.r, + } + } +} + +impl ops::AddAssign for HexPosition { + fn add_assign(&mut self, other: HexPosition) { + *self = *self + other; + } +} + +impl ops::Sub for HexPosition { + type Output = HexPosition; + + fn sub(self, other: HexPosition) -> HexPosition { + HexPosition { + q: self.q - other.q, + r: self.r - other.r, + } + } +} + +impl ops::SubAssign for HexPosition { + fn sub_assign(&mut self, other: HexPosition) { + *self = *self - other; + } +} + +impl ops::Mul for HexPosition { + type Output = HexPosition; + + fn mul(self, other: i32) -> HexPosition { + HexPosition { + q: self.q * other, + r: self.r * other, + } + } +} + +impl ops::MulAssign for HexPosition { + fn mul_assign(&mut self, other: i32) { + *self = *self * other; + } +} + +impl ops::Div for HexPosition { + type Output = HexPosition; + + fn div(self, other: i32) -> HexPosition { + HexPosition { + q: self.q / other, + r: self.r / other, + } + } +} + +impl ops::DivAssign for HexPosition { + fn div_assign(&mut self, other: i32) { + *self = *self / other; + } +}