-
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.
Like most traversers it accepts an `at` and `start` option and a `radius` and `rotation` options. It always starts at the cursor (or `at`/`start`) and spirals outward until the required radius is reached (in Northern direction of the first hex).
- Loading branch information
1 parent
f5a2d6f
commit d433af0
Showing
5 changed files
with
138 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { createHex, createHexPrototype } from '../../hex' | ||
import { spiral } from './spiral' | ||
|
||
const hexPrototype = createHexPrototype() | ||
const getHex = jest.fn((coordinates) => createHex(hexPrototype, coordinates)) | ||
const cursor = createHex(hexPrototype, { q: 1, r: 2 }) | ||
|
||
describe('when called with a radius', () => { | ||
test('returns a traverser that returns hexes in a spiral around but excluding the cursor', () => { | ||
expect(spiral({ radius: 2 })(cursor, getHex)).toEqual([ | ||
cursor.clone({ q: 2, r: 1 }), | ||
cursor.clone({ q: 2, r: 2 }), | ||
cursor.clone({ q: 1, r: 3 }), | ||
cursor.clone({ q: 0, r: 3 }), | ||
cursor.clone({ q: 0, r: 2 }), | ||
cursor.clone({ q: 1, r: 1 }), | ||
cursor.clone({ q: 2, r: 0 }), | ||
cursor.clone({ q: 3, r: 0 }), | ||
cursor.clone({ q: 3, r: 1 }), | ||
cursor.clone({ q: 3, r: 2 }), | ||
cursor.clone({ q: 2, r: 3 }), | ||
cursor.clone({ q: 1, r: 4 }), | ||
cursor.clone({ q: 0, r: 4 }), | ||
cursor.clone({ q: -1, r: 4 }), | ||
cursor.clone({ q: -1, r: 3 }), | ||
cursor.clone({ q: -1, r: 2 }), | ||
cursor.clone({ q: 0, r: 1 }), | ||
cursor.clone({ q: 1, r: 0 }), | ||
]) | ||
}) | ||
}) | ||
|
||
describe('when called with at coordinates', () => { | ||
test('returns a traverser that returns hexes in a spiral around but excluding the "at" coordinates', () => { | ||
expect(spiral({ at: { q: 1, r: 3 }, radius: 2 })(cursor, getHex)).toEqual([ | ||
cursor.clone({ q: 1, r: 2 }), | ||
cursor.clone({ q: 2, r: 2 }), | ||
cursor.clone({ q: 2, r: 3 }), | ||
cursor.clone({ q: 1, r: 4 }), | ||
cursor.clone({ q: 0, r: 4 }), | ||
cursor.clone({ q: 0, r: 3 }), | ||
cursor.clone({ q: 2, r: 1 }), | ||
cursor.clone({ q: 3, r: 1 }), | ||
cursor.clone({ q: 3, r: 2 }), | ||
cursor.clone({ q: 3, r: 3 }), | ||
cursor.clone({ q: 2, r: 4 }), | ||
cursor.clone({ q: 1, r: 5 }), | ||
cursor.clone({ q: 0, r: 5 }), | ||
cursor.clone({ q: -1, r: 5 }), | ||
cursor.clone({ q: -1, r: 4 }), | ||
cursor.clone({ q: -1, r: 3 }), | ||
cursor.clone({ q: 0, r: 2 }), | ||
cursor.clone({ q: 1, r: 1 }), | ||
]) | ||
}) | ||
}) | ||
|
||
describe('when called with start coordinates', () => { | ||
test('returns a traverser that returns hexes in a spiral around and including the "start" coordinates', () => { | ||
expect(spiral({ start: { q: 2, r: 1 }, radius: 2 })(cursor, getHex)).toEqual([ | ||
cursor.clone({ q: 2, r: 1 }), | ||
cursor.clone({ q: 2, r: 0 }), | ||
cursor.clone({ q: 3, r: 0 }), | ||
cursor.clone({ q: 3, r: 1 }), | ||
cursor.clone({ q: 2, r: 2 }), | ||
cursor.clone({ q: 1, r: 2 }), | ||
cursor.clone({ q: 1, r: 1 }), | ||
cursor.clone({ q: 3, r: -1 }), | ||
cursor.clone({ q: 4, r: -1 }), | ||
cursor.clone({ q: 4, r: 0 }), | ||
cursor.clone({ q: 4, r: 1 }), | ||
cursor.clone({ q: 3, r: 2 }), | ||
cursor.clone({ q: 2, r: 3 }), | ||
cursor.clone({ q: 1, r: 3 }), | ||
cursor.clone({ q: 0, r: 3 }), | ||
cursor.clone({ q: 0, r: 2 }), | ||
cursor.clone({ q: 0, r: 1 }), | ||
cursor.clone({ q: 1, r: 0 }), | ||
cursor.clone({ q: 2, r: -1 }), | ||
]) | ||
}) | ||
}) | ||
|
||
describe('when called with a counterclockwise rotation', () => { | ||
test('returns a traverser that returns hexes in a spiral around the cursor in a counterclockwise rotation', () => { | ||
expect(spiral({ radius: 2, rotation: 'counterclockwise' })(cursor, getHex)).toEqual([ | ||
cursor.clone({ q: 2, r: 1 }), | ||
cursor.clone({ q: 1, r: 1 }), | ||
cursor.clone({ q: 0, r: 2 }), | ||
cursor.clone({ q: 0, r: 3 }), | ||
cursor.clone({ q: 1, r: 3 }), | ||
cursor.clone({ q: 2, r: 2 }), | ||
cursor.clone({ q: 2, r: 0 }), | ||
cursor.clone({ q: 1, r: 0 }), | ||
cursor.clone({ q: 0, r: 1 }), | ||
cursor.clone({ q: -1, r: 2 }), | ||
cursor.clone({ q: -1, r: 3 }), | ||
cursor.clone({ q: -1, r: 4 }), | ||
cursor.clone({ q: 0, r: 4 }), | ||
cursor.clone({ q: 1, r: 4 }), | ||
cursor.clone({ q: 2, r: 3 }), | ||
cursor.clone({ q: 3, r: 2 }), | ||
cursor.clone({ q: 3, r: 1 }), | ||
cursor.clone({ q: 3, r: 0 }), | ||
]) | ||
}) | ||
}) |
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,24 @@ | ||
import { CompassDirection } from '../../compass' | ||
import { Hex, HexCoordinates } from '../../hex' | ||
import { Rotation, Traverser } from '../types' | ||
import { branch } from './branch' | ||
import { line } from './line' | ||
import { ring } from './ring' | ||
|
||
export const spiral = <T extends Hex>({ radius, start, at, rotation }: SpiralOptions): Traverser<T, T[]> => ( | ||
cursor, | ||
getHex, | ||
) => { | ||
const center = start ? getHex(start) : at ? getHex(at) : cursor | ||
return branch<T>(line({ start, at, direction: CompassDirection.N, length: radius }), ring({ center, rotation }))( | ||
getHex(center), | ||
getHex, | ||
) | ||
} | ||
|
||
export interface SpiralOptions { | ||
radius: number | ||
start?: HexCoordinates | ||
at?: HexCoordinates | ||
rotation?: Rotation | 'CLOCKWISE' | 'clockwise' | 'COUNTERCLOCKWISE' | 'counterclockwise' | ||
} |
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