-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexGridPosition.cs
More file actions
34 lines (28 loc) · 1.2 KB
/
HexGridPosition.cs
File metadata and controls
34 lines (28 loc) · 1.2 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
namespace M9Studio.HexMatrix
{
public readonly struct HexGridPosition
{
public int X { get; }
public int Y { get; }
public int Z => -X - Y;
public HexGridPosition(int x, int y)
{
X = x;
Y = y;
}
public HexGridPosition(int x, int y, int z)
{
if (x + y + z != 0)
throw new ArgumentException("x + y + z must be 0");
X = x;
Y = y;
}
public HexGridPosition MoveX(int dx) => new HexGridPosition(X + dx, Y - dx);
public HexGridPosition MoveY(int dy) => new HexGridPosition(X - dy, Y + dy);
public HexGridPosition MoveZ(int dz) => new HexGridPosition(X + dz, Y - dz);
public static HexGridPosition operator +(HexGridPosition a, HexGridPosition b) => new HexGridPosition(a.X + b.X, a.Y + b.Y);
public static HexGridPosition operator -(HexGridPosition a, HexGridPosition b) => new HexGridPosition(a.X - b.X, a.Y - b.Y);
public int DistanceTo(HexGridPosition other) => (Math.Abs(X - other.X) + Math.Abs(Y - other.Y) + Math.Abs(Z - other.Z)) / 2;
public override string ToString() => $"({X}, {Y}, {Z})";
}
}