Skip to content

Commit

Permalink
feat(hex): rename offsetToAxial() to offsetToCube() and make it retur…
Browse files Browse the repository at this point in the history
…n cube coordinates

This is more in line with the pointToCube() function and hey, why not throw in a free s coordinate?

BREAKING CHANGE: offsetToAxial() is now offsetToCube()
  • Loading branch information
flauwekeul committed Apr 22, 2021
1 parent d6e24b4 commit 52d89f8
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 38 deletions.
6 changes: 3 additions & 3 deletions src/grid/functions/neighborOf.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CompassDirection } from '../../compass'
import { AxialCoordinates, Hex, offsetToAxialFlat, offsetToAxialPointy } from '../../hex'
import { AxialCoordinates, Hex, offsetToCubeFlat, offsetToCubePointy } from '../../hex'

const DIRECTIONS_POINTY = [
null, // ambiguous
Expand All @@ -25,7 +25,7 @@ const DIRECTIONS_FLAT = [
export const neighborOfPointy = <T extends Hex>({ offset, q, r, col, row }: T, direction: CompassDirection) => {
if (direction === CompassDirection.S || direction === CompassDirection.N) {
const nextRow = direction === CompassDirection.S ? row + 1 : row - 1
return offsetToAxialPointy(col, nextRow, offset)
return offsetToCubePointy(col, nextRow, offset)
}
const neighbor = DIRECTIONS_POINTY[direction]
return { q: q + neighbor.q, r: r + neighbor.r }
Expand All @@ -34,7 +34,7 @@ export const neighborOfPointy = <T extends Hex>({ offset, q, r, col, row }: T, d
export const neighborOfFlat = <T extends Hex>({ offset, q, r, col, row }: T, direction: CompassDirection) => {
if (direction === CompassDirection.E || direction === CompassDirection.W) {
const nextCol = direction === CompassDirection.E ? col + 1 : col - 1
return offsetToAxialFlat(nextCol, row, offset)
return offsetToCubeFlat(nextCol, row, offset)
}
const neighbor = DIRECTIONS_FLAT[direction]
return { q: q + neighbor.q, r: r + neighbor.r }
Expand Down
4 changes: 2 additions & 2 deletions src/hex/functions/cloneHex.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { isOffset } from '../../utils'
import { Hex, HexCoordinates } from '../types'
import { offsetToAxial } from './offsetToAxial'
import { offsetToCube } from './offsetToCube'

export const cloneHex = <T extends Hex>(hex: T, newProps: Partial<T> | HexCoordinates = {}): T => {
if (isOffset(newProps)) {
const { col, row, ...otherProps } = newProps
const coordinates = offsetToAxial({ col, row }, hex)
const coordinates = offsetToCube({ col, row }, hex)
return Object.assign(Object.create(Object.getPrototypeOf(hex)), hex, coordinates, otherProps)
}

Expand Down
4 changes: 2 additions & 2 deletions src/hex/functions/createHex.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isOffset } from '../../utils'
import { Hex, HexCoordinates } from '../types'
import { isHex } from './isHex'
import { offsetToAxial } from './offsetToAxial'
import { offsetToCube } from './offsetToCube'

export const createHex = <T extends Hex>(prototypeOrHex: T, props: Partial<T> | HexCoordinates = { q: 0, r: 0 }): T => {
if (isHex(prototypeOrHex)) {
Expand All @@ -10,7 +10,7 @@ export const createHex = <T extends Hex>(prototypeOrHex: T, props: Partial<T> |

if (isOffset(props)) {
const { col, row, ...otherProps } = props
const coordinates = offsetToAxial({ col, row }, prototypeOrHex)
const coordinates = offsetToCube({ col, row }, prototypeOrHex)
return Object.assign(Object.create(prototypeOrHex), coordinates, otherProps)
}

Expand Down
2 changes: 1 addition & 1 deletion src/hex/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export * from './hexToPoint'
export * from './isFlat'
export * from './isHex'
export * from './isPointy'
export * from './offsetToAxial'
export * from './offsetToCube'
export * from './pointToCube'
export * from './round'
export * from './toString'
Expand Down
13 changes: 0 additions & 13 deletions src/hex/functions/offsetToAxial.test.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/hex/functions/offsetToAxial.ts

This file was deleted.

13 changes: 13 additions & 0 deletions src/hex/functions/offsetToCube.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { offsetToCube } from './offsetToCube'

test('returns axial coordinates bases on the passed offset coordinates', () => {
expect(offsetToCube({ col: 1, row: 4 }, { offset: -1, isPointy: true })).toEqual({ q: -1, r: 4, s: -3 })
expect(offsetToCube({ col: 1, row: 4 }, { offset: 1, isPointy: true })).toEqual({ q: -1, r: 4, s: -3 })
expect(offsetToCube({ col: 1, row: 4 }, { offset: -1, isPointy: false })).toEqual({ q: 1, r: 4, s: -5 })
expect(offsetToCube({ col: 1, row: 4 }, { offset: 1, isPointy: false })).toEqual({ q: 1, r: 3, s: -4 })

expect(offsetToCube({ col: 4, row: 1 }, { offset: -1, isPointy: true })).toEqual({ q: 4, r: 1, s: -5 })
expect(offsetToCube({ col: 4, row: 1 }, { offset: 1, isPointy: true })).toEqual({ q: 3, r: 1, s: -4 })
expect(offsetToCube({ col: 4, row: 1 }, { offset: -1, isPointy: false })).toEqual({ q: 4, r: -1, s: -3 })
expect(offsetToCube({ col: 4, row: 1 }, { offset: 1, isPointy: false })).toEqual({ q: 4, r: -1, s: -3 })
})
21 changes: 21 additions & 0 deletions src/hex/functions/offsetToCube.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { offsetFromZero } from '../../utils'
import { CubeCoordinates, HexPrototype, OffsetCoordinates } from '../types'

export const offsetToCubePointy = (col: number, row: number, offset: number): CubeCoordinates => {
const q = col - offsetFromZero(offset, row)
const r = row
const s = -q - r
return { q, r, s }
}

export const offsetToCubeFlat = (col: number, row: number, offset: number): CubeCoordinates => {
const q = col
const r = row - offsetFromZero(offset, col)
const s = -q - r
return { q, r, s }
}

export const offsetToCube = (
{ col, row }: OffsetCoordinates,
{ offset, isPointy }: Pick<HexPrototype, 'offset' | 'isPointy'>,
) => (isPointy ? offsetToCubePointy(col, row, offset) : offsetToCubeFlat(col, row, offset))

0 comments on commit 52d89f8

Please sign in to comment.