-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexGrid.cs
More file actions
78 lines (61 loc) · 2.26 KB
/
HexGrid.cs
File metadata and controls
78 lines (61 loc) · 2.26 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HexGrid : MonoBehaviour
{
public int height = 6;
public int width = 6;
public HexCell cellPrefab;
public Text cellLabelPrefab;
public Color defaultColor = Color.white;
public Color touchedColor = Color.magenta;
HexCell[] cells;
Canvas gridCanvas;
HexMesh hexMesh;
void Awake()
{
gridCanvas = GetComponentInChildren<Canvas>();
hexMesh = GetComponentInChildren<HexMesh>();
cells = new HexCell[height * width];
for (int z = 0, i = 0; z < height; z++)
{
for (int x = 0; x < width; x++)
{
CreateCell(x, z, i++);
}
}
}
void Start()
{
hexMesh.Triangulate(cells);
}
void CreateCell(int x, int z, int i)
{
Vector3 position;
// '/' rounds to the nearest integer, which is why this math works
position.x = (x + (z * 0.5f - z / 2)) * (HexMetrics.innerRadius * 2f);
position.y = 0f;
position.z = z * (HexMetrics.outerRadius * 1.5f);
HexCell cell = cells[i] = Instantiate<HexCell>(cellPrefab);
cell.transform.SetParent(transform, false);
cell.transform.localPosition = position;
cell.coordinates = HexCoordinates.FromOffsetCoordinates(x, z);
cell.color = defaultColor;
Text label = Instantiate<Text>(cellLabelPrefab);
label.rectTransform.SetParent(gridCanvas.transform, false);
label.rectTransform.anchoredPosition = new Vector2(position.x, position.z);
label.text = cell.coordinates.ToStringOnSeparateLines();
label.text = x.ToString() + "\n" + z.ToString();
}
public void ColorCell(Vector3 position, Color color)
{
position = transform.InverseTransformPoint(position);
HexCoordinates coordinates = HexCoordinates.FromPosition(position);
Debug.Log("touched at " + coordinates.ToString());
int index = coordinates.X + coordinates.Z * width + coordinates.Z / 2;
HexCell cell = cells[index];
cell.color = color;
hexMesh.Triangulate(cells);
}
}