-
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): update grid rectangle method to only within previous grid
- Loading branch information
1 parent
20655d1
commit b242c94
Showing
4 changed files
with
87 additions
and
35 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,50 +1,46 @@ | ||
import { createHex, CubeCoordinates, Hex, isPointy } from '../../hex' | ||
import { offsetFromZero, signedModulo } from '../../utils' | ||
import { CompassDirection, FlatCompassDirection, PointyCompassDirection } from '../types' | ||
import { RECTANGLE_DIRECTIONS } from '../constants' | ||
import { FlatCompassDirection, PointyCompassDirection, RectangleOptions } from '../types' | ||
|
||
export interface RectangleOptions { | ||
width: number | ||
height: number | ||
start?: CubeCoordinates | ||
direction?: CompassDirection | ||
} | ||
|
||
const DIRECTIONS = [ | ||
['q', 'r', 's'], | ||
['r', 'q', 's'], | ||
['r', 's', 'q'], | ||
['s', 'r', 'q'], | ||
['s', 'q', 'r'], | ||
['q', 's', 'r'], | ||
] as [keyof CubeCoordinates, keyof CubeCoordinates, keyof CubeCoordinates][] | ||
|
||
// todo: move this to Grid.rectangle()? | ||
export function* rectangle<T extends Hex>( | ||
export const rectangle = <T extends Hex>( | ||
hexPrototype: T, | ||
{ | ||
width, | ||
height, | ||
start = { q: 0, r: 0, s: 0 }, | ||
start = { q: 0, r: 0 }, | ||
direction = isPointy(hexPrototype) ? PointyCompassDirection.E : FlatCompassDirection.S, | ||
}: RectangleOptions, | ||
) { | ||
) => { | ||
const result: T[] = [] | ||
const _start: CubeCoordinates = { q: start.q, r: start.r, s: -start.q - start.r } | ||
// const hasTraversedBefore = this.traverser !== infiniteTraverser | ||
// const previousHexes = [...this.traverser()] | ||
// let coordinates: CubeCoordinates = previousHexes[previousHexes.length - 1] || { q: 0, r: 0 } | ||
|
||
if (direction < 0 || direction > 5) { | ||
direction = signedModulo(direction, 6) | ||
} | ||
|
||
const [firstCoordinate, secondCoordinate, thirdCoordinate] = DIRECTIONS[direction] | ||
const [firstCoordinate, secondCoordinate, thirdCoordinate] = RECTANGLE_DIRECTIONS[direction] | ||
const [firstStop, secondStop] = isPointy(hexPrototype) ? [width, height] : [height, width] | ||
|
||
for (let second = 0; second < secondStop; second++) { | ||
const secondOffset = offsetFromZero(hexPrototype.offset, second) | ||
|
||
for (let first = -secondOffset; first < firstStop - secondOffset; first++) { | ||
const nextCoordinates = { | ||
[firstCoordinate]: first + start[firstCoordinate], | ||
[secondCoordinate]: second + start[secondCoordinate], | ||
[thirdCoordinate]: -first - second + start[thirdCoordinate], | ||
[firstCoordinate]: first + _start[firstCoordinate], | ||
[secondCoordinate]: second + _start[secondCoordinate], | ||
[thirdCoordinate]: -first - second + _start[thirdCoordinate], | ||
} as unknown | ||
yield createHex(hexPrototype, nextCoordinates as CubeCoordinates) | ||
// coordinates = nextCoordinates as CubeCoordinates | ||
// if (hasTraversedBefore && !previousHexes.some((prevCoords) => equals(prevCoords, coordinates))) { | ||
// return result // todo: or continue? or make this configurable? | ||
// } | ||
result.push(createHex(hexPrototype, nextCoordinates as CubeCoordinates)) | ||
} | ||
} | ||
|
||
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
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