Skip to content

Commit

Permalink
📝 update docs and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
flauwekeul committed Sep 3, 2022
1 parent 97e2838 commit 7a93cae
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
14 changes: 9 additions & 5 deletions docs/guide/coordinate-system.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ Most functions/methods that require coordinates accept `HexCoordinates`, which i
Because `HexCoordinates` can be any of the four types, you may use `toCube()` to convert `HexCoordinates` to `CubeCoordinates`:

```typescript
toCube(Hex.prototype, [1, 2]) // { q: 1, r: 2, s: -3 }
toCube(Hex.prototype, { col: 1, row: 2 }) // { q: 0, r: 2, s: -2 }
toCube(Hex.prototype, { s: 3, r: 4 }) // { q: -7, r: 4, s: 3 }
toCube(Hex.settings, [1, 2]) // { q: 1, r: 2, s: -3 }
toCube(Hex.settings, { col: 1, row: 2 }) // { q: 0, r: 2, s: -2 }
toCube(Hex.settings, { s: 3, r: 4 }) // { q: -7, r: 4, s: 3 }
```

## Converting
Expand All @@ -89,8 +89,8 @@ There are some functions for converting between types of coordinates.
Converting to cube coordinates:

```typescript
offsetToCube(Hex.prototype, { col: 1, row: 2 }) // { q: 0, r: 2, s: -2 }
pointToCube(Hex.prototype, { x: 10, y: 20 }) // { q: -1, r: 13, s: -12 }
offsetToCube(Hex.settings, { col: 1, row: 2 }) // { q: 0, r: 2, s: -2 }
pointToCube(Hex.settings, { x: 10, y: 20 }) // { q: -1, r: 13, s: -12 }
tupleToCube([1, 2]) // { q: 1, r: 2, s: -3 }
```

Expand All @@ -104,3 +104,7 @@ const hex = new Hex([1, 2])
hexToOffset(hex) // { col: 2, row: 2 }
hexToPoint(hex) // { x: 103.92304845413263, y: 90 }
```

::: details
`pointToCube()`, `offsetToCube()`, `toCube()` and [`distance()`](/api/#distance) require [hex settings](/api/interfaces/HexSettings) to work. See [Custom hexes](/guide/custom-hexes) to learn more about this.
:::
2 changes: 2 additions & 0 deletions docs/guide/custom-hexes.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ As you can see, for `dimensions` you can pass a number, which is interpreted as

For `origin` the string `'topLeft'` is also valid, meaning the origin of the hex will be in the very top left corner. This is convenient when rendering hexes on screen and you treat a hex as a DOM element. DOM elements have their origin in their top left corner.

For convenience, `Hex` has a static property `settings` that returns the settings of the (custom) Hex class.

## Custom properties

Because `defineHex()` returns a class, you can simply extend that class to add your own properties and methods:
Expand Down
4 changes: 2 additions & 2 deletions examples/a-star-path-finding/aStar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export function aStar<T extends Hex>({
// todo: probably better to work with hexes (instead of axial coordinates)?
const open: PathData[] = []
const closed: PathData[] = []
const _start = toCube(Tile.prototype, start)
const _target = toCube(Tile.prototype, target)
const _start = toCube(Tile.settings, start)
const _target = toCube(Tile.settings, target)
const createPathData = pathDataFactory(getCost, getDistance, _target)
let targetFound = false

Expand Down
2 changes: 1 addition & 1 deletion src/grid/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class Grid<T extends Hex> implements HexIterable<T>, HexTraversable<T> {
return result
}

let result = initialValue
let result: T | R = initialValue
for (const hex of this) {
result = reducer(result, hex)
}
Expand Down

0 comments on commit 7a93cae

Please sign in to comment.