From 4f45d1f32d372bf2af9ab9d11285ce14a20bfb33 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Mon, 28 Nov 2022 08:18:43 +0100 Subject: [PATCH] start on hexgrids --- tools/grid.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tools/grid.py b/tools/grid.py index 825d7b7..3e5fbec 100644 --- a/tools/grid.py +++ b/tools/grid.py @@ -326,3 +326,33 @@ class Grid: print(spacer, end="") print() + + +class HexGrid(Grid): + """ + https://www.redblobgames.com/grids/hexagons/#coordinates-cube + Treat as 3d Grid + +y -x +z + y x z + yxz + z x y + -z +x -y + """ + def __init__(self, default=False): + super().__init__(default=default) + + def getNeighboursOf(self, pos: Coordinate, includeDefault: bool = False, includeDiagonal: bool = None) \ + -> List[Coordinate]: + """ + includeDiagonal is just here because of signature reasons, makes no difference in a hex grid + """ + vectors = [ + Coordinate(-1, 1, 0), # nw + Coordinate(-1, 0, 1), # ne + Coordinate(0, -1, 1), # e + Coordinate(1, -1, 0), # se + Coordinate(1, 0, -1), # sw + Coordinate(0, 1, -1), # w + ] + + return [pos + v for v in vectors if includeDefault or self.get(pos + v) != self.__default]