-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grid): improve typing of Grid methods and traverse commands
The Grid and traverse commands now work with a type parameter to support custom hex types
- Loading branch information
1 parent
b7fcaf5
commit 193531d
Showing
9 changed files
with
49 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { AxialCoordinates } from '../hex' | ||
|
||
export const DIRECTION_COORDINATES: AxialCoordinates[] = [ | ||
{ q: 1, r: 0 }, | ||
{ q: 0, r: 1 }, | ||
{ q: -1, r: 1 }, | ||
{ q: -1, r: 0 }, | ||
{ q: 0, r: -1 }, | ||
{ q: 1, r: -1 }, | ||
] |
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,9 @@ | ||
import { HexCoordinates } from '../../hex' | ||
import { createHex, Hex, HexCoordinates } from '../../hex' | ||
|
||
export const at = (hex: HexCoordinates) => | ||
function* next() { | ||
yield hex | ||
export const at = <T extends Hex>(coordinates: HexCoordinates) => | ||
function* next(hex: T) { | ||
// todo: make createHex accept hex instances or use cloneHex()? | ||
yield createHex(Object.getPrototypeOf(hex), coordinates) | ||
} | ||
|
||
export const start = at |
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,10 @@ | ||
import { HexCoordinates } from '../../hex' | ||
import { createHex, Hex } from '../../hex' | ||
import { DIRECTION_COORDINATES } from '../constants' | ||
import { FlatCompassDirection, PointyCompassDirection } from '../types' | ||
|
||
export const move = (direction: PointyCompassDirection | FlatCompassDirection) => | ||
function* next({ q, r }: HexCoordinates) { | ||
console.warn('fixme: this always returns the same hex coordinate') | ||
yield { q: q + 1, r } | ||
export const move = <T extends Hex>(direction: PointyCompassDirection | FlatCompassDirection) => | ||
function* next(hex: T) { | ||
const { q, r } = DIRECTION_COORDINATES[direction] | ||
// todo: make createHex accept hex instances or use cloneHex()? | ||
yield createHex(Object.getPrototypeOf(hex), { q: hex.q + q, r: hex.r + r }) | ||
} |
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
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,3 +1,4 @@ | ||
export * from './constants' | ||
export * from './functions' | ||
export * from './grid' | ||
export * from './types' |
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