-
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): remove move() alias of line() and update signature of line()
line() now accepts an object with options: `direction`, `length`, `at` and `start`. The `direction` and `length` options are the same as before. The `at` option sets the cursor to the provided coordinates, but doesn't include it. `start` does the same as `at` but *does* include those coordinates. These `at` and `start` options will be added to most traversers. BREAKING CHANGE: Remove move() alias of line(). line() now accepts an object with options: `direction`, `length`, `at` and `start`.
- Loading branch information
1 parent
e63f650
commit 5205e93
Showing
5 changed files
with
58 additions
and
51 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
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,20 +1,29 @@ | ||
import { CompassDirection } from '../../compass' | ||
import { Hex } from '../../hex' | ||
import { Hex, HexCoordinates } from '../../hex' | ||
import { neighborOf } from '../functions' | ||
import { Traverser } from '../types' | ||
|
||
export const line = <T extends Hex>(direction: CompassDirection, length = 1): Traverser<T> => { | ||
export const line = <T extends Hex>({ direction, start, at, length = 1 }: LineOptions): Traverser<T> => { | ||
return (cursor, getHex) => { | ||
const result: T[] = [] | ||
let _cursor = cursor | ||
// todo: these 3 lines should be moved to a helper? e.g.: `const { hexes, cursor } = getInitialTraverserState(options)` | ||
const startHex = start ? getHex(start) : null | ||
const hexes: T[] = startHex ? [startHex] : [] | ||
let _cursor = startHex ?? (at ? getHex(at) : cursor) | ||
|
||
for (let i = 1; i <= length; i++) { | ||
_cursor = getHex(neighborOf(_cursor, direction)) | ||
result.push(_cursor) | ||
hexes.push(_cursor) | ||
} | ||
|
||
return result | ||
return hexes | ||
} | ||
} | ||
|
||
export const move = line | ||
// todo: probably extend from something that has `start` or `at` or neither (XOR) | ||
// https://stackoverflow.com/a/53229567/660260 | ||
export interface LineOptions { | ||
direction: CompassDirection | ||
start?: HexCoordinates | ||
at?: HexCoordinates | ||
length?: number | ||
} |
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