-
Notifications
You must be signed in to change notification settings - Fork 0
/
CubeCoordinate.cs
105 lines (83 loc) · 2.47 KB
/
CubeCoordinate.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
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
using System;
using System.Collections.Generic;
using UnityEngine;
public class CubeCoordinate : IEquatable<CubeCoordinate>
{
private const int DirectionCount = 6;
public int Q { get; }
public int R { get; }
public int S { get; }
public static readonly CubeCoordinate DirQ = new(0, -1);
public static readonly CubeCoordinate DirR = new(1, 0);
public static readonly CubeCoordinate DirS = new(-1, 1);
public static List<CubeCoordinate> Directions => new() { DirQ, -DirS, DirR, -DirQ, DirS, -DirR };
public CubeCoordinate(int q, int r)
{
Q = q;
R = r;
S = -q - r;
}
public CubeCoordinate(int q, int r, int s)
{
Q = q;
R = r;
S = s;
}
#region Operators
public static bool operator ==(CubeCoordinate c1, CubeCoordinate c2)
{
if (c1 is null) return false;
if (c2 is null) return false;
return c1.Q == c2.Q && c1.R == c2.R;
}
public static bool operator !=(CubeCoordinate c1, CubeCoordinate c2)
{
if (c1 is null) return false;
if (c2 is null) return false;
return c1.Q != c2.Q || c1.R != c2.R;
}
public static CubeCoordinate operator +(CubeCoordinate c1, CubeCoordinate c2)
{
return new CubeCoordinate(c1.Q + c2.Q, c1.R + c2.R);
}
public static CubeCoordinate operator *(CubeCoordinate c1, int c2)
{
return new CubeCoordinate(c1.Q * c2, c1.R * c2);
}
public static CubeCoordinate operator -(CubeCoordinate c1)
{
return new CubeCoordinate(-c1.Q, -c1.R);
}
#endregion
#region Direction Functions
public static CubeCoordinate GetDirection(int d)
{
d = Mathf.Clamp(d, 0, DirectionCount);
return Directions[d];
}
public static CubeCoordinate RotateDirection(CubeCoordinate direction, int amount)
{
if (amount < 0) amount = DirectionCount - -amount % DirectionCount;
var index = Directions.IndexOf(direction) + amount;
return Directions[index % DirectionCount];
}
#endregion
#region Inherited Functions
public override int GetHashCode()
{
return HashCode.Combine(Q, R);
}
public override bool Equals(object obj)
{
return obj is not null && Equals((CubeCoordinate)obj);
}
public bool Equals(CubeCoordinate coordinate)
{
return this == coordinate;
}
public override string ToString()
{
return Q + "|" + R;
}
#endregion
}