-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hex,grid): traversers return hexes (as before), fix move() trave…
…rser, add neighborOf() function The move() traverser didn't work properly, fixed it by converting to offset coordinates, update row or col (depending on hex orientation) and then converting back to axial coordinates. Because of this some new functions were needed: hexToOffset() and offsetToAxial(). Also added row and col getters (which use hexToOffset()) on hex prototype for convenience. The functionality to get a neighboring hex was moved from the move() traverser to a new neighborOf() grid function. This function also supports ambiguous directions (North and South for pointy and East and West for flat hexes). But because it was very awkward to create a hex (needed for the generic neighborOf() function) and then returning only its coordinates in move(), I decided that all traversers should just receive and return hexes (instead of receiving hexes but returning coordinates). For this I added a copyHex() function (and delegating copy() method on hex prototype) that's able to copy the "cursor" hex a traverser receives with new coordinates. This hex is then returned from traversers. Finally, both copyHex() and createHex() now copy/create any custom props in their returning hex.
- Loading branch information
1 parent
751be5a
commit b2da583
Showing
19 changed files
with
157 additions
and
102 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 @@ | ||
export * from './neighborOf' |
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,26 @@ | ||
import { AxialCoordinates, Hex, offsetToAxialFlat, offsetToAxialPointy } from '../../hex' | ||
import { Compass } from '../types' | ||
|
||
const DIRECTION_COORDINATES: AxialCoordinates[] = [ | ||
{ q: 1, r: 0 }, | ||
{ q: 0, r: 1 }, | ||
{ q: -1, r: 1 }, | ||
{ q: -1, r: 0 }, | ||
{ q: 0, r: -1 }, | ||
{ q: 1, r: -1 }, | ||
] | ||
|
||
export const neighborOf = <T extends Hex>(hex: T, direction: Compass) => { | ||
if ((direction === Compass.S || direction === Compass.N) && hex.isPointy) { | ||
const nextRow = direction === Compass.S ? hex.row + 1 : hex.row - 1 | ||
return offsetToAxialPointy(hex.col, nextRow, hex.offset) | ||
} | ||
|
||
if ((direction === Compass.E || direction === Compass.W) && hex.isFlat) { | ||
const nextCol = direction === Compass.E ? hex.col + 1 : hex.col - 1 | ||
return offsetToAxialFlat(nextCol, hex.row, hex.offset) | ||
} | ||
|
||
const neighbor = DIRECTION_COORDINATES[direction] | ||
return { q: hex.q + neighbor.q, r: hex.r + neighbor.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
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,6 +1,6 @@ | ||
import { DefaultHexPrototype, HexCoordinates } from '../../hex' | ||
import { Hex, HexCoordinates } from '../../hex' | ||
import { Traverser } from '../types' | ||
|
||
export const at = <T extends DefaultHexPrototype>(cursor: HexCoordinates): Traverser<T> => () => [cursor] | ||
export const at = <T extends Hex>(coordinates: HexCoordinates): Traverser<T> => (cursor) => [cursor.copy(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,46 +1,17 @@ | ||
import { DefaultHexPrototype, HexCoordinates } from '../../hex' | ||
import { offsetFromZero } from '../../utils' | ||
import { DIRECTION_COORDINATES } from '../constants' | ||
import { Hex } from '../../hex' | ||
import { neighborOf } from '../functions' | ||
import { Compass, Traverser } from '../types' | ||
|
||
// todo: also accept a string and/or number for direction? | ||
export const move = <T extends DefaultHexPrototype>(direction: Compass, times = 1): Traverser<T> => { | ||
const { q, r } = DIRECTION_COORDINATES[direction] | ||
|
||
return (cursor, hexPrototype) => { | ||
const result: HexCoordinates[] = [] | ||
const relativeOffset = (coordinate: number) => offsetFromZero(hexPrototype.offset, coordinate) | ||
|
||
// todo: refactor, move ifs inside single for loop | ||
if (hexPrototype.isPointy && (direction === Compass.S || direction === Compass.N)) { | ||
for (let i = 1; i <= times; i++) { | ||
const cursorCol = cursor.q - relativeOffset(cursor.r) | ||
const cursorRow = cursor.r | ||
const addCol = i * q - relativeOffset(i * r) | ||
const addRow = i * r | ||
const _q = cursorCol + relativeOffset(cursorRow) + addCol | ||
const _r = cursorRow + addRow | ||
result.push({ q: _q, r: _r }) | ||
} | ||
return result | ||
} | ||
|
||
if (hexPrototype.isFlat && (direction === Compass.E || direction === Compass.W)) { | ||
for (let i = 1; i <= times; i++) { | ||
const cursorCol = cursor.q | ||
const cursorRow = cursor.r - relativeOffset(cursor.q) | ||
const addCol = i * q | ||
const addRow = i * r - relativeOffset(i * q) | ||
const _q = cursorCol + addCol | ||
const _r = cursorRow + relativeOffset(cursorCol) + addRow | ||
result.push({ q: _q, r: _r }) | ||
} | ||
return result | ||
} | ||
export const move = <T extends Hex>(direction: Compass, times = 1): Traverser<T> => { | ||
return (cursor) => { | ||
const result: T[] = [] | ||
let _cursor = cursor | ||
|
||
for (let i = 1; i <= times; i++) { | ||
result.push({ q: cursor.q + q * i, r: cursor.r + r * i }) | ||
_cursor = _cursor.copy(neighborOf(_cursor, direction)) | ||
result.push(_cursor) | ||
} | ||
|
||
return result | ||
} | ||
} |
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
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,4 @@ | ||
import { Hex, HexCoordinates } from '../types' | ||
|
||
export const copyHex = <T extends Hex>(hex: T, newProps: Partial<T> | HexCoordinates = {}) => | ||
Object.assign(Object.create(Object.getPrototypeOf(hex)), hex, newProps) |
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,7 +1,10 @@ | ||
import { DefaultHexPrototype, Hex, HexCoordinates } from '../types' | ||
import { Hex, HexCoordinates } from '../types' | ||
|
||
export const createHex = <T extends DefaultHexPrototype>(prototype: T, { q, r, s = -q - r }: HexCoordinates) => | ||
export const createHex = <T extends Hex>( | ||
prototype: T, | ||
{ q, r, s = -q - r, ...rest }: HexCoordinates | T = { q: 0, r: 0 }, | ||
) => | ||
// fixme: when `prototype` is a hex instance, an object with that instance as prototype is created... | ||
// either only accept a hex prototype or check if `prototype` is a hex instance and then clone it? | ||
// todo: make coordinates readonly | ||
Object.assign(Object.create(prototype), { q, r, s }) as T extends Hex ? T : T & Hex | ||
Object.assign(Object.create(prototype), { q, r, s }, rest) as T extends Hex ? T : T & Hex |
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
Oops, something went wrong.