Skip to content

Instantly share code, notes, and snippets.

@turbohermit
Last active January 3, 2018 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turbohermit/854dca0261b8c1fbe85d80873c0f78fb to your computer and use it in GitHub Desktop.
Save turbohermit/854dca0261b8c1fbe85d80873c0f78fb to your computer and use it in GitHub Desktop.
public class GridManager : MonoBehaviour
{
public int radiusInRingAmount = 5;
public GameObject tilePrefab;
public static List<Vector3> cubeCoordinates = new List<Vector3>();
void Awake()
{
GenerateGrid();
}
public void GenerateGrid()
{
for(int c = -radiusInRingAmount; c <= radiusInRingAmount; c++)
{
int negativeRows = Mathf.Max(-radiusInRingAmount, -c - radiusInRingAmount);
int positiveRows = Mathf.Min(radiusInRingAmount, -c + radiusInRingAmount);
for(int r = negativeRows; r <= positiveRows; r++)
{
cubeCoordinates.Add(new Vector3(c, r, -c - r));
PlaceNewTile(cubeCoordinates[cubeCoordinates.Count - 1]);
}
}
}
public void PlaceNewTile(Vector3 position)
{
GameObject newTile = Instantiate(tilePrefab, position, Quaternion.identity,transform);
newTile.name = "tile: "+position;
}
public Vector2 CubeToAxial(Vector3 cubeCoords){
return new Vector2(cubeCoords.x,cubeCoords.y);
}
public Vector3 AxialToCube(Vector2 axialCoords){
return new Vector3(axialCoords.x,axialCoords.y,-axialCoords.x-axialCoords.y);
}
//Return direction vectors for each side. From left to right: 0 right, 1 right-up, 2 left-up, 3 left, 4 left-down, 5 right-down.
//Directiosn could be enumerated for clarification.
public Vector3[] GetDirections()
{
return new Vector3[] { new Vector3(1,-1,0), new Vector3(1,0,-1), new Vector3(0,1,-1), new Vector3(-1, 1, 0), new Vector3(-1, 0, +1), new Vector3(0, -1, +1)};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment