-
Notifications
You must be signed in to change notification settings - Fork 19
/
HexCoordinate.cs
62 lines (52 loc) · 2 KB
/
HexCoordinate.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
// If you are using Unity 4.5.0 or higher this class is NOT needed.
using UnityEngine;
using System;
namespace Settworks.Hexagons {
/// <summary>Serializable hexagon grid coordinate.</summary>
/// <remarks><see cref="Settworks.Hexagons.HexCoord"/> is a struct for performance reasons, but Unity before v4.5.0 did not support serialization of structs. This serializable class is easily converted to and from <see cref="Settworks.Hexagons.HexCoord"/>, allowing it to be used in places where serialization is needed without affecting the performance of other logic.</remarks>
[Serializable]
public class HexCoordinate {
[SerializeField]
public int q;
[SerializeField]
public int r;
/// <summary>
/// The equivalent <see cref="Settworks.Hexagons.HexCoord"/>.
/// </summary>
public HexCoord HexCoord {
get {
return (HexCoord)this;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Settworks.Hexagons.HexCoordinate"/> class.
/// </summary>
public HexCoordinate() { }
/// <summary>
/// Initializes a new instance of the <see cref="Settworks.Hexagons.HexCoordinate"/> class
/// duplicating the Q and R values of a specified <see cref="Settworks.Hexagons.HexCoord"/>.
/// </summary>
/// <param name="hex"><see cref="Settworks.Hexagons.HexCoord"/> to duplicate.</param>
public HexCoordinate(HexCoord hex) {
Become(hex);
}
/// <summary>
/// Change the Q and R values of this <see cref="Settworks.Hexagons.HexCoordinate"/> to those
/// of the specified <see cref="Settworks.Hexagons.HexCoord"/>.
/// </summary>
/// <param name="hex"><see cref="Settworks.Hexagons.HexCoord"/> to become.</param>
public void Become(HexCoord hex) {
q = hex.q;
r = hex.r;
}
public bool Is(HexCoord hex) {
return this.q == hex.q && this.r == hex.r;
}
public static implicit operator HexCoord(HexCoordinate hex) {
return new HexCoord(hex.q, hex.r);
}
public static implicit operator HexCoordinate(HexCoord hex) {
return new HexCoordinate(hex);
}
}
}