Skip to content

Commit

Permalink
✨ add translate() function
Browse files Browse the repository at this point in the history
It translates a hex or partial cube coordinates to another "position".
  • Loading branch information
flauwekeul committed Aug 2, 2022
1 parent f74f0d9 commit 0db7e22
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
13 changes: 2 additions & 11 deletions examples/line-of-sight/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import {
assertCubeCoordinates,
createHex,
createHexPrototype,
CubeCoordinates,
fromCoordinates,
Grid,
HexCoordinates,
hexToPoint,
line,
PartialCubeCoordinates,
repeatWith,
ring,
translate,
Traverser,
TupleCoordinates,
} from '../../src'
Expand Down Expand Up @@ -73,7 +72,7 @@ function fieldOfView(start: HexCoordinates): Traverser<Tile> {
return repeatWith(
ring({
center: start,
start: translate(startTile, { r: -config.viewDistanceInTiles }),
start: translate(startTile, { q: startTile.q, r: -config.viewDistanceInTiles }),
}),
lineOfSight(start),
{ includeSource: false },
Expand Down Expand Up @@ -104,11 +103,3 @@ function coordinatesFromTarget(target: EventTarget) {
const id = SVG(target).parent('[data-id]')?.data('id')
return typeof id === 'string' ? (id.split(',').map(Number) as TupleCoordinates) : null
}

// todo: move to honeycomb
// todo: rename to add?
function translate(hex: PartialCubeCoordinates, delta: Partial<CubeCoordinates>) {
const { q = 0, r = 0, s = 0 } = delta
const hexS = hex.s ?? -hex.q - hex.r
return { q: hex.q + q, r: hex.r + r, s: hexS + s }
}
1 change: 1 addition & 0 deletions src/hex/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export * from './offsetToCube'
export * from './pointToCube'
export * from './round'
export * from './toString'
export * from './translate'
export * from './width'
25 changes: 25 additions & 0 deletions src/hex/functions/translate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { CubeCoordinates, Hex, PartialCubeCoordinates } from '../types'
import { assertCubeCoordinates } from './assertCubeCoordinates'
import { completeCubeCoordinates } from './completeCubeCoordinates'
import { isHex } from './isHex'

// todo: add to hex prototype
/**
* @category Hex
*/
export function translate<T extends Hex>(hex: T, delta: PartialCubeCoordinates): T
export function translate(coordinates: PartialCubeCoordinates, delta: PartialCubeCoordinates): CubeCoordinates
export function translate<T extends Hex>(
input: T | PartialCubeCoordinates,
delta: PartialCubeCoordinates,
): T | CubeCoordinates {
const { q: deltaQ, r: deltaR, s: deltaS } = completeCubeCoordinates(delta)

if (isHex(input)) {
const { q, r, s } = assertCubeCoordinates(input, input)
return input.clone({ q: q + deltaQ, r: r + deltaR, s: s + deltaS })
}

const { q, r, s } = completeCubeCoordinates(input)
return { q: q + deltaQ, r: r + deltaR, s: s + deltaS }
}

0 comments on commit 0db7e22

Please sign in to comment.