Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grid.neighborsOf #91

Closed
ggomaeng opened this issue Oct 13, 2022 · 8 comments
Closed

grid.neighborsOf #91

ggomaeng opened this issue Oct 13, 2022 · 8 comments
Labels

Comments

@ggomaeng
Copy link

ggomaeng commented Oct 13, 2022

First, thank you for this amazing project! :)

Is grid.neighborsOf deprecated in v4.0.0?

@flauwekeul
Copy link
Owner

Thanks! 😄

It's been replaced with neighborOf().

@ggomaeng
Copy link
Author

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

Thank you so much for the pointer! 👍 ❤

github-actions bot pushed a commit that referenced this issue Oct 13, 2022
# [v4.0.1](v4.0.0...v4.0.1) (2022-10-13)

## 🐛 Bug Fixes
- [`07af072`](07af072)  grid.neighborOf() now uses grid.getHex() primarily (Issues: [`#91`](#91))

## [4.0.1](v4.0.0...v4.0.1) (2022-10-13)
@github-actions
Copy link

🎉 This issue has been resolved in version 4.0.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants