-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It translates a hex or partial cube coordinates to another "position".
- Loading branch information
1 parent
f74f0d9
commit 0db7e22
Showing
3 changed files
with
28 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |