Skip to content

Commit

Permalink
✨ hexes now only have q and r in the instance
Browse files Browse the repository at this point in the history
The s coordinate is moved to the prototype (because it's redundant).
  • Loading branch information
flauwekeul committed Aug 11, 2022
1 parent 500af5d commit b369f1b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/grid/grid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ describe('toJSON()', () => {

expect(result).toStrictEqual({ hexSettings, coordinates })
expect(JSON.stringify(grid)).toBe(
'{"hexSettings":{"dimensions":{"xRadius":10,"yRadius":10},"orientation":"FLAT","origin":{"x":0,"y":0},"offset":1},"coordinates":[{"q":0,"r":0,"s":0},{"q":1,"r":0,"s":-1}]}',
'{"hexSettings":{"dimensions":{"xRadius":10,"yRadius":10},"orientation":"FLAT","origin":{"x":0,"y":0},"offset":1},"coordinates":[{"q":0,"r":0},{"q":1,"r":0}]}',
)
})
})
Expand Down
10 changes: 6 additions & 4 deletions src/hex/hex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ describe('creation', () => {
expect(new Hex([1, 2])).toContain<CubeCoordinates>({ q: 1, r: 2, s: -3 })
expect(new Hex({ col: 1, row: 2 })).toContain<CubeCoordinates>({ q: 0, r: 2, s: -2 })
expect(new Hex({ q: 1, r: 2 })).toContain<CubeCoordinates>({ q: 1, r: 2, s: -3 })
expect(new Hex({ r: 3, s: 0 })).toContain<CubeCoordinates>({ q: -3, r: 3, s: 0 })
expect(new Hex({ q: -3, r: 4, s: -1 })).toContain<CubeCoordinates>({ q: -3, r: 4, s: -1 })
})
})

Expand Down Expand Up @@ -119,15 +121,15 @@ test('implements a point in the prototype', () => {
expect(Object.hasOwn(hex, 'y')).toBe(false)
})

test('implements cube coordinates in the instance', () => {
test('implements axial coordinates in the instance and the s coordinate in the prototype', () => {
const hex = new Hex([3, -1])

expect(hex.q).toBe(3)
expect(hex.r).toBe(-1)
expect(hex.s).toBe(-2)
;['q', 'r', 's'].forEach((coordinate) => {
expect(Object.hasOwn(hex, coordinate)).toBe(true)
})
expect(Object.hasOwn(hex, 'q')).toBe(true)
expect(Object.hasOwn(hex, 'r')).toBe(true)
expect(Object.hasOwn(hex, 's')).toBe(false)
})

describe('clone()', () => {
Expand Down
8 changes: 5 additions & 3 deletions src/hex/hex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,18 @@ export class Hex
return hexToPoint(this).y
}

get s(): number {
return -this.q - this.r
}

readonly q: number
readonly r: number
readonly s: number

// todo: also accept q, r and optional s as arguments?
constructor(coordinates: HexCoordinates = [0, 0]) {
const { q, r, s } = toCube(this, coordinates)
const { q, r } = toCube(this, coordinates)
this.q = q
this.r = r
this.s = s
}

clone<T extends Hex>(newProps: HexCoordinates = this): T {
Expand Down

0 comments on commit b369f1b

Please sign in to comment.