Skip to content

Commit 62aa10a

Browse files
committed
HexPosition class added
1 parent dc81202 commit 62aa10a

File tree

6 files changed

+97
-11
lines changed

6 files changed

+97
-11
lines changed

LICENSE.meta

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# HexEngine
22
HexEngine is a library for working with hex coordinates for Unity.
33

4-
It is inspired by http://www.redblobgames.com/grids/hexagons/.
4+
Inspired by http://www.redblobgames.com/grids/hexagons/.
5+

Runtime/HexMath.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,27 @@ public static class HexMath {
1313
/// Returns HexCoords of hex on given point (ignoring point.y value).
1414
/// </summary>
1515
public static HexCoords PointToHexCoords(Vector3 point, float gridScale = 1f) {
16-
return RoundAxialToHex(PointToAxial(point, gridScale));
16+
return RoundPositionToCoords(PointToHexPosition(point, gridScale));
1717
}
1818

1919
/// <summary>
2020
/// Converts 3D point to Vector2 containing axial position.
2121
/// </summary>
22-
public static Vector2 PointToAxial(Vector3 point, float gridScale = 1f) {
22+
public static HexPosition PointToHexPosition(Vector3 point, float gridScale = 1f) {
2323
var x = B * point.x;
2424
var y = (A * point.x + point.z);
25-
return new Vector2(x, y) / gridScale;
25+
return new HexPosition(x / gridScale, y / gridScale);
2626
}
2727

2828
/// <summary>
2929
/// Rounds axial coordinates and converts them to the HexCoords.
3030
/// </summary>
31-
public static HexCoords RoundAxialToHex(Vector2 axial) {
32-
var x = axial.x;
33-
var y = axial.y;
34-
var z = -axial.x - axial.y;
35-
var xr = Mathf.RoundToInt(axial.x);
36-
var yr = Mathf.RoundToInt(axial.y);
31+
public static HexCoords RoundPositionToCoords(HexPosition position) {
32+
var x = position.X;
33+
var y = position.Y;
34+
var z = position.Z;
35+
var xr = Mathf.RoundToInt(x);
36+
var yr = Mathf.RoundToInt(y);
3737
var zr = Mathf.RoundToInt(z);
3838

3939
var diffX = Mathf.Abs(x - xr);

Runtime/HexPosition.cs

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace HexEngine {
5+
public struct HexPosition : IEquatable<HexPosition> {
6+
public static readonly HexPosition Zero = new HexPosition();
7+
8+
[SerializeField]
9+
private float _x;
10+
[SerializeField]
11+
private float _y;
12+
13+
public float X {
14+
get => _x;
15+
set => _x = value;
16+
}
17+
18+
public float Y {
19+
get => _y;
20+
set => _y = value;
21+
}
22+
23+
public float Z => -X - Y;
24+
25+
public HexPosition(float x, float y) {
26+
_x = x;
27+
_y = y;
28+
}
29+
30+
public static HexPosition operator +(HexPosition a, HexPosition b)
31+
=> new HexPosition(a.X + b.X, a.Y + b.Y);
32+
33+
public static HexPosition operator +(HexPosition a, HexDirection b)
34+
=> a + b.Coords();
35+
36+
public static HexPosition operator -(HexPosition a, HexPosition b)
37+
=> new HexPosition(a.X - b.X, a.Y - b.Y);
38+
39+
public static HexPosition operator -(HexPosition a, HexDirection b)
40+
=> a - b.Coords();
41+
42+
public static implicit operator HexPosition(HexCoords coords) => new HexPosition(coords.X, coords.Y);
43+
44+
/// <summary>
45+
/// The offset from the Vector3.zero to the center of the hex.
46+
/// </summary>
47+
public Vector3 Offset() {
48+
return HexDirection.NE.Direction() * X + HexDirection.N.Direction() * Y;
49+
}
50+
51+
public bool Equals(HexPosition other) {
52+
return other.X == X && other.Y == Y;
53+
}
54+
55+
public override bool Equals(object obj) {
56+
return obj is HexCoords coords && Equals(coords);
57+
}
58+
59+
public override int GetHashCode() {
60+
return Tuple.Create(_x, _y).GetHashCode();
61+
}
62+
63+
public override string ToString() {
64+
return $"({X}, {Y}, {Z})";
65+
}
66+
67+
public static bool operator ==(HexPosition a, HexPosition b) {
68+
return a.Equals(b);
69+
}
70+
71+
public static bool operator !=(HexPosition a, HexPosition b) {
72+
return !(a == b);
73+
}
74+
}
75+
}

Runtime/HexPosition.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.juna8001.hexengine",
33
"displayName": "Hex Engine",
4-
"version": "0.1.0",
4+
"version": "0.1.1",
55
"unity": "2018.1",
66
"description": "HexEngine is a library for working with hex coordinates for Unity.",
77
"keywords": [

0 commit comments

Comments
 (0)