-
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): add optional argument to move() traverser to move in the …
…same direction multiple times
- Loading branch information
1 parent
b242c94
commit f8b96ad
Showing
4 changed files
with
14 additions
and
53 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 |
---|---|---|
@@ -1,12 +1,6 @@ | ||
import { HexCoordinates } from '../../hex' | ||
import { Traverser } from '../types' | ||
|
||
// export const at = <T extends Hex>(coordinates: HexCoordinates): Traverser<T> => | ||
// function* next(currentHex) { | ||
// // todo: make createHex accept hex instances or use cloneHex()? | ||
// yield createHex(Object.getPrototypeOf(currentHex), coordinates) | ||
// } | ||
|
||
export const at = (coordinates: HexCoordinates): Traverser => () => [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,16 +1,15 @@ | ||
import { HexCoordinates } from '../../hex' | ||
import { DIRECTION_COORDINATES } from '../constants' | ||
import { CompassDirection, Traverser } from '../types' | ||
|
||
// todo: also accept a string and/or number for direction | ||
// export const move = <T extends Hex>(direction: CompassDirection): Traverser<T> => | ||
// function* next(currentHex) { | ||
// const { q, r } = DIRECTION_COORDINATES[direction] | ||
// const nextCoordinates = { q: currentHex.q + q, r: currentHex.r + r } | ||
// // todo: make createHex accept hex instances or use cloneHex()? | ||
// yield createHex(Object.getPrototypeOf(currentHex), nextCoordinates) | ||
// } | ||
|
||
export const move = (direction: CompassDirection): Traverser => { | ||
// todo: also accept a string and/or number for direction? | ||
export const move = (direction: CompassDirection, times = 1): Traverser => { | ||
const { q, r } = DIRECTION_COORDINATES[direction] | ||
return (currentCoordinates) => [{ q: currentCoordinates.q + q, r: currentCoordinates.r + r }] | ||
return (currentCoordinates) => { | ||
const result: HexCoordinates[] = [] | ||
for (let i = 1; i <= times; i++) { | ||
result.push({ q: currentCoordinates.q + q * i, r: currentCoordinates.r + r * i }) | ||
} | ||
return result | ||
} | ||
} |
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