/
HexGridGenerator.cs
34 lines (32 loc) · 1.15 KB
/
HexGridGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using UnityEngine;
public class HexGridGenerator : MonoBehaviour
{
public int size;
private float height = Mathf.Sqrt(0.75f);
public static Node[][] nodes;
public GameObject hex;
public static GameObject[][] grid;
void Start()
{
nodes = new Node[size][];
grid = new GameObject[size][];
for (int i = 0; i < size; i++)
{
nodes[i] = new Node[size];
grid[i] = new GameObject[size];
for (int j = 0; j < size; j++)
{
float offset = height / 2 * ((float)i % 2);
nodes[i][j] = new Node(Random.Range(0f, 1f) < 0.75f, new Vector3(i + offset, 0f, j), new Vector2Int(i, j), i % 2);
GameObject o = Instantiate(hex);
o.transform.position = new Vector3(i * 0.75f, 0, j * height + offset);
Cell c = o.GetComponent<Cell>();
c.gridPos = new Vector2Int(i, j);
c.offset = i % 2;
c.blocked = !(nodes[i][j].open);
o.name = "Hex " + (i + j * size);
grid[i][j] = o;
}
}
}
}