wangguan
2020-11-17 551101abeb1a997da1444ba4ed3059b4d878fb4a
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
using System;
using UnityEngine;
 
namespace Core.Utilities
{
    /// <summary>
    /// Structure to contain cubic coordinates for hexagonal grids. Provides a derived Z coordinate where
    /// z = x + y, providing a new third axis
    /// </summary>
    public struct HexPoint : IEquatable<HexPoint>
    {
        /// <summary>
        /// X-coordinate of hexagon point
        /// </summary>
        public readonly int x;
 
        /// <summary>
        /// Y-coordinate of hexagon point
        /// </summary>
        public readonly int y;
 
        /// <summary>
        /// Z-coordinate of hexagon point. This value is derived from x and y
        /// </summary>
        public readonly int z;
 
        /// <summary>
        /// Calculates the magnitude of this Hex point vector (its hex distance from the origin
        /// </summary>
        public int magnitude
        {
            get { return (Mathf.Abs(x) + Mathf.Abs(y) + Mathf.Abs(z)) / 2; }
        }
 
        /// <summary>
        /// Initialize a new hex point with two x,y coordinates
        /// </summary>
        public HexPoint(int x, int y)
        {
            this.x = x;
            this.y = y;
            this.z = x + y;
        }
 
        /// <summary>
        /// Initialized a new hex point with x and z coordinates
        /// </summary>
        public static HexPoint FromXZ(int x, int z)
        {
            int y = z - x;
            return new HexPoint(x, y);
        }
 
        /// <summary>
        /// Initialized a new hex point with y and z coordinates
        /// </summary>
        public static HexPoint FromYZ(int y, int z)
        {
            int x = z - y;
            return new HexPoint(x, y);
        }
 
        public bool Equals(HexPoint other)
        {
            return other.x == x && other.y == y;
        }
 
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
            {
                return false;
            }
            return obj is HexPoint && Equals((HexPoint) obj);
        }
 
        /// <summary>
        /// Simple hash multiplying by two primes
        /// </summary>
        public override int GetHashCode()
        {
            unchecked
            {
                return (x.GetHashCode() * 22447) ^ (y.GetHashCode() * 31);
            }
        }
 
        public override string ToString()
        {
            return string.Format("X: {0}, Y: {1}, Z: {2}", x, y, z);
        }
 
        /// <summary>
        /// Rotate the given hex point by 60 degrees counterclockwise, around the origin
        /// </summary>
        public static HexPoint RotateLeft(HexPoint original)
        {
            return new HexPoint(-original.y, original.z);
        }
 
        /// <summary>
        /// Rotate the given hex point by 60 degrees counterclockwise, around the given point
        /// </summary>
        public static HexPoint RotateLeft(HexPoint original, HexPoint origin)
        {
            return RotateLeft(original - origin) + origin;
        }
 
        /// <summary>
        /// Rotate the given hex point by 60 degrees clockwise, around the origin
        /// </summary>
        public static HexPoint RotateRight(HexPoint original)
        {
            return new HexPoint(original.z, -original.x);
        }
 
        /// <summary>
        /// Rotate the given hex point by 60 degrees clockwise, around the given point
        /// </summary>
        public static HexPoint RotateRight(HexPoint original, HexPoint origin)
        {
            return RotateRight(original - origin) + origin;
        }
 
        /// <summary>
        /// Rotate the given hex point by 120 degrees counterclockwise, around the origin
        /// </summary>
        public static HexPoint RotateLeft120(HexPoint original)
        {
            return new HexPoint(-original.z, original.x);
        }
 
        /// <summary>
        /// Rotate the given hex point by 120 degrees counterclockwise, around the the given point
        /// </summary>
        public static HexPoint RotateLeft120(HexPoint original, HexPoint origin)
        {
            return RotateLeft120(original - origin) + origin;
        }
 
        /// <summary>
        /// Rotate the given hex point by 120 degrees clockwise, around the origin
        /// </summary>
        public static HexPoint RotateRight120(HexPoint original)
        {
            return new HexPoint(original.y, -original.z);
        }
 
        /// <summary>
        /// Rotate the given hex point by 120 degrees clockwise, around the the given point
        /// </summary>
        public static HexPoint RotateRight120(HexPoint original, HexPoint origin)
        {
            return RotateRight120(original - origin) + origin;
        }
 
        /// <summary>
        /// Rotate the given hex point 180 degrees around the origin
        /// </summary>
        public static HexPoint Rotate180(HexPoint original)
        {
            return new HexPoint(-original.x, -original.y);
        }
 
        /// <summary>
        /// Rotate the given hex point 180 degrees around the the given point
        /// </summary>
        public static HexPoint Rotate180(HexPoint original, HexPoint origin)
        {
            return Rotate180(original - origin) + origin;
        }
 
        /// <summary>
        /// Reflect the given hex point around the x-axis
        /// </summary>
        public static HexPoint ReflectX(HexPoint original)
        {
            int x = original.z;
            int y = -original.y;
 
            return new HexPoint(x, y);
        }
 
        /// <summary>
        /// Reflect the given hex point around the line where y is the given value
        /// </summary>
        public static HexPoint ReflectX(HexPoint original, int y)
        {
            var offset = new HexPoint(0, y);
 
            return ReflectX(original - offset) + offset;
        }
 
        /// <summary>
        /// Reflect the given hex point around the y-axis
        /// </summary>
        public static HexPoint ReflectY(HexPoint original)
        {
            int x = -original.x;
            int y = original.z;
 
            return new HexPoint(x, y);
        }
 
        /// <summary>
        /// Reflect the given hex point around the line where x is the given value
        /// </summary>
        public static HexPoint ReflectY(HexPoint original, int x)
        {
            var offset = new HexPoint(x, 0);
 
            return ReflectY(original - offset) + offset;
        }
 
        /// <summary>
        /// Reflect the given hex point around the z-axis
        /// </summary>
        public static HexPoint ReflectZ(HexPoint original)
        {
            int x = -original.y;
            int y = -original.x;
 
            return new HexPoint(x, y);
        }
 
        /// <summary>
        /// Reflect the given hex point around the line where z is the given value
        /// </summary>
        public static HexPoint ReflectZ(HexPoint original, int z)
        {
            var offset = FromXZ(0, z);
 
            return ReflectZ(original - offset) + offset;
        }
        
        // Math operators and conversions
        // Equality operators
        public static bool operator ==(HexPoint left, HexPoint right)
        {
            return left.Equals(right);
        }
 
        public static bool operator !=(HexPoint left, HexPoint right)
        {
            return !left.Equals(right);
        }
        
        // Conversion to and from IntVector2
        public static explicit operator IntVector2(HexPoint hexPoint)
        {
            return new IntVector2(hexPoint.x, hexPoint.y);
        }
        
        public static explicit operator HexPoint(IntVector2 vector)
        {
            return new HexPoint(vector.x, vector.y);
        }
        
        // Math operators
        public static HexPoint operator +(HexPoint left, HexPoint right)
        {
            return new HexPoint(left.x + right.x, left.y + right.y);
        }
 
        public static HexPoint operator -(HexPoint left, HexPoint right)
        {
            return new HexPoint(left.x - right.x, left.y - right.y);
        }
 
        public static HexPoint operator *(int scale, HexPoint right)
        {
            return new HexPoint(right.x * scale, right.y * scale);
        }
 
        public static HexPoint operator *(HexPoint left, int scale)
        {
            return new HexPoint(left.x * scale, left.y * scale);
        }
 
        public static HexPoint operator -(HexPoint left)
        {
            return new HexPoint(-left.x, -left.y);
        }
    }
}