Created
April 8, 2024 07:49
-
-
Save general-games/f7e3e66cf73ff069d26d73664bdda502 to your computer and use it in GitHub Desktop.
Init entity for 2d Hex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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