Skip to content

Commit

Permalink
feat(grid/transducers): add tap()
Browse files Browse the repository at this point in the history
  • Loading branch information
flauwekeul committed Jul 28, 2022
1 parent f9871ee commit 28797cf
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 31 deletions.
20 changes: 9 additions & 11 deletions examples/a-star-path-finding/aStar.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertCubeCoordinates, AxialCoordinates, equals, Grid, Hex, HexCoordinates, ring } from 'honeycomb-grid'
import { assertCubeCoordinates, AxialCoordinates, equals, Grid, Hex, HexCoordinates, ring, tap } from 'honeycomb-grid'
import { filter, remove } from 'transducist'
import { TARGET_COORDINATES } from './index'
import { AStarOptions, PathData } from './types'
Expand Down Expand Up @@ -30,15 +30,12 @@ export function aStar<T extends Hex>({
targetFound = equals(current.coordinates, TARGET_COORDINATES)
if (targetFound) return backtrack(closed, grid)

grid
.traverse(ring({ center: current.coordinates, radius: 1 }), [
filter(isPassable),
remove((hex) => isInList(closed, hex)),
// todo: this isn't allowed, because a transducer must always return T, but is that reasonable?
// map(tile => createPathData(tile))
])
// todo: use forEach() transducer once it exists
.forEach((neighbor) => {
grid.traverse(ring({ center: current.coordinates, radius: 1 }), [
filter(isPassable),
remove((hex) => isInList(closed, hex)),
// todo: this isn't allowed, because a transducer must always return T, but is that reasonable?
// map(tile => createPathData(tile))
tap((neighbor) => {
if (!isInList(open, neighbor)) {
const neighborPathData = createPathData(neighbor)
const nextG = current.g + neighborPathData.g
Expand All @@ -48,7 +45,8 @@ export function aStar<T extends Hex>({
}
open.push(neighborPathData)
}
})
}),
])
}

// no path found
Expand Down
12 changes: 5 additions & 7 deletions examples/a-star-path-finding/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Color, Polygon } from '@svgdotjs/svg.js'
import { createHexPrototype, Grid, rectangle } from 'honeycomb-grid'
import { map, mapIndexed, remove } from 'transducist'
import { createHexPrototype, Grid, rectangle, tap } from 'honeycomb-grid'
import { mapIndexed, remove } from 'transducist'
import { aStar } from './aStar'
import { getTileFill, render } from './render'
import { Tile } from './types'
Expand All @@ -20,18 +20,16 @@ const hexPrototype = createHexPrototype<Tile>({
},
})
const grid = new Grid(hexPrototype, rectangle({ width: 24, height: 12 })).update([
map((tile) => {
tap((tile) => {
if (tile.equals(START_COORDINATES) || tile.equals(TARGET_COORDINATES)) {
tile.cost = 1
return tile
return
}

tile.cost = Math.random() > IMPASSABLE_CHANCE ? Math.floor(Math.random() * MAX_COST) : IMPASSABLE_COST
return tile
}),
map((tile) => {
tap((tile) => {
tile.svg = render(tile)
return tile
}),
])
const shortestPath = aStar<Tile>({
Expand Down
13 changes: 5 additions & 8 deletions examples/line-of-sight/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import {
PartialCubeCoordinates,
repeatWith,
ring,
tap,
transduce,
Traverser,
TupleCoordinates,
} from 'honeycomb-grid'
import { filter, map, takeWhile, toArray } from 'transducist'
import { filter, takeWhile, toArray } from 'transducist'
import { initialGameState, onUpdate, updateGameState } from './gameState'
import { renderMap, renderPlayer } from './render'
import { TILES } from './tiles'
Expand Down Expand Up @@ -53,22 +54,19 @@ updateGameState(initialGameState)
function updateDiscoveredHexes(grid: Grid<Tile>) {
grid.update([
filter((tile) => tile.visibility === 'visible'),
// todo: create each() transducer for these things?
map((tile) => {
tap((tile) => {
tile.element.first().addClass('discovered')
return tile
}),
])
}

function updateFieldOfView(grid: Grid<Tile>, start: HexCoordinates) {
grid.update(
[
map((tile) => {
tap((tile) => {
tile.visibility = 'visible'
tile.element.first().removeClass('discovered')
tile.element.first().removeClass('undiscovered')
return tile
}),
],
fieldOfView(start),
Expand Down Expand Up @@ -96,9 +94,8 @@ function lineOfSight(start: HexCoordinates): Traverser<Tile> {
// todo: instead of keeping state like this, try making a reduce() transducer?
[
takeWhile(() => !foundOpaqueTerrain),
map((tile) => {
tap((tile) => {
foundOpaqueTerrain = tile.terrain.opaque
return tile
}),
],
toArray(),
Expand Down
7 changes: 2 additions & 5 deletions examples/line-of-sight/render.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { Svg } from '@svgdotjs/svg.js'
import { Grid } from 'honeycomb-grid'
import { map } from 'transducist'
import { Grid, tap } from 'honeycomb-grid'
import { Tile } from './types'

export function renderMap(draw: Svg, grid: Grid<Tile>) {
grid.update(
// todo: expose map() via honeycomb
map((tile) => {
tap((tile) => {
tile.element = renderTile(draw, tile)
return tile
}),
)
}
Expand Down
1 change: 1 addition & 0 deletions src/grid/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './functions'
export * from './grid'
export * from './transduce'
export * from './transducers'
export * from './traversers'
export * from './types'
1 change: 1 addition & 0 deletions src/grid/transducers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './inGrid'
export * from './tap'
14 changes: 14 additions & 0 deletions src/grid/transducers/tap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Transducer } from 'transducist'
import { Hex } from '../../hex'
import { INIT, RESULT, STEP } from '../constants'

export const tap =
<T extends Hex>(fn: (hex: T) => void): Transducer<T, T> =>
(xf) => ({
[INIT]: () => xf[INIT](),
[RESULT]: (result) => xf[RESULT](result),
[STEP]: (result, hex) => {
fn(hex)
return xf[STEP](result, hex)
},
})

0 comments on commit 28797cf

Please sign in to comment.