Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save general-games/f7e3e66cf73ff069d26d73664bdda502 to your computer and use it in GitHub Desktop.
Save general-games/f7e3e66cf73ff069d26d73664bdda502 to your computer and use it in GitHub Desktop.
Init entity for 2d Hex
void InitEntityForHex(GridEntity entity)
{
if (grid.viewType == ViewType.View3D)
entity.Initialize(grid, (pos) =>
{
var cubeCoords = GetCubeCoords3D(pos);
return new GridPosition(cubeCoords, GetHexIndex(cubeCoords));
}, GetCell);
else if(grid.viewType == ViewType.View2D)
entity.Initialize(grid, (pos) =>
{
var cubeCoords = GetCubeCoords2D(pos);
return new GridPosition(cubeCoords, GetHexIndex(cubeCoords));
}, GetCell);
Vector3Int GetCubeCoords2D(Vector3 pos) => HexUtil2D.CubeCoordsFromWorldPosition(_origin, pos, HexUtil3D.GetInnerRadius(_cellSize), _cellSize);
Vector3Int GetCubeCoords3D(Vector3 pos) => HexUtil3D.CubeCoordsFromWorldPosition(_origin, pos, HexUtil3D.GetInnerRadius(_cellSize), _cellSize);
int GetHexIndex(Vector3Int cubeCoords) => HexUtil3D.GetIndex(cubeCoords, _width, _height);
Cell GetCell(GridPosition gridPosition) => grid.Graph.Find(gridPosition.index)?.Value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment