Skip to content

Commit

Permalink
✨ add static settings prop to Hex
Browse files Browse the repository at this point in the history
Also change type signatures of `pointToCube()`, `offsetToCube()`, `toCube()` and `distance()` so that they require (part of) hex settings instead of the prototype.
Closes #86.
  • Loading branch information
flauwekeul committed Sep 3, 2022
1 parent f7f287f commit 97e2838
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 99 deletions.
59 changes: 37 additions & 22 deletions docs/api/classes/Hex.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- [origin](Hex.md#origin)
- [row](Hex.md#row)
- [s](Hex.md#s)
- [settings](Hex.md#settings)
- [width](Hex.md#width)
- [x](Hex.md#x)
- [y](Hex.md#y)
Expand Down Expand Up @@ -55,7 +56,7 @@

#### Defined in

[hex/hex.ts:23](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L23)
[hex/hex.ts:28](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L28)

___

Expand All @@ -73,7 +74,7 @@ Readonly.col

#### Defined in

[hex/hex.ts:28](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L28)
[hex/hex.ts:33](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L33)

___

Expand All @@ -87,7 +88,7 @@ ___

#### Defined in

[hex/hex.ts:33](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L33)
[hex/hex.ts:38](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L38)

___

Expand All @@ -101,7 +102,7 @@ ___

#### Defined in

[hex/hex.ts:38](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L38)
[hex/hex.ts:43](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L43)

___

Expand All @@ -119,7 +120,7 @@ Readonly.height

#### Defined in

[hex/hex.ts:42](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L42)
[hex/hex.ts:47](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L47)

___

Expand All @@ -133,7 +134,7 @@ ___

#### Defined in

[hex/hex.ts:50](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L50)
[hex/hex.ts:55](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L55)

___

Expand All @@ -147,7 +148,7 @@ ___

#### Defined in

[hex/hex.ts:54](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L54)
[hex/hex.ts:59](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L59)

___

Expand All @@ -161,7 +162,7 @@ ___

#### Defined in

[hex/hex.ts:66](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L66)
[hex/hex.ts:71](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L71)

___

Expand All @@ -175,7 +176,7 @@ ___

#### Defined in

[hex/hex.ts:58](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L58)
[hex/hex.ts:63](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L63)

___

Expand All @@ -189,7 +190,7 @@ ___

#### Defined in

[hex/hex.ts:62](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L62)
[hex/hex.ts:67](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L67)

___

Expand All @@ -207,7 +208,7 @@ Readonly.row

#### Defined in

[hex/hex.ts:70](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L70)
[hex/hex.ts:75](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L75)

___

Expand All @@ -225,7 +226,21 @@ Readonly.s

#### Defined in

[hex/hex.ts:90](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L90)
[hex/hex.ts:95](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L95)

___

### <a id="settings" name="settings"></a> settings

`Static` `get` **settings**(): [`HexSettings`](../interfaces/HexSettings.md)

#### Returns

[`HexSettings`](../interfaces/HexSettings.md)

#### Defined in

[hex/hex.ts:22](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L22)

___

Expand All @@ -243,7 +258,7 @@ Readonly.width

#### Defined in

[hex/hex.ts:74](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L74)
[hex/hex.ts:79](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L79)

___

Expand All @@ -261,7 +276,7 @@ Readonly.x

#### Defined in

[hex/hex.ts:82](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L82)
[hex/hex.ts:87](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L87)

___

Expand All @@ -279,7 +294,7 @@ Readonly.y

#### Defined in

[hex/hex.ts:86](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L86)
[hex/hex.ts:91](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L91)

## Methods

Expand All @@ -305,7 +320,7 @@ Readonly.y

#### Defined in

[hex/hex.ts:103](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L103)
[hex/hex.ts:108](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L108)

___

Expand All @@ -325,7 +340,7 @@ ___

#### Defined in

[hex/hex.ts:107](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L107)
[hex/hex.ts:112](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L112)

___

Expand All @@ -339,7 +354,7 @@ ___

#### Defined in

[hex/hex.ts:111](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L111)
[hex/hex.ts:116](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L116)

___

Expand All @@ -359,7 +374,7 @@ ___

#### Defined in

[hex/hex.ts:115](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L115)
[hex/hex.ts:120](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L120)

## Constructors

Expand All @@ -375,7 +390,7 @@ ___

#### Defined in

[hex/hex.ts:97](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L97)
[hex/hex.ts:102](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L102)

## Properties

Expand All @@ -389,7 +404,7 @@ Readonly.q

#### Defined in

[hex/hex.ts:94](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L94)
[hex/hex.ts:99](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L99)

___

Expand All @@ -403,4 +418,4 @@ Readonly.r

#### Defined in

[hex/hex.ts:95](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L95)
[hex/hex.ts:100](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L100)
18 changes: 9 additions & 9 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,15 @@ ___

### <a id="toCube" name="toCube"></a> toCube

**toCube**(`hex`, `coordinates`): [`CubeCoordinates`](interfaces/CubeCoordinates.md)
**toCube**(`hexSettings`, `coordinates`): [`CubeCoordinates`](interfaces/CubeCoordinates.md)

Util for converting offset/axial/cube/tuple coordinates to cube coordinates.

#### Parameters

| Name | Type |
| :------ | :------ |
| `hex` | `Pick`<[`Hex`](classes/Hex.md), ``"offset"`` \| ``"isPointy"``\> |
| `hexSettings` | `Pick`<[`HexSettings`](interfaces/HexSettings.md), ``"orientation"`` \| ``"offset"``\> |
| `coordinates` | [`HexCoordinates`](index.md#HexCoordinates) |

#### Returns
Expand Down Expand Up @@ -543,7 +543,7 @@ ___

| Name | Type |
| :------ | :------ |
| `__namedParameters` | `Pick`<[`Hex`](classes/Hex.md), ``"offset"`` \| ``"isPointy"``\> |
| `__namedParameters` | `Pick`<[`HexSettings`](interfaces/HexSettings.md), ``"orientation"`` \| ``"offset"``\> |
| `__namedParameters` | [`OffsetCoordinates`](interfaces/OffsetCoordinates.md) |

#### Returns
Expand All @@ -552,7 +552,7 @@ ___

#### Defined in

[hex/functions/offsetToCube.ts:28](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/functions/offsetToCube.ts#L28)
[hex/functions/offsetToCube.ts:27](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/functions/offsetToCube.ts#L27)

___

Expand All @@ -564,7 +564,7 @@ ___

| Name | Type |
| :------ | :------ |
| `__namedParameters` | `Pick`<[`Hex`](classes/Hex.md), ``"isPointy"`` \| ``"dimensions"`` \| ``"origin"``\> |
| `__namedParameters` | `Pick`<[`HexSettings`](interfaces/HexSettings.md), ``"dimensions"`` \| ``"orientation"`` \| ``"origin"``\> |
| `__namedParameters` | [`Point`](interfaces/Point.md) |

#### Returns
Expand All @@ -573,7 +573,7 @@ ___

#### Defined in

[hex/functions/pointToCube.ts:11](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/functions/pointToCube.ts#L11)
[hex/functions/pointToCube.ts:10](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/functions/pointToCube.ts#L10)

___

Expand Down Expand Up @@ -956,13 +956,13 @@ ___

### <a id="distance" name="distance"></a> distance

**distance**(`hex`, `from`, `to`): `number`
**distance**(`hexSettings`, `from`, `to`): `number`

#### Parameters

| Name | Type |
| :------ | :------ |
| `hex` | `Pick`<[`Hex`](classes/Hex.md), ``"offset"`` \| ``"isPointy"``\> |
| `hexSettings` | `Pick`<[`HexSettings`](interfaces/HexSettings.md), ``"orientation"`` \| ``"offset"``\> |
| `from` | [`HexCoordinates`](index.md#HexCoordinates) |
| `to` | [`HexCoordinates`](index.md#HexCoordinates) |

Expand Down Expand Up @@ -1050,4 +1050,4 @@ ___

#### Defined in

[hex/hex.ts:123](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L123)
[hex/hex.ts:128](https://github.com/flauwekeul/honeycomb/blob/beta/src/hex/hex.ts#L128)
5 changes: 2 additions & 3 deletions src/grid/functions/distance.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { expect, test } from 'vitest'
import { Hex } from '../../hex'
import { defaultHexSettings } from '../../hex'
import { distance } from './distance'

test('returns the number of hexes between the passed 2 hexes (excluding the last hex)', () => {
const hexPrototype = Hex.prototype
expect(distance(hexPrototype, { q: 1, r: 3 }, { q: 8, r: 7 })).toBe(11)
expect(distance(defaultHexSettings, { q: 1, r: 3 }, { q: 8, r: 7 })).toBe(11)
})
12 changes: 8 additions & 4 deletions src/grid/functions/distance.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Hex, HexCoordinates, toCube } from '../../hex'
import { HexCoordinates, HexSettings, toCube } from '../../hex'

export function distance(hex: Pick<Hex, 'offset' | 'isPointy'>, from: HexCoordinates, to: HexCoordinates) {
const { q: fromQ, r: fromR, s: fromS } = toCube(hex, from)
const { q: toQ, r: toR, s: toS } = toCube(hex, to)
export function distance(
hexSettings: Pick<HexSettings, 'offset' | 'orientation'>,
from: HexCoordinates,
to: HexCoordinates,
) {
const { q: fromQ, r: fromR, s: fromS } = toCube(hexSettings, from)
const { q: toQ, r: toR, s: toS } = toCube(hexSettings, to)
return Math.max(Math.abs(fromQ - toQ), Math.abs(fromR - toR), Math.abs(fromS - toS))
}
37 changes: 29 additions & 8 deletions src/hex/functions/offsetToCube.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
import { expect, test } from 'vitest'
import { Orientation } from '../types'
import { offsetToCube } from './offsetToCube'

test('returns axial coordinates bases on the passed offset coordinates', () => {
expect(offsetToCube({ offset: -1, isPointy: true }, { col: 1, row: 4 })).toEqual({ q: -1, r: 4, s: -3 })
expect(offsetToCube({ offset: 1, isPointy: true }, { col: 1, row: 4 })).toEqual({ q: -1, r: 4, s: -3 })
expect(offsetToCube({ offset: -1, isPointy: false }, { col: 1, row: 4 })).toEqual({ q: 1, r: 4, s: -5 })
expect(offsetToCube({ offset: 1, isPointy: false }, { col: 1, row: 4 })).toEqual({ q: 1, r: 3, s: -4 })
expect(offsetToCube({ offset: -1, orientation: Orientation.POINTY }, { col: 1, row: 4 })).toEqual({
q: -1,
r: 4,
s: -3,
})
expect(offsetToCube({ offset: 1, orientation: Orientation.POINTY }, { col: 1, row: 4 })).toEqual({
q: -1,
r: 4,
s: -3,
})
expect(offsetToCube({ offset: -1, orientation: Orientation.FLAT }, { col: 1, row: 4 })).toEqual({ q: 1, r: 4, s: -5 })
expect(offsetToCube({ offset: 1, orientation: Orientation.FLAT }, { col: 1, row: 4 })).toEqual({ q: 1, r: 3, s: -4 })

expect(offsetToCube({ offset: -1, isPointy: true }, { col: 4, row: 1 })).toEqual({ q: 4, r: 1, s: -5 })
expect(offsetToCube({ offset: 1, isPointy: true }, { col: 4, row: 1 })).toEqual({ q: 3, r: 1, s: -4 })
expect(offsetToCube({ offset: -1, isPointy: false }, { col: 4, row: 1 })).toEqual({ q: 4, r: -1, s: -3 })
expect(offsetToCube({ offset: 1, isPointy: false }, { col: 4, row: 1 })).toEqual({ q: 4, r: -1, s: -3 })
expect(offsetToCube({ offset: -1, orientation: Orientation.POINTY }, { col: 4, row: 1 })).toEqual({
q: 4,
r: 1,
s: -5,
})
expect(offsetToCube({ offset: 1, orientation: Orientation.POINTY }, { col: 4, row: 1 })).toEqual({
q: 3,
r: 1,
s: -4,
})
expect(offsetToCube({ offset: -1, orientation: Orientation.FLAT }, { col: 4, row: 1 })).toEqual({
q: 4,
r: -1,
s: -3,
})
expect(offsetToCube({ offset: 1, orientation: Orientation.FLAT }, { col: 4, row: 1 })).toEqual({ q: 4, r: -1, s: -3 })
})
9 changes: 5 additions & 4 deletions src/hex/functions/offsetToCube.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { offsetFromZero } from '../../utils'
import { Hex } from '../hex'
import { CubeCoordinates, HexOffset, OffsetCoordinates } from '../types'
import { CubeCoordinates, HexOffset, HexSettings, OffsetCoordinates, Orientation } from '../types'

/**
* @hidden
Expand All @@ -25,5 +24,7 @@ export const offsetToCubeFlat = (col: number, row: number, offset: HexOffset): C
/**
* @category Hex
*/
export const offsetToCube = ({ offset, isPointy }: Pick<Hex, 'offset' | 'isPointy'>, { col, row }: OffsetCoordinates) =>
isPointy ? offsetToCubePointy(col, row, offset) : offsetToCubeFlat(col, row, offset)
export const offsetToCube = (
{ offset, orientation }: Pick<HexSettings, 'offset' | 'orientation'>,
{ col, row }: OffsetCoordinates,
) => (orientation === Orientation.POINTY ? offsetToCubePointy(col, row, offset) : offsetToCubeFlat(col, row, offset))
Loading

0 comments on commit 97e2838

Please sign in to comment.