Skip to content

Commit

Permalink
🐛 Rings created with a radius never skip the first hex
Browse files Browse the repository at this point in the history
Because the start hex can't be controlled when creating rings from a radius, it makes no sense if their first hex is skipped. This normally happens when a cursor is present.

fixes #100
  • Loading branch information
flauwekeul committed May 16, 2023
1 parent 418583a commit 00b2567
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 63 deletions.
2 changes: 1 addition & 1 deletion docs/api/index.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# API - v4.0.1
# API - v4.1.0

## Table of contents

Expand Down
6 changes: 3 additions & 3 deletions docs/api/interfaces/RingFromRadiusOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#### Defined in

[grid/traversers/ring.ts:69](https://github.com/flauwekeul/honeycomb/blob/master/src/grid/traversers/ring.ts#L69)
[grid/traversers/ring.ts:72](https://github.com/flauwekeul/honeycomb/blob/master/src/grid/traversers/ring.ts#L72)

___

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

#### Defined in

[grid/traversers/ring.ts:70](https://github.com/flauwekeul/honeycomb/blob/master/src/grid/traversers/ring.ts#L70)
[grid/traversers/ring.ts:73](https://github.com/flauwekeul/honeycomb/blob/master/src/grid/traversers/ring.ts#L73)

___

Expand All @@ -36,4 +36,4 @@ ___

#### Defined in

[grid/traversers/ring.ts:71](https://github.com/flauwekeul/honeycomb/blob/master/src/grid/traversers/ring.ts#L71)
[grid/traversers/ring.ts:74](https://github.com/flauwekeul/honeycomb/blob/master/src/grid/traversers/ring.ts#L74)
6 changes: 3 additions & 3 deletions docs/api/interfaces/RingOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#### Defined in

[grid/traversers/ring.ts:61](https://github.com/flauwekeul/honeycomb/blob/master/src/grid/traversers/ring.ts#L61)
[grid/traversers/ring.ts:64](https://github.com/flauwekeul/honeycomb/blob/master/src/grid/traversers/ring.ts#L64)

___

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

#### Defined in

[grid/traversers/ring.ts:62](https://github.com/flauwekeul/honeycomb/blob/master/src/grid/traversers/ring.ts#L62)
[grid/traversers/ring.ts:65](https://github.com/flauwekeul/honeycomb/blob/master/src/grid/traversers/ring.ts#L65)

___

Expand All @@ -36,4 +36,4 @@ ___

#### Defined in

[grid/traversers/ring.ts:60](https://github.com/flauwekeul/honeycomb/blob/master/src/grid/traversers/ring.ts#L60)
[grid/traversers/ring.ts:63](https://github.com/flauwekeul/honeycomb/blob/master/src/grid/traversers/ring.ts#L63)
33 changes: 8 additions & 25 deletions playground/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
import { defineHex, Grid, GridAsJSON, HexCoordinates } from '../src'
import { defineHex, Grid, rectangle } from '../src'
import { render } from './render'

class CustomHex extends defineHex() {
static create(coordinates: HexCoordinates, custom: string) {
const hex = new CustomHex(coordinates)
hex.custom = custom
return hex
}

custom!: string
class CustomHex extends defineHex({ dimensions: 30, origin: 'topLeft' }) {
custom = 'test'
}

const hexes = [
[0, 0],
[1, 0],
[0, 1],
].map((coordinates) => CustomHex.create(coordinates as HexCoordinates, 'custom'))
const grid1 = new Grid(CustomHex, hexes)
const serializedGrid = JSON.stringify(grid1)

// console.log({ serializedGrid })

const deserializedGrid: GridAsJSON<CustomHex> = JSON.parse(serializedGrid)
const grid = new Grid(CustomHex, rectangle({ width: 10, height: 10 }))

// console.log({ deserializedGrid })

const grid2 = Grid.fromJSON(deserializedGrid, ({ q, r, custom }) => CustomHex.create([q, r], custom))

console.log(grid2.toArray())
for (const hex of grid) {
render(hex)
}
93 changes: 64 additions & 29 deletions src/grid/traversers/ring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,34 +172,69 @@ describe('when called with center and rotation', () => {
})

describe('when called with center and radius', () => {
test('returns a traverser that returns hexes in a ring shape around the center with the given radius', () => {
expect(ring({ center: [0, 0], radius: 1 })(createHex)).toMatchInlineSnapshot(`
[
Hex {
"q": 1,
"r": 0,
},
Hex {
"q": 0,
"r": 1,
},
Hex {
"q": -1,
"r": 1,
},
Hex {
"q": -1,
"r": 0,
},
Hex {
"q": 0,
"r": -1,
},
Hex {
"q": 1,
"r": -1,
},
]
`)
describe('without cursor', () => {
test('returns a traverser that returns hexes in a ring shape around the center with the given radius', () => {
expect(ring({ center: [0, 0], radius: 1 })(createHex)).toMatchInlineSnapshot(`
[
Hex {
"q": 1,
"r": 0,
},
Hex {
"q": 0,
"r": 1,
},
Hex {
"q": -1,
"r": 1,
},
Hex {
"q": -1,
"r": 0,
},
Hex {
"q": 0,
"r": -1,
},
Hex {
"q": 1,
"r": -1,
},
]
`)
})
})

describe('with cursor', () => {
test('returns a traverser that returns hexes in a ring shape around the center with the given radius', () => {
expect(ring({ center: [0, 0], radius: 1 })(createHex, cursor)).toMatchInlineSnapshot(`
[
Hex {
"q": 1,
"r": 0,
},
Hex {
"q": 0,
"r": 1,
},
Hex {
"q": -1,
"r": 1,
},
Hex {
"q": -1,
"r": 0,
},
Hex {
"q": 0,
"r": -1,
},
Hex {
"q": 1,
"r": -1,
},
]
`)
})
})
})
7 changes: 5 additions & 2 deletions src/grid/traversers/ring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ export function ring<T extends Hex>(options: RingOptions | RingFromRadiusOptions
const _rotation = rotation.toUpperCase() as Rotation
const hexes: T[] = []
let { radius } = options as RingFromRadiusOptions
const hasRadiusOption = isNumber(radius)
let firstHex: T

if (isNumber(radius)) {
if (hasRadiusOption) {
firstHex = createHex(center).translate({ q: radius, s: -radius })
} else {
firstHex = createHex((options as RingOptions).start ?? cursor)
Expand Down Expand Up @@ -46,7 +47,9 @@ export function ring<T extends Hex>(options: RingOptions | RingFromRadiusOptions
}
}

const skipFirstHex = !(options as RingOptions).start && cursor
// when a radius is passed in the options, it makes no sense to skip the first hex
// see https://github.com/flauwekeul/honeycomb/issues/100
const skipFirstHex = hasRadiusOption ? false : !(options as RingOptions).start && cursor
const startIndex = hexes.findIndex((hex) => hex.equals(firstHex))
// move part of hexes array to the front so that firstHex is actually the first hex
return hexes.slice(startIndex + (skipFirstHex ? 1 : 0)).concat(hexes.slice(0, startIndex))
Expand Down

0 comments on commit 00b2567

Please sign in to comment.