-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathHexMap.cs
68 lines (57 loc) · 1.95 KB
/
HexMap.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
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
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class HexMap : MonoBehaviour, IInitialize {
public HexGrid grid;
[UpdateWhenChanged]
public int size = 10;
[UpdateWhenChanged]
public GridShape shape = GridShape.Hexagonal;
// Use this for initialization
void Start () {
Init();
}
// Update is called once per frame
void Update () {
}
public void Init() {
var oldgrid = grid;
switch (shape) {
case GridShape.Hexagonal:
grid = new HexGrid(HexGrid.HexagonalShape(size));
break;
case GridShape.Triangle:
grid = new HexGrid(HexGrid.TriangularShape(size));
break;
case GridShape.Trapeze:
grid = new HexGrid(HexGrid.TrapezoidalShape(0, size, 0, size, HexGrid.OddQToCube));
break;
}
if (oldgrid != null) {
grid.orientation = oldgrid.orientation;
grid.scale = oldgrid.scale;
}
}
void Reset() {
Init();
}
void OnDrawGizmos() {
var poly = grid.PolygonVertices();
foreach (Cube cube in grid.Hexes) {
ScreenCoordinate pos = grid.HexToCenter(cube);
ScreenCoordinate first = null;
ScreenCoordinate last = null;
foreach (ScreenCoordinate coord in poly) {
if (last == null) {
first = coord + pos;
last = coord + pos;
continue;
}
ScreenCoordinate next = coord + pos;
Gizmos.DrawLine(new Vector3(last.position.x, last.position.y), new Vector3(next.position.x, next.position.y));
last = next;
}
Gizmos.DrawLine(new Vector3(last.position.x, last.position.y), new Vector3(first.position.x, first.position.y));
}
}
}