Skip to content

Commit

Permalink
Merge pull request #470 from Piturnah/feat/impl-from
Browse files Browse the repository at this point in the history
Implement `From<TilePos>` for `CubePos`
  • Loading branch information
StarArawn authored Dec 17, 2023
2 parents 3ee9862 + ac731f2 commit d6d8511
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/helpers/hex_grid/axial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,21 @@ pub struct AxialPos {
pub r: i32,
}

impl From<&TilePos> for AxialPos {
fn from(tile_pos: &TilePos) -> Self {
impl From<TilePos> for AxialPos {
fn from(tile_pos: TilePos) -> Self {
AxialPos {
q: tile_pos.x as i32,
r: tile_pos.y as i32,
}
}
}

impl From<&TilePos> for AxialPos {
fn from(tile_pos: &TilePos) -> Self {
AxialPos::from(*tile_pos)
}
}

impl From<CubePos> for AxialPos {
fn from(cube_pos: CubePos) -> Self {
let CubePos { q, r, .. } = cube_pos;
Expand Down
12 changes: 11 additions & 1 deletion src/helpers/hex_grid/cube.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Code for the cube coordinate system
use crate::helpers::hex_grid::axial::{AxialPos, FractionalAxialPos};
use crate::{
helpers::hex_grid::axial::{AxialPos, FractionalAxialPos},
tiles::TilePos,
};
use std::ops::{Add, Mul, Sub};

/// Identical to [`AxialPos`], but has an extra component `s`. Together, `q`, `r`, `s`
Expand Down Expand Up @@ -31,6 +34,13 @@ impl From<AxialPos> for CubePos {
}
}

impl From<TilePos> for CubePos {
#[inline]
fn from(tile_pos: TilePos) -> Self {
AxialPos::from(tile_pos).into()
}
}

impl Add<CubePos> for CubePos {
type Output = CubePos;

Expand Down

0 comments on commit d6d8511

Please sign in to comment.