-
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): restrict Grid.traverse() to the hexes in the grid (from t…
…he previous iteration, if any)
- Loading branch information
1 parent
193531d
commit c25249b
Showing
8 changed files
with
71 additions
and
37 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,9 +1,10 @@ | ||
import { createHex, Hex, HexCoordinates } from '../../hex' | ||
import { GridGeneratorFunction } from '../types' | ||
|
||
export const at = <T extends Hex>(coordinates: HexCoordinates) => | ||
function* next(hex: T) { | ||
export const at = <T extends Hex>(coordinates: HexCoordinates): GridGeneratorFunction<T> => | ||
function* (currentHex) { | ||
// todo: make createHex accept hex instances or use cloneHex()? | ||
yield createHex(Object.getPrototypeOf(hex), coordinates) | ||
yield createHex(Object.getPrototypeOf(currentHex), 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,10 +1,13 @@ | ||
import { createHex, Hex } from '../../hex' | ||
import { DIRECTION_COORDINATES } from '../constants' | ||
import { FlatCompassDirection, PointyCompassDirection } from '../types' | ||
import { FlatCompassDirection, GridGeneratorFunction, PointyCompassDirection } from '../types' | ||
|
||
export const move = <T extends Hex>(direction: PointyCompassDirection | FlatCompassDirection) => | ||
function* next(hex: T) { | ||
export const move = <T extends Hex>( | ||
direction: PointyCompassDirection | FlatCompassDirection, | ||
): GridGeneratorFunction<T> => | ||
function* (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(hex), { q: hex.q + q, r: hex.r + r }) | ||
yield createHex(Object.getPrototypeOf(currentHex), nextCoordinates) | ||
} |
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,16 @@ | ||
import { Hex } from '../../hex' | ||
import { GridGenerator } from '../types' | ||
import { GridGeneratorFunction } from '../types' | ||
|
||
export const repeat = <T extends Hex>(amount: number, command: (hex: T) => GridGenerator<T>) => | ||
function* next(hex: T) { | ||
// todo: looks a lot like Grid.traverse() | ||
export const repeat = <T extends Hex>(amount: number, command: GridGeneratorFunction<T>): GridGeneratorFunction<T> => | ||
function* (currentHex) { | ||
let nextHex = currentHex | ||
for (let i = 0; i < amount; i++) { | ||
const hexes = command(hex) | ||
for (hex of hexes) { | ||
const hexes = command(nextHex) | ||
for (const hex of hexes) { | ||
yield hex | ||
nextHex = hex | ||
} | ||
// todo: yield* command(grid, nextHex) | ||
} | ||
} |
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,3 @@ | ||
import { AxialCoordinates } from '../types' | ||
|
||
export const equals = (hexA: AxialCoordinates, hexB: AxialCoordinates) => hexA.q === hexB.q && hexA.r === hexB.r |
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