Skip to content

Commit

Permalink
perf(grid): improve grid creation
Browse files Browse the repository at this point in the history
Don't use a generator internally and don't accept hex coordinates to create a grid.
  • Loading branch information
flauwekeul committed Jul 26, 2022
1 parent 63a932f commit 0748558
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
3 changes: 2 additions & 1 deletion examples/tank-game/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
createHex,
createHexPrototype,
CubeCoordinates,
fromCoordinates,
Grid,
HexCoordinates,
hexToPoint,
Expand All @@ -28,7 +29,7 @@ const config = {
const draw = SVG().addTo('body').size('100%', '100%').id('container')

const hexPrototype = createHexPrototype<Tile>({ dimensions: 50, origin: 'topLeft', visibility: 'undiscovered' })
const grid = new Grid<Tile>(hexPrototype, TILES)
const grid = new Grid<Tile>(hexPrototype, fromCoordinates(...TILES))

renderMap(draw, grid)

Expand Down
24 changes: 8 additions & 16 deletions src/grid/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ export class Grid<T extends Hex> implements Iterable<T> {

constructor(hexPrototype: T)
constructor(hexPrototype: T, traversers: Traverser<T> | Traverser<T>[])
constructor(hexPrototype: T, hexLikes: Iterable<HexCoordinates | T>)
constructor(hexPrototype: T, hexes: Iterable<T>)
constructor(grid: Grid<T>)
constructor(
hexPrototypeOrGrid: T | Grid<T>,
input: Traverser<T> | Traverser<T>[] | Iterable<HexCoordinates | T> = [],
) {
constructor(hexPrototypeOrGrid: T | Grid<T>, input: Traverser<T> | Traverser<T>[] | Iterable<T> = []) {
if (hexPrototypeOrGrid instanceof Grid<T>) {
this.hexPrototype = hexPrototypeOrGrid.hexPrototype
this.setHexes(hexPrototypeOrGrid)
Expand Down Expand Up @@ -113,18 +110,19 @@ export class Grid<T extends Hex> implements Iterable<T> {
}

update(transducers: Transducer<T, T> | Transducer<T, T>[]): this
update(transducers: Transducer<T, T> | Transducer<T, T>[], hexes: Iterable<HexCoordinates | T>): this
update(transducers: Transducer<T, T> | Transducer<T, T>[], hexes: Iterable<T>): this
update(transducers: Transducer<T, T> | Transducer<T, T>[], traversers: Traverser<T> | Traverser<T>[]): this
update(
transducers: Transducer<T, T> | Transducer<T, T>[],
hexesOrTraversers: Grid<T> | Iterable<HexCoordinates | T> | Traverser<T> | Traverser<T>[] = this,
hexesOrTraversers: Grid<T> | Iterable<T> | Traverser<T> | Traverser<T>[] = this,
): this {
if (hexesOrTraversers === this) {
transduce(hexesOrTraversers as Grid<T>, transducers, this.#toGridReducer)
return this
}

transduce(
// todo: wrapping this in a generator/iterator might improve performance
this.#getHexesFromIterableOrTraversers(hexesOrTraversers),
// automatically limit to hexes in grid (unless hexes is already those in the grid)
[inGrid(this)].concat(transducers),
Expand All @@ -142,18 +140,12 @@ export class Grid<T extends Hex> implements Iterable<T> {
return distance(this.hexPrototype, from, to)
}

*#getHexesFromIterableOrTraversers(
input: Iterable<HexCoordinates | T> | Traverser<T> | Traverser<T>[],
): Generator<T, void, void> {
const hexLikes = this.#isTraverser(input)
#getHexesFromIterableOrTraversers(input: Iterable<T> | Traverser<T> | Traverser<T>[]): Iterable<T> {
return this.#isTraverser(input)
? input(this.createHex)
: Array.isArray(input) && this.#isTraverser(input[0])
? concat(input)(this.createHex)
: (input as Iterable<HexCoordinates | T>)

for (const hexLike of hexLikes) {
yield this.createHex(hexLike)
}
: (input as Iterable<T>)
}

#isTraverser(value: unknown): value is Traverser<T> {
Expand Down

0 comments on commit 0748558

Please sign in to comment.