1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using UnityEngine ;
4
+
5
+ namespace HexEngine {
6
+ /// <summary>
7
+ /// Enum representation of six direction in hex space.
8
+ /// </summary>
9
+ public enum HexDirection
10
+ {
11
+ N ,
12
+ NE ,
13
+ SE ,
14
+ S ,
15
+ SW ,
16
+ NW
17
+ }
18
+
19
+ public static class HexDirectionExtension {
20
+
21
+ #region Readonly Arrays
22
+ private static readonly HexCoords [ ] _coords = new [ ] {
23
+ new HexCoords ( 0 , 1 ) ,
24
+ new HexCoords ( 1 , 0 ) ,
25
+ new HexCoords ( 1 , - 1 ) ,
26
+ new HexCoords ( 0 , - 1 ) ,
27
+ new HexCoords ( - 1 , 0 ) ,
28
+ new HexCoords ( - 1 , 1 ) ,
29
+ } ;
30
+
31
+ private static readonly float [ ] _angles = new [ ] {
32
+ 0f , 60f , 120f ,
33
+ 180f , 240f , 300f
34
+ } ;
35
+
36
+ private static readonly Vector3 [ ] _directions = new [ ] {
37
+ Quaternion . Euler ( 0f , _angles [ 0 ] , 0f ) * Vector3 . forward ,
38
+ Quaternion . Euler ( 0f , _angles [ 1 ] , 0f ) * Vector3 . forward ,
39
+ Quaternion . Euler ( 0f , _angles [ 2 ] , 0f ) * Vector3 . forward ,
40
+ Quaternion . Euler ( 0f , _angles [ 3 ] , 0f ) * Vector3 . forward ,
41
+ Quaternion . Euler ( 0f , _angles [ 4 ] , 0f ) * Vector3 . forward ,
42
+ Quaternion . Euler ( 0f , _angles [ 5 ] , 0f ) * Vector3 . forward ,
43
+ } ;
44
+
45
+ private static readonly Vector3 [ ] _vertexDirections = new [ ] {
46
+ Quaternion . Euler ( 0f , _angles [ 0 ] - 30f , 0f ) * Vector3 . forward ,
47
+ Quaternion . Euler ( 0f , _angles [ 1 ] - 30f , 0f ) * Vector3 . forward ,
48
+ Quaternion . Euler ( 0f , _angles [ 2 ] - 30f , 0f ) * Vector3 . forward ,
49
+ Quaternion . Euler ( 0f , _angles [ 3 ] - 30f , 0f ) * Vector3 . forward ,
50
+ Quaternion . Euler ( 0f , _angles [ 4 ] - 30f , 0f ) * Vector3 . forward ,
51
+ Quaternion . Euler ( 0f , _angles [ 5 ] - 30f , 0f ) * Vector3 . forward ,
52
+ } ;
53
+
54
+ private const float VertexFactor = .5f * HexMath . InToOutRadius ;
55
+ private static readonly Vector3 [ ] _vertices = new [ ] {
56
+ Quaternion . Euler ( 0f , _angles [ 0 ] - 30f , 0f ) * Vector3 . forward * VertexFactor ,
57
+ Quaternion . Euler ( 0f , _angles [ 1 ] - 30f , 0f ) * Vector3 . forward * VertexFactor ,
58
+ Quaternion . Euler ( 0f , _angles [ 2 ] - 30f , 0f ) * Vector3 . forward * VertexFactor ,
59
+ Quaternion . Euler ( 0f , _angles [ 3 ] - 30f , 0f ) * Vector3 . forward * VertexFactor ,
60
+ Quaternion . Euler ( 0f , _angles [ 4 ] - 30f , 0f ) * Vector3 . forward * VertexFactor ,
61
+ Quaternion . Euler ( 0f , _angles [ 5 ] - 30f , 0f ) * Vector3 . forward * VertexFactor ,
62
+ } ;
63
+ #endregion
64
+
65
+ /// <summary>
66
+ /// HexCoords of the direction.
67
+ /// </summary>
68
+ public static HexCoords Coords ( this HexDirection direction ) => _coords [ ( int ) direction ] ;
69
+
70
+ /// <summary>
71
+ /// Angle in degrees from 0 for HexDirection.N, increasing clockwise.
72
+ /// </summary>
73
+ public static float Angle ( this HexDirection direction ) => _angles [ ( int ) direction ] ;
74
+
75
+ /// <summary>
76
+ /// Normalized vector of direction in 3D space for HexDirection, where HexDirection.N is Vector3.forward.
77
+ /// </summary>
78
+ public static Vector3 Direction ( this HexDirection direction ) => _directions [ ( int ) direction ] ;
79
+
80
+ /// <summary>
81
+ /// Normalized vector of direction to the left corner of the hex edge in given direction.
82
+ /// </summary>
83
+ public static Vector3 LeftCornerDirection ( this HexDirection direction ) => _vertexDirections [ ( int ) direction ] ;
84
+
85
+ /// <summary>
86
+ /// Normalized vector of direction to the right corner of the hex edge in given direction.
87
+ /// </summary>
88
+ public static Vector3 RightCornerDirection ( this HexDirection direction ) => _vertexDirections [ ( ( int ) direction + 1 ) % 6 ] ;
89
+
90
+ /// <summary>
91
+ /// Vector from the center of the hex to the left corner on the edge in given direction.
92
+ /// </summary>
93
+ public static Vector3 LeftCorner ( this HexDirection direction ) => _vertices [ ( int ) direction ] ;
94
+
95
+ /// <summary>
96
+ /// Vector from the center of the hex to the right corner on the edge in given direction.
97
+ /// </summary>
98
+ public static Vector3 RightCorner ( this HexDirection direction ) => _vertices [ ( ( int ) direction + 1 ) % 6 ] ;
99
+
100
+ /// <summary>
101
+ /// Iterates through all hex directions starting from this direction and going clockwise.
102
+ /// </summary>
103
+ public static IEnumerable < HexDirection > Loop ( this HexDirection direction ) {
104
+ var startIndex = ( int ) direction ;
105
+ for ( int i = 0 ; i < 6 ; i ++ ) {
106
+ yield return ( HexDirection ) ( ( startIndex + i ) % 6 ) ;
107
+ }
108
+ }
109
+
110
+ /// <summary>
111
+ /// Returns HexDirection rotated clockwise.
112
+ /// </summary>
113
+ public static HexDirection Rotate ( this HexDirection direction , int rotation ) {
114
+ var value = ( int ) direction + rotation ;
115
+ value = ( ( value % 6 ) + 6 ) ;
116
+ return ( HexDirection ) ( rotation % 6 ) ;
117
+ }
118
+
119
+ /// <summary>
120
+ /// Returns HexDirection rotated clockwise once.
121
+ /// </summary>
122
+ public static HexDirection Right ( this HexDirection direction ) {
123
+ return ( HexDirection ) ( ( ( int ) direction + 1 ) % 6 ) ;
124
+ }
125
+
126
+ /// <summary>
127
+ /// Returns HexDirection rotated counter-clockwise once.
128
+ /// </summary>
129
+ public static HexDirection Left ( this HexDirection direction ) {
130
+ return ( HexDirection ) ( ( ( int ) direction + 5 ) % 6 ) ;
131
+ }
132
+
133
+ /// <summary>
134
+ /// Returns the opposite HexDirection.
135
+ /// </summary>
136
+ public static HexDirection Opposite ( this HexDirection direction ) {
137
+ return ( HexDirection ) ( ( ( int ) direction + 3 ) % 6 ) ;
138
+ }
139
+
140
+ /// <summary>
141
+ /// If given HexCoords are coords of any HexDirection, it returns given HexDirection.
142
+ /// Otherwise it throws an error.
143
+ /// </summary>
144
+ public static HexDirection ToDirection ( this HexCoords coords ) {
145
+ return ( HexDirection ) Array . IndexOf ( _coords , coords ) ;
146
+ }
147
+ }
148
+ }
0 commit comments