flauwekeul / honeycomb

Create hex grids easily, in node or the browser.
https://abbekeultjes.nl/honeycomb
MIT License
630 stars 57 forks source link

grid.neighborsOf #91

Closed ggomaeng closed 1 year ago

ggomaeng commented 1 year ago

First, thank you for this amazing project! :)

Is grid.neighborsOf deprecated in v4.0.0?

flauwekeul commented 1 year ago

Thanks! 😄

It's been replaced with neighborOf().

ggomaeng commented 1 year ago

Wow thank you for such a fast response 💓

Just wondering, does neighborOf() return the reference from the grid, or just the hex containing the q, r, s values?

I am creating custom hexes with custom data inside, but it seems like when I call neighborOf, it doesn't seem to return the binded data associated to the object.

ggomaeng commented 1 year ago

To implement neighborsOf myself, would something like the code below suffice?

export function neighborsOf(grid, hex) {
  const east = grid.neighborOf(hex, Direction.E);
  const northeast = grid.neighborOf(hex, Direction.NE);
  const southeast = grid.neighborOf(hex, Direction.SE);
  const west = grid.neighborOf(hex, Direction.W);
  const northwest = grid.neighborOf(hex, Direction.NW);
  const southwest = grid.neighborOf(hex, Direction.SW);
  const neighbors = [east, northeast, southeast, west, northwest, southwest].map(({ q, r }) => {
    return grid.getHex([q, r]);
  });
  return neighbors;
}

thank you for your input in advance :)

flauwekeul commented 1 year ago

Hm, looks like you stumbled on a bug there. Unless you pass { allowOutside: false } as the third argument, it creates a new hex (with the expected coordinates) and returns that. But it would make more sense if it gets the hex from the grid and returns that. I'll make a bug fix for this soon!

For now you can circumvent the bug by doing this (this will return undefined if the neighboring hex doesn't exist in the grid though):

grid.getHex(grid.neighborOf([1,2], Direction.E))
ggomaeng commented 1 year ago

Ah I'm glad I could point it out. The previous major version returned the references so I thought I was making a mistake somewhere. Any plans on providing the neighborsOf function in the library?

flauwekeul commented 1 year ago

The neighborOf() (singular) function is already present (both as a standalone function that returns axial coordinates and as a method of Grid that returns a hex). If you want the neighborsOf() (plural) functionality from v3, you could use the ring() traverser:

const neighbors = grid.traverse(ring({ center: [1, 2], radius: 1 }))
ggomaeng commented 1 year ago

Thank you so much for the pointer! 👍 ❤

github-actions[bot] commented 1 year ago

:tada: This issue has been resolved in version 4.0.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket: