Skip to content

Commit

Permalink
added new function to all relevant structs
Browse files Browse the repository at this point in the history
  • Loading branch information
bread42 committed Dec 6, 2023
1 parent ac22adb commit 2862786
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/helpers/hex_grid/axial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/helpers/hex_grid/cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ impl Mul<CubePos> 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).
Expand Down Expand Up @@ -123,6 +127,10 @@ impl From<FractionalAxialPos> 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]
Expand Down
16 changes: 16 additions & 0 deletions src/helpers/hex_grid/offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/helpers/square_grid/diamond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
4 changes: 4 additions & 0 deletions src/helpers/square_grid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ impl From<StaggeredPos> 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.
///
Expand Down
4 changes: 4 additions & 0 deletions src/helpers/square_grid/staggered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ impl Mul<StaggeredPos> 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 {
Expand Down
26 changes: 26 additions & 0 deletions src/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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<TilemapTileSize> for TilemapGridSize {
fn from(tile_size: TilemapTileSize) -> Self {
TilemapGridSize {
Expand Down Expand Up @@ -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<TilemapGridSize> for Vec2 {
fn from(grid_size: TilemapGridSize) -> Self {
Vec2::new(grid_size.x, grid_size.y)
Expand Down Expand Up @@ -280,6 +296,10 @@ impl From<TilemapSpacing> 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 }
}
Expand All @@ -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<TilemapTextureSize> for Vec2 {
fn from(texture_size: TilemapTextureSize) -> Self {
Vec2::new(texture_size.x, texture_size.y)
Expand Down

0 comments on commit 2862786

Please sign in to comment.