From 2862786a8b26a78ef54d1c402044bdfd085c3862 Mon Sep 17 00:00:00 2001 From: Bread <73413659+bread42@users.noreply.github.com> Date: Wed, 6 Dec 2023 20:46:59 +1100 Subject: [PATCH] added new function to all relevant structs --- src/helpers/hex_grid/axial.rs | 8 ++++++++ src/helpers/hex_grid/cube.rs | 8 ++++++++ src/helpers/hex_grid/offset.rs | 16 ++++++++++++++++ src/helpers/square_grid/diamond.rs | 4 ++++ src/helpers/square_grid/mod.rs | 4 ++++ src/helpers/square_grid/staggered.rs | 4 ++++ src/map/mod.rs | 26 ++++++++++++++++++++++++++ 7 files changed, 70 insertions(+) diff --git a/src/helpers/hex_grid/axial.rs b/src/helpers/hex_grid/axial.rs index 100c8fbc..b2e39726 100644 --- a/src/helpers/hex_grid/axial.rs +++ b/src/helpers/hex_grid/axial.rs @@ -212,6 +212,10 @@ pub const UNIT_R: AxialPos = AxialPos { q: 0, r: -1 }; pub const UNIT_S: AxialPos = AxialPos { q: 1, r: -1 }; impl AxialPos { + pub fn new(q: i32, r: i32) -> Self { + Self { q, r } + } + /// The magnitude of an axial position is its distance away from `(0, 0)` in the hex grid. /// /// See the Red Blob Games article for a [helpful interactive diagram](https://www.redblobgames.com/grids/hexagons/#distances-cube). @@ -473,6 +477,10 @@ pub struct FractionalAxialPos { } impl FractionalAxialPos { + pub fn new(q: f32, r: f32) -> Self { + Self { q, r } + } + #[inline] fn round(&self) -> AxialPos { let frac_cube_pos = FractionalCubePos::from(*self); diff --git a/src/helpers/hex_grid/cube.rs b/src/helpers/hex_grid/cube.rs index 35f4ad23..9b77eb56 100644 --- a/src/helpers/hex_grid/cube.rs +++ b/src/helpers/hex_grid/cube.rs @@ -92,6 +92,10 @@ impl Mul for u32 { } impl CubePos { + pub fn new(q: i32, r: i32, s: i32) -> Self { + Self { q, r, s } + } + /// The magnitude of a cube position is its distance away from `[0, 0, 0]` in the cube grid. /// /// See the Red Blob Games article for a [helpful interactive diagram](https://www.redblobgames.com/grids/hexagons/#distances-cube). @@ -123,6 +127,10 @@ impl From for FractionalCubePos { } impl FractionalCubePos { + pub fn new(q: f32, r: f32, s: f32) -> Self { + Self { q, r, s } + } + /// Returns `self` rounded to a [`CubePos`] that contains `self`. This is particularly useful /// for determining the hex tile that this fractional position is in. #[inline] diff --git a/src/helpers/hex_grid/offset.rs b/src/helpers/hex_grid/offset.rs index 65ed54f3..2d239e68 100644 --- a/src/helpers/hex_grid/offset.rs +++ b/src/helpers/hex_grid/offset.rs @@ -13,6 +13,10 @@ pub struct RowOddPos { } impl RowOddPos { + pub fn new(q: i32, r: i32) -> Self { + Self { q, r } + } + /// Returns the position of this tile's center, in world space. #[inline] pub fn center_in_world(&self, grid_size: &TilemapGridSize) -> Vec2 { @@ -99,6 +103,10 @@ pub struct RowEvenPos { } impl RowEvenPos { + pub fn new(q: i32, r: i32) -> Self { + Self { q, r } + } + /// Returns the position of this tile's center, in world space. #[inline] pub fn center_in_world(&self, grid_size: &TilemapGridSize) -> Vec2 { @@ -185,6 +193,10 @@ pub struct ColOddPos { } impl ColOddPos { + pub fn new(q: i32, r: i32) -> Self { + Self { q, r } + } + /// Returns the position of this tile's center, in world space. #[inline] pub fn center_in_world(&self, grid_size: &TilemapGridSize) -> Vec2 { @@ -271,6 +283,10 @@ pub struct ColEvenPos { } impl ColEvenPos { + pub fn new(q: i32, r: i32) -> Self { + Self { q, r } + } + /// Returns the position of this tile's center, in world space. #[inline] pub fn center_in_world(&self, grid_size: &TilemapGridSize) -> Vec2 { diff --git a/src/helpers/square_grid/diamond.rs b/src/helpers/square_grid/diamond.rs index fa54f108..6ba2500d 100644 --- a/src/helpers/square_grid/diamond.rs +++ b/src/helpers/square_grid/diamond.rs @@ -122,6 +122,10 @@ impl From<&SquarePos> for DiamondPos { } impl DiamondPos { + pub fn new(x: i32, y: i32) -> Self { + Self { x, y } + } + /// Project a vector representing a fractional tile position (i.e. the components can be `f32`) /// into world space. /// diff --git a/src/helpers/square_grid/mod.rs b/src/helpers/square_grid/mod.rs index 786c5412..dd1fd861 100644 --- a/src/helpers/square_grid/mod.rs +++ b/src/helpers/square_grid/mod.rs @@ -100,6 +100,10 @@ impl From for SquarePos { } impl SquarePos { + pub fn new(x: i32, y: i32) -> Self { + Self { x, y } + } + /// Project a vector representing a fractional tile position (i.e. the components can be `f32`) /// into world space. /// diff --git a/src/helpers/square_grid/staggered.rs b/src/helpers/square_grid/staggered.rs index e7468399..ef5ab0f4 100644 --- a/src/helpers/square_grid/staggered.rs +++ b/src/helpers/square_grid/staggered.rs @@ -89,6 +89,10 @@ impl Mul for i32 { } impl StaggeredPos { + pub fn new(x: i32, y: i32) -> Self { + Self { x, y } + } + /// Returns the position of this tile's center, in world space. #[inline] pub fn center_in_world(&self, grid_size: &TilemapGridSize) -> Vec2 { diff --git a/src/map/mod.rs b/src/map/mod.rs index 9b90475a..f6eae37f 100644 --- a/src/map/mod.rs +++ b/src/map/mod.rs @@ -59,6 +59,10 @@ pub struct TilemapSize { } impl TilemapSize { + pub fn new(x: u32, y: u32) -> Self { + Self { x, y } + } + pub fn count(&self) -> usize { (self.x * self.y) as usize } @@ -201,6 +205,12 @@ pub struct TilemapTileSize { pub y: f32, } +impl TilemapTileSize { + pub fn new(x: f32, y: f32) -> Self { + Self { x, y } + } +} + impl From for TilemapGridSize { fn from(tile_size: TilemapTileSize) -> Self { TilemapGridSize { @@ -240,6 +250,12 @@ pub struct TilemapGridSize { pub y: f32, } +impl TilemapGridSize { + pub fn new(x: f32, y: f32) -> Self { + Self { x, y } + } +} + impl From for Vec2 { fn from(grid_size: TilemapGridSize) -> Self { Vec2::new(grid_size.x, grid_size.y) @@ -280,6 +296,10 @@ impl From for Vec2 { } impl TilemapSpacing { + pub fn new(x: f32, y: f32) -> Self { + Self { x, y } + } + pub fn zero() -> Self { Self { x: 0.0, y: 0.0 } } @@ -293,6 +313,12 @@ pub struct TilemapTextureSize { pub y: f32, } +impl TilemapTextureSize { + pub fn new(x: f32, y: f32) -> Self { + Self { x, y } + } +} + impl From for Vec2 { fn from(texture_size: TilemapTextureSize) -> Self { Vec2::new(texture_size.x, texture_size.y)