From 6fb4996b8cbe3373d27e84a9033f9a853cb557eb Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Fri, 16 Feb 2024 01:41:31 +0100 Subject: [PATCH] add doc --- crates/border-wars/src/map/hex.rs | 37 ++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/crates/border-wars/src/map/hex.rs b/crates/border-wars/src/map/hex.rs index 4bb867c..6f24a0e 100644 --- a/crates/border-wars/src/map/hex.rs +++ b/crates/border-wars/src/map/hex.rs @@ -4,8 +4,6 @@ use std::ops::{ Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign, }; -use num::cast::AsPrimitive; -use num::FromPrimitive; use paste::paste; /// Represents a number that can be used in calculations for hexagonal grids. @@ -59,9 +57,11 @@ pub trait Number: /// Converts `self` to an `usize`. fn to_usize(self) -> usize; + /// Converts an `usize` to `Self`. fn from_usize(value: usize) -> Self; } +/// Implements the `Number` trait for the given types. macro_rules! number_impl { ($($t:ty,)*) => {paste!{$( impl Number for $t { @@ -117,23 +117,30 @@ pub enum HexDirection { impl HexDirection { /// Returns the vector of the direction. - pub fn to_vector(self) -> HexPosition { + pub const fn to_vector(self) -> HexPosition { match self { - HexDirection::Right => HexPosition(T::ONE, T::ZERO), - HexDirection::UpRight => HexPosition(T::ONE, T::MINUS_ONE), - HexDirection::UpLeft => HexPosition(T::ZERO, T::MINUS_ONE), - HexDirection::Left => HexPosition(T::MINUS_ONE, T::ZERO), - HexDirection::DownLeft => HexPosition(T::MINUS_ONE, T::ONE), - HexDirection::DownRight => HexPosition(T::ZERO, T::ONE), + Self::Right => HexPosition(T::ONE, T::ZERO), + Self::UpRight => HexPosition(T::ONE, T::MINUS_ONE), + Self::UpLeft => HexPosition(T::ZERO, T::MINUS_ONE), + Self::Left => HexPosition(T::MINUS_ONE, T::ZERO), + Self::DownLeft => HexPosition(T::MINUS_ONE, T::ONE), + Self::DownRight => HexPosition(T::ZERO, T::ONE), } } } /// A hexagonal ring iterator. pub struct HexRing { + /// The current position in the ring. current: HexPosition, + + /// The direction of the current position to the next in the ring. direction: HexDirection, + + /// The radius of the ring. radius: usize, + + /// The index of the current position in the ring. index: usize, } @@ -173,9 +180,16 @@ impl Iterator for HexRing { /// A hexagonal spiral iterator. pub struct HexSpiral { + /// The origin of the spiral. origin: HexPosition, + + /// The current ring of the spiral. current: HexRing, + + /// The radius of the spiral. radius: usize, + + /// The index of the current ring in the spiral. index: usize, } @@ -202,7 +216,7 @@ impl Iterator for HexSpiral { impl HexPosition { /// Creates a new hexagonal position. - pub fn new(x: T, y: T) -> Self { + pub const fn new(x: T, y: T) -> Self { Self(x, y) } @@ -215,7 +229,7 @@ impl HexPosition { /// Returns the hexagonal ring of the given radius. pub fn ring(self, radius: usize) -> HexRing { HexRing { - current: self + HexDirection::DownLeft.to_vector() * radius.from_usize(), + current: self + HexDirection::DownLeft.to_vector() * T::from_usize(radius), direction: HexDirection::Right, radius, index: 0, @@ -233,6 +247,7 @@ impl HexPosition { } } +/// Implementation of the arithmetic operators for hexagonal positions. macro_rules! impl_ops { ($(($t:ty, $n:ident),)*) => {paste!{$( impl $t for HexPosition {