Instantly share code, notes, and snippets.

Embed
What would you like to do?
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