-
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.
Also change type signatures of `pointToCube()`, `offsetToCube()`, `toCube()` and `distance()` so that they require (part of) hex settings instead of the prototype. Closes #86.
- Loading branch information
1 parent
f7f287f
commit 97e2838
Showing
12 changed files
with
144 additions
and
99 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 |
---|---|---|
@@ -1,8 +1,7 @@ | ||
import { expect, test } from 'vitest' | ||
import { Hex } from '../../hex' | ||
import { defaultHexSettings } from '../../hex' | ||
import { distance } from './distance' | ||
|
||
test('returns the number of hexes between the passed 2 hexes (excluding the last hex)', () => { | ||
const hexPrototype = Hex.prototype | ||
expect(distance(hexPrototype, { q: 1, r: 3 }, { q: 8, r: 7 })).toBe(11) | ||
expect(distance(defaultHexSettings, { q: 1, r: 3 }, { q: 8, r: 7 })).toBe(11) | ||
}) |
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 |
---|---|---|
@@ -1,7 +1,11 @@ | ||
import { Hex, HexCoordinates, toCube } from '../../hex' | ||
import { HexCoordinates, HexSettings, toCube } from '../../hex' | ||
|
||
export function distance(hex: Pick<Hex, 'offset' | 'isPointy'>, from: HexCoordinates, to: HexCoordinates) { | ||
const { q: fromQ, r: fromR, s: fromS } = toCube(hex, from) | ||
const { q: toQ, r: toR, s: toS } = toCube(hex, to) | ||
export function distance( | ||
hexSettings: Pick<HexSettings, 'offset' | 'orientation'>, | ||
from: HexCoordinates, | ||
to: HexCoordinates, | ||
) { | ||
const { q: fromQ, r: fromR, s: fromS } = toCube(hexSettings, from) | ||
const { q: toQ, r: toR, s: toS } = toCube(hexSettings, to) | ||
return Math.max(Math.abs(fromQ - toQ), Math.abs(fromR - toR), Math.abs(fromS - toS)) | ||
} |
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 |
---|---|---|
@@ -1,14 +1,35 @@ | ||
import { expect, test } from 'vitest' | ||
import { Orientation } from '../types' | ||
import { offsetToCube } from './offsetToCube' | ||
|
||
test('returns axial coordinates bases on the passed offset coordinates', () => { | ||
expect(offsetToCube({ offset: -1, isPointy: true }, { col: 1, row: 4 })).toEqual({ q: -1, r: 4, s: -3 }) | ||
expect(offsetToCube({ offset: 1, isPointy: true }, { col: 1, row: 4 })).toEqual({ q: -1, r: 4, s: -3 }) | ||
expect(offsetToCube({ offset: -1, isPointy: false }, { col: 1, row: 4 })).toEqual({ q: 1, r: 4, s: -5 }) | ||
expect(offsetToCube({ offset: 1, isPointy: false }, { col: 1, row: 4 })).toEqual({ q: 1, r: 3, s: -4 }) | ||
expect(offsetToCube({ offset: -1, orientation: Orientation.POINTY }, { col: 1, row: 4 })).toEqual({ | ||
q: -1, | ||
r: 4, | ||
s: -3, | ||
}) | ||
expect(offsetToCube({ offset: 1, orientation: Orientation.POINTY }, { col: 1, row: 4 })).toEqual({ | ||
q: -1, | ||
r: 4, | ||
s: -3, | ||
}) | ||
expect(offsetToCube({ offset: -1, orientation: Orientation.FLAT }, { col: 1, row: 4 })).toEqual({ q: 1, r: 4, s: -5 }) | ||
expect(offsetToCube({ offset: 1, orientation: Orientation.FLAT }, { col: 1, row: 4 })).toEqual({ q: 1, r: 3, s: -4 }) | ||
|
||
expect(offsetToCube({ offset: -1, isPointy: true }, { col: 4, row: 1 })).toEqual({ q: 4, r: 1, s: -5 }) | ||
expect(offsetToCube({ offset: 1, isPointy: true }, { col: 4, row: 1 })).toEqual({ q: 3, r: 1, s: -4 }) | ||
expect(offsetToCube({ offset: -1, isPointy: false }, { col: 4, row: 1 })).toEqual({ q: 4, r: -1, s: -3 }) | ||
expect(offsetToCube({ offset: 1, isPointy: false }, { col: 4, row: 1 })).toEqual({ q: 4, r: -1, s: -3 }) | ||
expect(offsetToCube({ offset: -1, orientation: Orientation.POINTY }, { col: 4, row: 1 })).toEqual({ | ||
q: 4, | ||
r: 1, | ||
s: -5, | ||
}) | ||
expect(offsetToCube({ offset: 1, orientation: Orientation.POINTY }, { col: 4, row: 1 })).toEqual({ | ||
q: 3, | ||
r: 1, | ||
s: -4, | ||
}) | ||
expect(offsetToCube({ offset: -1, orientation: Orientation.FLAT }, { col: 4, row: 1 })).toEqual({ | ||
q: 4, | ||
r: -1, | ||
s: -3, | ||
}) | ||
expect(offsetToCube({ offset: 1, orientation: Orientation.FLAT }, { col: 4, row: 1 })).toEqual({ q: 4, r: -1, s: -3 }) | ||
}) |
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
Oops, something went wrong.