GodotComponentTest/HexCell.cs

247 lines
7.4 KiB
C#
Raw Normal View History

2022-12-02 21:09:40 +01:00
using System;
using System.Linq;
2022-12-02 21:09:40 +01:00
using Godot;
2023-08-13 21:18:46 +02:00
using GodotComponentTest.utils;
2022-12-02 21:09:40 +01:00
2023-11-18 22:32:57 +01:00
public class HexCell : IEquatable<HexCell> {
public override bool Equals(object obj) {
if (ReferenceEquals(null, obj)) {
return false;
}
if (ReferenceEquals(this, obj)) {
return true;
}
if (obj.GetType() != GetType()) {
return false;
}
return Equals((HexCell)obj);
}
2023-11-18 22:32:57 +01:00
public override int GetHashCode() {
return _cubeCoords.GetHashCode();
}
2023-08-13 21:18:46 +02:00
public static readonly Vector2 Size = new(1, Mathf.Sqrt(3) / 2);
private const float Width = 2;
private static readonly float Height = Mathf.Sqrt(3);
2022-12-02 21:09:40 +01:00
2023-08-13 21:18:46 +02:00
public static readonly Vector3 DIR_N = new(0, 1, -1);
public static readonly Vector3 DIR_NE = new(1, 0, -1);
public static readonly Vector3 DIR_SE = new(1, -1, 0);
public static readonly Vector3 DIR_S = new(0, -1, 1);
public static readonly Vector3 DIR_SW = new(-1, 0, 1);
public static readonly Vector3 DIR_NW = new(-1, 1, 0);
2023-11-18 22:32:57 +01:00
public static readonly Vector3[] NeighborDirections = {
2023-08-13 21:18:46 +02:00
DIR_N,
DIR_NW,
DIR_SW,
DIR_S,
DIR_SE,
DIR_NE
};
2023-11-18 22:32:57 +01:00
private static readonly Vector2 CornerNW = new(-Width / 4, -Height / 2);
private static readonly Vector2 CornerNE = new(Width / 4, -Height / 2);
private static readonly Vector2 CornerE = new(Width / 2, 0);
private static readonly Vector2 CornerSE = new(Width / 4, Height / 2);
private static readonly Vector2 CornerSW = new(-Width / 4, Height / 2);
private static readonly Vector2 CornerW = new(-Width / 2, 0);
private static readonly Vector2 PlaneNormalN = new(0, 1);
2023-08-13 21:18:46 +02:00
private static readonly Vector2 PlaneNormalNE =
-new Vector2(Mathf.Cos(Mathf.Deg2Rad(30)), -Mathf.Sin(Mathf.Deg2Rad(30)));
private static readonly Vector2 PlaneNormalSE =
-new Vector2(Mathf.Cos(Mathf.Deg2Rad(-30)), -Mathf.Sin(Mathf.Deg2Rad(-30)));
2023-11-18 22:32:57 +01:00
private static readonly Vector2 PlaneNormalS = new(0, -1);
2023-08-13 21:18:46 +02:00
2023-11-18 22:32:57 +01:00
private static readonly Vector2 PlaneNormalSW = new(Mathf.Cos(Mathf.Deg2Rad(30)), -Mathf.Sin(Mathf.Deg2Rad(30)));
2023-08-13 21:18:46 +02:00
2023-11-18 22:32:57 +01:00
private static readonly Vector2 PlaneNormalNW = new(Mathf.Cos(Mathf.Deg2Rad(-30)), -Mathf.Sin(Mathf.Deg2Rad(-30)));
2023-08-13 21:18:46 +02:00
2023-11-18 22:32:57 +01:00
private static readonly Plane2D[] BoundaryPlanes = {
new(CornerNE, PlaneNormalN),
new(CornerNW, PlaneNormalNW),
new(CornerW, PlaneNormalSW),
new(CornerSW, PlaneNormalS),
new(CornerSE, PlaneNormalSE),
new(CornerE, PlaneNormalNE)
2023-08-13 21:18:46 +02:00
};
2023-11-18 22:32:57 +01:00
public HexCell() { }
2023-11-18 22:32:57 +01:00
public HexCell(float cubeX, float cubeY, float cubeZ) {
2022-12-02 21:09:40 +01:00
CubeCoords = RoundCoords(new Vector3(cubeX, cubeY, cubeZ));
}
2023-11-18 22:32:57 +01:00
public virtual bool Equals(HexCell other) {
if (other == null) {
2023-08-12 14:27:30 +02:00
return false;
}
2023-08-13 21:18:46 +02:00
2023-08-12 14:27:30 +02:00
return CubeCoords == other.CubeCoords;
}
2023-11-18 22:32:57 +01:00
public static bool operator ==(HexCell cellA, HexCell cellB) {
return Equals(cellA, cellB);
}
2023-08-13 21:18:46 +02:00
2023-11-18 22:32:57 +01:00
public static bool operator !=(HexCell cellA, HexCell cellB) {
return !(cellA == cellB);
}
2023-08-13 21:18:46 +02:00
2023-11-18 22:32:57 +01:00
public HexCell(Vector3 cubeCoords) {
2022-12-02 21:09:40 +01:00
CubeCoords = cubeCoords;
}
2023-11-18 22:32:57 +01:00
public HexCell(float axialX, float axialY) {
2022-12-02 21:09:40 +01:00
AxialCoords = new Vector2(axialX, axialY);
}
2023-11-18 22:32:57 +01:00
public HexCell(Vector2 axialCoords) {
2022-12-02 21:09:40 +01:00
AxialCoords = axialCoords;
}
2023-11-18 22:32:57 +01:00
public HexCell(HexCell other) {
2022-12-02 21:09:40 +01:00
CubeCoords = other.CubeCoords;
}
2023-11-18 22:32:57 +01:00
public static HexCell FromOffsetCoords(Vector2 offsetCoords) {
HexCell result = new();
result.OffsetCoords = offsetCoords;
return result;
}
2022-12-02 21:09:40 +01:00
private Vector3 _cubeCoords;
2023-11-18 22:32:57 +01:00
public Vector3 CubeCoords {
get => _cubeCoords;
set {
if (Mathf.Abs(value.x + value.y + value.z) > 0.0001) {
2022-12-02 21:09:40 +01:00
GD.Print("Warning: Invalid cube coordinates for hex (x + y + z != 0): ", value.ToString());
}
2022-12-02 21:09:40 +01:00
_cubeCoords = RoundCoords(value);
}
}
2023-11-18 22:32:57 +01:00
public Vector2 AxialCoords {
get => new(CubeCoords.x, CubeCoords.y);
2022-12-02 21:09:40 +01:00
2023-11-18 22:32:57 +01:00
set => CubeCoords = AxialToCubeCoords(value);
2022-12-02 21:09:40 +01:00
}
2023-11-18 22:32:57 +01:00
public Vector2 OffsetCoords {
get {
2022-12-02 21:09:40 +01:00
int x = (int)CubeCoords.x;
int y = (int)CubeCoords.y;
2023-08-13 21:18:46 +02:00
int offY = y + (x - (x & 1)) / 2;
return new Vector2(x, offY);
2022-12-02 21:09:40 +01:00
}
2023-11-18 22:32:57 +01:00
set {
2022-12-02 21:09:40 +01:00
int x = (int)value.x;
int y = (int)value.y;
2023-08-13 21:18:46 +02:00
int cubeY = y - (x - (x & 1)) / 2;
AxialCoords = new Vector2(x, cubeY);
2022-12-02 21:09:40 +01:00
}
}
2023-11-18 22:32:57 +01:00
public Vector3 AxialToCubeCoords(Vector2 axialCoords) {
2022-12-02 21:09:40 +01:00
return new Vector3(axialCoords.x, axialCoords.y, -axialCoords.x - axialCoords.y);
}
2023-11-18 22:32:57 +01:00
public Vector3 RoundCoords(Vector2 coords) {
2022-12-02 21:09:40 +01:00
Vector3 cubeCoords = AxialToCubeCoords(coords);
return RoundCoords(cubeCoords);
}
2023-11-18 22:32:57 +01:00
public Vector3 RoundCoords(Vector3 cubeCoords) {
Vector3 rounded = new(
2022-12-02 21:09:40 +01:00
Mathf.Round(cubeCoords.x),
Mathf.Round(cubeCoords.y),
Mathf.Round(cubeCoords.z));
Vector3 diffs = (rounded - cubeCoords).Abs();
2023-11-18 22:32:57 +01:00
if (diffs.x > diffs.y && diffs.x > diffs.z) {
2022-12-02 21:09:40 +01:00
rounded.x = -rounded.y - rounded.z;
2023-11-18 22:32:57 +01:00
} else if (diffs.y > diffs.z) {
2022-12-02 21:09:40 +01:00
rounded.y = -rounded.x - rounded.z;
2023-11-18 22:32:57 +01:00
} else {
2022-12-02 21:09:40 +01:00
rounded.z = -rounded.x - rounded.y;
}
return rounded;
}
2023-11-18 22:32:57 +01:00
public HexCell GetAdjacent(Vector3 dir) {
return new HexCell(CubeCoords + dir);
2022-12-02 21:09:40 +01:00
}
2023-11-18 22:32:57 +01:00
public HexCell[] GetAllAdjacent() {
return new[] {
2023-08-13 21:18:46 +02:00
GetAdjacent(DIR_NE),
GetAdjacent(DIR_SE),
GetAdjacent(DIR_S),
GetAdjacent(DIR_SW),
GetAdjacent(DIR_NW),
GetAdjacent(DIR_N)
};
}
2023-11-18 22:32:57 +01:00
public int DistanceTo(HexCell target) {
return (int)(
Mathf.Abs(_cubeCoords.x - target.CubeCoords.x)
+ Mathf.Abs(_cubeCoords.y - target.CubeCoords.y)
+ Mathf.Abs(_cubeCoords.z - target.CubeCoords.z)
) / 2;
}
2023-11-18 22:32:57 +01:00
public HexCell[] LineTo(HexCell target) {
HexCell nudgedTarget = new();
nudgedTarget.CubeCoords = target.CubeCoords + new Vector3(1.0e-6f, 2.0e-6f, -3.0e-6f);
int steps = DistanceTo(target);
HexCell[] path = new HexCell[steps + 1];
2023-11-18 22:32:57 +01:00
foreach (int dist in Enumerable.Range(0, steps)) {
path[dist] = new HexCell();
path[dist].CubeCoords = CubeCoords.LinearInterpolate(nudgedTarget.CubeCoords, (float)dist / steps);
}
path[steps] = target;
return path;
}
2023-08-13 21:18:46 +02:00
2023-11-18 22:32:57 +01:00
public void QueryClosestCellBoundary(Vector2 pointLocal, Vector2 dir, out int neighbourIndex, out float distance) {
distance = float.PositiveInfinity;
2023-08-13 21:18:46 +02:00
neighbourIndex = 0;
2023-11-18 22:32:57 +01:00
foreach (int i in Enumerable.Range(0, 6)) {
if (BoundaryPlanes[i].Normal.Dot(dir) >= Plane2D.DistancePrecision) {
2023-08-13 21:18:46 +02:00
continue;
}
float planeDistance = BoundaryPlanes[i].DistanceToLineSegment(pointLocal, dir);
2023-11-18 22:32:57 +01:00
if (planeDistance > float.NegativeInfinity && planeDistance < distance) {
2023-08-13 21:18:46 +02:00
distance = planeDistance;
neighbourIndex = i;
}
}
}
2023-11-18 22:32:57 +01:00
public HexCell NextCellAlongLine(Vector2 pointLocal, Vector2 dir) {
2023-08-13 21:18:46 +02:00
int planeIndex;
QueryClosestCellBoundary(pointLocal, dir, out planeIndex, out _);
return GetAdjacent(NeighborDirections[planeIndex]);
}
2022-12-02 21:09:40 +01:00
}