diff --git a/crates/border-wars/src/map/hex.rs b/crates/border-wars/src/map/hex.rs index 93616f2..79a1b65 100644 --- a/crates/border-wars/src/map/hex.rs +++ b/crates/border-wars/src/map/hex.rs @@ -118,8 +118,8 @@ impl HexDirection { pub struct HexRing { current: HexPosition, direction: HexDirection, - radius: usize, - index: usize, + radius: T, + index: T, } impl Iterator for HexRing { @@ -160,16 +160,16 @@ impl Iterator for HexRing { pub struct HexSpiral { origin: HexPosition, current: HexRing, - radius: usize, - index: usize, + radius: T, + index: T, } -impl + FromPrimitive> Iterator for HexSpiral { +impl Iterator for HexSpiral { type Item = HexPosition; fn next(&mut self) -> Option { - if self.index == 0 { - self.index += 1; + if self.index == T::TWO { + self.index += T::ONE; return Some(self.origin); } if self.index > self.radius { @@ -177,15 +177,15 @@ impl + FromPrimitive> Iterator for HexSpiral { } let mut result = self.current.next(); if result.is_none() && self.index < self.radius { - self.index += 1; - self.current = self.origin.ring(T::from_usize(self.index).unwrap()); + self.index += T::ONE; + self.current = self.origin.ring(self.index); result = self.current.next(); } result } } -impl + FromPrimitive> HexPosition { +impl HexPosition { /// Creates a new hexagonal position. pub fn new(x: T, y: T) -> Self { Self(x, y) @@ -193,29 +193,27 @@ impl + FromPrimitive> HexPosition { /// Returns the distance between two hexagonal positions. pub fn distance(self, other: Self) -> T { - let HexPosition(x, y) = self - other; + let Self(x, y) = self - other; x.abs() + y.abs() + (x + y).abs() / T::TWO } /// Returns the hexagonal ring of the given radius. pub fn ring(self, radius: T) -> HexRing { - let usize_radius = radius.as_(); HexRing { current: self + HexDirection::DownLeft.to_vector() * radius, direction: HexDirection::Right, - radius: usize_radius, - index: 0, + radius, + index: T::ZERO, } } /// Returns the hexagonal spiral of the given radius. pub fn spiral(self, radius: T) -> HexSpiral { - let usize_radius = radius.as_(); HexSpiral { origin: self, current: self.ring(T::ONE), - radius: usize_radius, - index: 0, + radius, + index: T::ZERO, } } }