From 7c7993821212015ef80466840acbf495041db629 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Fri, 16 Feb 2024 01:52:18 +0100 Subject: [PATCH] WIP save --- crates/border-wars/src/map/hex.rs | 39 +++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/crates/border-wars/src/map/hex.rs b/crates/border-wars/src/map/hex.rs index 6f24a0e..55647f2 100644 --- a/crates/border-wars/src/map/hex.rs +++ b/crates/border-wars/src/map/hex.rs @@ -1,8 +1,8 @@ //! All functions related to calculations in a hexagonal grid. -use std::ops::{ +use std::{collections::HashSet, ops::{ Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign, -}; +}}; use paste::paste; @@ -59,6 +59,12 @@ pub trait Number: /// Converts an `usize` to `Self`. fn from_usize(value: usize) -> Self; + + /// Converts `self` to an `f32`. + fn to_f32(self) -> f32; + + /// Converts an `f32` to `Self`. + fn from_f32(value: f32) -> Self; } /// Implements the `Number` trait for the given types. @@ -79,6 +85,14 @@ macro_rules! number_impl { value as $t } + fn to_f32(self) -> f32 { + self as f32 + } + + fn from_f32(value: f32) -> Self { + value as $t + } + } )*}}; @@ -220,12 +234,33 @@ impl HexPosition { Self(x, y) } + /// Returns the pixel coordinates of the hexagonal position. + pub fn to_pixel_coordinates(&self, size: (f32, f32)) -> (f32, f32) { + ( + size.0 + * 3f32 + .sqrt() + .mul_add(T::to_f32(self.0), 3f32.sqrt() / 2.0 * T::to_f32(self.0)), + size.1 * (3.0 / 2.0 * T::to_f32(self.1)), + ) + } + /// Returns the distance between two hexagonal positions. pub fn distance(self, other: Self) -> T { let Self(x, y) = self - other; x.abs() + y.abs() + (x + y).abs() / T::TWO } + pub fn range(&self, range: T) -> HashSet { + let mut result_positions = HashSet::new(); + for q in num::range_inclusive(-range, range) { + for r in num::range_inclusive((-range, -q - range), min(range, -q + range)) { + result_positions.insert(Self { q, r }); + } + } + result_positions + } + /// Returns the hexagonal ring of the given radius. pub fn ring(self, radius: usize) -> HexRing { HexRing {