-
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.
feat(grid): use single Compass enum, make move() accept ambiguous dir…
…ections, improve grid.rectangle A single compass with 8 directions is simpler, even though 2 directions are ambiguous (E and W for pointy hexes and S and N for flat hexes). The lib should solve this ambiguity. The move() traverser does this by converting to offset coordinates (only for these ambiguous directions). Finally, grid.rectangle() now uses grid.traverse(), removing some duplication.
- Loading branch information
1 parent
abf047f
commit 2bf8d1e
Showing
9 changed files
with
101 additions
and
63 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,6 +1,6 @@ | ||
import { HexCoordinates } from '../../hex' | ||
import { DefaultHexPrototype, HexCoordinates } from '../../hex' | ||
import { Traverser } from '../types' | ||
|
||
export const at = (cursor: HexCoordinates): Traverser => () => [cursor] | ||
export const at = <T extends DefaultHexPrototype>(cursor: HexCoordinates): Traverser<T> => () => [cursor] | ||
|
||
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
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,32 +1,23 @@ | ||
import { HexCoordinates } from '../hex' | ||
import { DefaultHexPrototype, HexCoordinates } from '../hex' | ||
|
||
export enum PointyCompassDirection { | ||
export enum Compass { | ||
E, | ||
SE, | ||
SW, | ||
W, | ||
NW, | ||
NE, | ||
} | ||
|
||
export enum FlatCompassDirection { | ||
SE, | ||
S, | ||
SW, | ||
W, | ||
NW, | ||
N, | ||
NE, | ||
} | ||
|
||
export type CompassDirection = PointyCompassDirection | FlatCompassDirection | ||
|
||
export interface Traverser { | ||
(cursor: HexCoordinates): Iterable<HexCoordinates> | ||
export interface Traverser<T extends DefaultHexPrototype> { | ||
(cursor: HexCoordinates, hexPrototype: T): Iterable<HexCoordinates> | ||
} | ||
|
||
export interface RectangleOptions { | ||
width: number | ||
height: number | ||
start?: HexCoordinates | ||
direction?: CompassDirection | ||
direction?: Compass | ||
} |
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,2 +1,3 @@ | ||
// todo: rename (also rename offset)? | ||
// todo: change to https://www.redblobgames.com/grids/hexagons/#conversions-offset | ||
export const offsetFromZero = (offset: number, distance: number) => (distance + offset * (distance & 1)) >> 1 |