Skip to content

Commit

Permalink
feat(grid): add optional argument to move() traverser to move in the …
Browse files Browse the repository at this point in the history
…same direction multiple times
  • Loading branch information
flauwekeul committed Apr 22, 2021
1 parent b242c94 commit f8b96ad
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 53 deletions.
26 changes: 4 additions & 22 deletions playground/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ Grid.of(hexPrototype)
at({ q: 0, r: 0 }),
repeat(
2,
repeat(4, move(PointyCompassDirection.E)),
move(PointyCompassDirection.E, 4),
move(PointyCompassDirection.SE),
repeat(4, move(PointyCompassDirection.W)),
move(PointyCompassDirection.W, 4),
move(PointyCompassDirection.SW),
),
repeat(4, move(PointyCompassDirection.E)),
move(PointyCompassDirection.E, 4),
)
.rectangle({ width: 10, height: 10 })
.each((hex) => {
Expand All @@ -34,22 +34,4 @@ Grid.of(hexPrototype)
.run()

const grid = Grid.of(hexPrototype)
createSuite()
.add('rectangle', function () {
grid.rectangle({ width: 5, height: 5 }).run()
})
.add('traverse', function () {
grid
.traverse(
at({ q: 0, r: 0 }),
repeat(
2,
repeat(4, move(PointyCompassDirection.E)),
move(PointyCompassDirection.SE),
repeat(4, move(PointyCompassDirection.W)),
move(PointyCompassDirection.SW),
),
repeat(4, move(PointyCompassDirection.E)),
)
.run()
})
createSuite().add('', function () {})
6 changes: 0 additions & 6 deletions src/grid/functions/at.ts
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
21 changes: 10 additions & 11 deletions src/grid/functions/move.ts
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
}
}
14 changes: 0 additions & 14 deletions src/grid/functions/repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@ import { HexCoordinates } from '../../hex'
import { Traverser } from '../types'

// todo: looks a lot like Grid.traverse()

// export const repeat = <T extends Hex>(amount: number, command: Traverser<T>): Traverser<T> =>
// function* next(currentHex) {
// let nextHex = currentHex
// for (let i = 0; i < amount; i++) {
// const hexes = command(nextHex)
// for (const hex of hexes) {
// yield hex
// nextHex = hex
// }
// // todo: yield* command(grid, nextHex)
// }
// }

export const repeat = (amount: number, ...commands: Traverser[]): Traverser => (currentCoordinates) => {
const result: HexCoordinates[] = []
let coordinates = currentCoordinates
Expand Down

0 comments on commit f8b96ad

Please sign in to comment.