GodotComponentTest/tests/HexCellTests.cs

290 lines
9.8 KiB
C#
Raw Permalink Normal View History

2022-12-02 21:09:40 +01:00
using System.Diagnostics;
using System.Linq;
2023-11-18 22:32:57 +01:00
using Godot;
using GoDotTest;
using Xunit;
2022-12-02 21:09:40 +01:00
2023-11-18 22:32:57 +01:00
public class HexCellTests : TestClass {
public HexCellTests(Node testScene) : base(testScene) { }
2022-12-02 21:09:40 +01:00
[Test]
2023-11-18 22:32:57 +01:00
public void TestHexCellSimple() {
HexCell cell = new();
Debug.Assert(cell.CubeCoords == new Vector3(0, 0, 0));
2022-12-02 21:09:40 +01:00
}
[Test]
2023-11-18 22:32:57 +01:00
public void TestHexCellEqualityInequality() {
HexCell cellA = new();
HexCell cellB = new();
cellA.AxialCoords = new Vector2(2, 3);
cellB.AxialCoords = new Vector2(2, 3);
Assert.Equal(cellA, cellB);
bool result = cellA == cellB;
Assert.True(cellA == cellB);
Assert.False(cellA != cellB);
2023-08-13 21:18:46 +02:00
cellB.AxialCoords = new Vector2(3, 2);
Assert.NotEqual(cellA, cellB);
Assert.False(cellA == cellB);
2023-08-13 21:18:46 +02:00
Assert.True(cellA != cellB);
}
2022-12-02 21:09:40 +01:00
[Test]
2023-11-18 22:32:57 +01:00
public void TestAxialCoords() {
HexCell cell = new(1, 1, -2);
2022-12-02 21:09:40 +01:00
Debug.Assert(cell.AxialCoords == new Vector2(1, 1));
cell = new HexCell(1, -1);
Debug.Assert(cell.AxialCoords == new Vector2(1, -1));
cell = new HexCell(new Vector3(-1, 2, -1));
HexCell otherCell = cell;
Debug.Assert(otherCell.AxialCoords == new Vector2(-1, 2));
2022-12-02 21:09:40 +01:00
}
[Test]
2023-11-18 22:32:57 +01:00
public void TestAxialCoordsRounded() {
HexCell cell = new(new Vector2(-0.1f, 0.6f));
2022-12-02 21:09:40 +01:00
Debug.Assert(cell.CubeCoords == new Vector3(0, 1, -1));
cell = new HexCell(new Vector2(4.2f, -5.5f));
Debug.Assert(cell.CubeCoords == new Vector3(4, -5, 1));
}
2022-12-02 21:09:40 +01:00
[Test]
2023-11-18 22:32:57 +01:00
public void TestConversion() {
HexCell cell = new();
2022-12-02 21:09:40 +01:00
Debug.Assert(cell.AxialToCubeCoords(new Vector2(2, 1)) == new Vector3(2, 1, -3));
Debug.Assert(cell.AxialToCubeCoords(new Vector2(-1, -1)) == new Vector3(-1, -1, 2));
}
[Test]
2023-11-18 22:32:57 +01:00
public void TestRounding() {
HexCell cell = new();
2022-12-02 21:09:40 +01:00
Debug.Assert(cell.RoundCoords(new Vector3(0.1f, 0.5f, -0.6f)) == new Vector3(0, 1, -1));
Debug.Assert(cell.RoundCoords(new Vector3(-0.4f, -1.3f, 1.7f)) == new Vector3(-1, -1, 2));
2022-12-02 21:09:40 +01:00
Debug.Assert(cell.RoundCoords(new Vector2(-0.1f, 0.6f)) == new Vector3(0, 1, -1));
Debug.Assert(cell.RoundCoords(new Vector2(4.2f, -5.5f)) == new Vector3(4, -5, 1));
}
[Test]
2023-11-18 22:32:57 +01:00
public void TestCoords() {
HexCell cell = new();
2022-12-02 21:09:40 +01:00
// from cubic positive
cell.CubeCoords = new Vector3(2, 1, -3);
Debug.Assert(cell.CubeCoords == new Vector3(2, 1, -3));
Debug.Assert(cell.AxialCoords == new Vector2(2, 1));
Debug.Assert(cell.OffsetCoords == new Vector2(2, 2));
// from offset positive
cell.OffsetCoords = new Vector2(2, 2);
Debug.Assert(cell.CubeCoords == new Vector3(2, 1, -3));
Debug.Assert(cell.AxialCoords == new Vector2(2, 1));
Debug.Assert(cell.OffsetCoords == new Vector2(2, 2));
2022-12-02 21:09:40 +01:00
// from offset negative
cell.OffsetCoords = new Vector2(-1, -2);
Debug.Assert(cell.CubeCoords == new Vector3(-1, -1, 2));
Debug.Assert(cell.AxialCoords == new Vector2(-1, -1));
Debug.Assert(cell.OffsetCoords == new Vector2(-1, -2));
}
[Test]
2023-11-18 22:32:57 +01:00
public void TestNearby() {
HexCell cell = new(new Vector2(1, 2));
2022-12-02 21:09:40 +01:00
// adjacent
HexCell otherCell = cell.GetAdjacent(HexCell.DIR_N);
2022-12-02 21:09:40 +01:00
Debug.Assert(otherCell.AxialCoords == new Vector2(1, 3));
otherCell = cell.GetAdjacent(HexCell.DIR_NE);
2022-12-02 21:09:40 +01:00
Debug.Assert(otherCell.AxialCoords == new Vector2(2, 2));
otherCell = cell.GetAdjacent(HexCell.DIR_SE);
2022-12-02 21:09:40 +01:00
Debug.Assert(otherCell.AxialCoords == new Vector2(2, 1));
otherCell = cell.GetAdjacent(HexCell.DIR_S);
2022-12-02 21:09:40 +01:00
Debug.Assert(otherCell.AxialCoords == new Vector2(1, 1));
otherCell = cell.GetAdjacent(HexCell.DIR_SW);
2022-12-02 21:09:40 +01:00
Debug.Assert(otherCell.AxialCoords == new Vector2(0, 2));
otherCell = cell.GetAdjacent(HexCell.DIR_NW);
2022-12-02 21:09:40 +01:00
Debug.Assert(otherCell.AxialCoords == new Vector2(0, 3));
2022-12-02 21:09:40 +01:00
// not really adjacent
otherCell = cell.GetAdjacent(new Vector3(-3, -3, 6));
2022-12-02 21:09:40 +01:00
Debug.Assert(otherCell.AxialCoords == new Vector2(-2, -1));
}
[Test]
2023-11-18 22:32:57 +01:00
public void TestDistance() {
HexCell cell = new();
cell.OffsetCoords = new Vector2(1, 2);
2023-08-13 21:18:46 +02:00
Debug.Assert(cell.DistanceTo(new HexCell(new Vector2(0, 0))) == 3);
Debug.Assert(cell.DistanceTo(new HexCell(new Vector2(3, 4))) == 4);
Debug.Assert(cell.DistanceTo(new HexCell(new Vector2(-1, -1))) == 5);
}
2023-08-13 21:18:46 +02:00
[Test]
2023-11-18 22:32:57 +01:00
public void TestLineTo() {
HexCell cell = new();
cell.OffsetCoords = new Vector2(1, 2);
HexCell[] path = cell.LineTo(new HexCell(5, 2));
2023-11-18 22:32:57 +01:00
HexCell[] pathExpected = {
new(1, 2),
new(2, 2),
new(3, 2),
new(4, 2),
new(5, 2)
};
2023-08-13 21:18:46 +02:00
Debug.Assert(path.Length == pathExpected.Length);
2023-11-18 22:32:57 +01:00
foreach (int index in Enumerable.Range(0, path.Length)) {
Debug.Assert(path[index].AxialCoords == pathExpected[index].AxialCoords);
}
}
[Test]
2023-11-18 22:32:57 +01:00
public void TestLineToAngled() {
HexCell cell = new();
cell.OffsetCoords = new Vector2(1, 2);
HexCell[] path = cell.LineTo(new HexCell(5, 4));
2023-11-18 22:32:57 +01:00
HexCell[] pathExpected = {
new(1, 2),
new(2, 2),
new(2, 3),
new(3, 3),
new(4, 3),
new(4, 4),
new(5, 4)
};
2023-08-13 21:18:46 +02:00
Debug.Assert(path.Length == pathExpected.Length);
2023-11-18 22:32:57 +01:00
foreach (int index in Enumerable.Range(0, path.Length)) {
Debug.Assert(path[index].AxialCoords == pathExpected[index].AxialCoords);
}
}
2023-08-13 21:18:46 +02:00
[Test]
2023-11-18 22:32:57 +01:00
public void TestLineEdge() {
HexCell cell = new();
cell.OffsetCoords = new Vector2(1, 2);
HexCell[] path = cell.LineTo(new HexCell(3, 4));
2023-11-18 22:32:57 +01:00
HexCell[] pathExpected = {
new(1, 2),
new(2, 2),
new(2, 3),
new(2, 4),
new(3, 4)
};
2023-08-13 21:18:46 +02:00
Debug.Assert(path.Length == pathExpected.Length);
2023-11-18 22:32:57 +01:00
foreach (int index in Enumerable.Range(0, path.Length)) {
2023-08-13 21:18:46 +02:00
Debug.Print("index: " + index + " path: " + path[index].AxialCoords + " expected: " +
pathExpected[index].AxialCoords);
Debug.Assert(path[index].AxialCoords == pathExpected[index].AxialCoords);
}
}
2023-08-13 21:18:46 +02:00
[Test]
2023-11-18 22:32:57 +01:00
public void TestCellDirections() {
HexCell cell = new();
2023-08-13 21:18:46 +02:00
HexCell cellN = HexCell.FromOffsetCoords(new Vector2(0, 1));
HexCell cellNW = HexCell.FromOffsetCoords(new Vector2(-1, 0));
HexCell cellSW = HexCell.FromOffsetCoords(new Vector2(-1, -1));
HexCell cellS = HexCell.FromOffsetCoords(new Vector2(0, -1));
HexCell cellSE = HexCell.FromOffsetCoords(new Vector2(1, -1));
HexCell cellNE = HexCell.FromOffsetCoords(new Vector2(1, 0));
HexCell neighbour = cell.GetAdjacent(HexCell.DIR_N);
Assert.Equal(cellN, neighbour);
neighbour = cell.GetAdjacent(HexCell.DIR_NW);
Assert.Equal(cellNW, neighbour);
neighbour = cell.GetAdjacent(HexCell.DIR_SW);
Assert.Equal(cellSW, neighbour);
neighbour = cell.GetAdjacent(HexCell.DIR_S);
Assert.Equal(cellS, neighbour);
neighbour = cell.GetAdjacent(HexCell.DIR_SE);
Assert.Equal(cellSE, neighbour);
neighbour = cell.GetAdjacent(HexCell.DIR_NW);
Assert.Equal(cellNW, neighbour);
}
[Test]
2023-11-18 22:32:57 +01:00
public void TestCellDirectionsNonzeroReference() {
2023-08-13 21:18:46 +02:00
HexCell cell = HexCell.FromOffsetCoords(new Vector2(-4, -3));
HexCell cellN = HexCell.FromOffsetCoords(new Vector2(-4, -2));
HexCell cellNW = HexCell.FromOffsetCoords(new Vector2(-5, -3));
HexCell cellSW = HexCell.FromOffsetCoords(new Vector2(-5, -4));
HexCell cellS = HexCell.FromOffsetCoords(new Vector2(-4, -4));
HexCell cellSE = HexCell.FromOffsetCoords(new Vector2(-3, -4));
HexCell cellNE = HexCell.FromOffsetCoords(new Vector2(-3, -3));
HexCell neighbour = cell.GetAdjacent(HexCell.DIR_N);
Assert.Equal(cellN, neighbour);
neighbour = cell.GetAdjacent(HexCell.DIR_NW);
Assert.Equal(cellNW, neighbour);
neighbour = cell.GetAdjacent(HexCell.DIR_SW);
Assert.Equal(cellSW, neighbour);
neighbour = cell.GetAdjacent(HexCell.DIR_S);
Assert.Equal(cellS, neighbour);
neighbour = cell.GetAdjacent(HexCell.DIR_SE);
Assert.Equal(cellSE, neighbour);
neighbour = cell.GetAdjacent(HexCell.DIR_NW);
Assert.Equal(cellNW, neighbour);
}
[Test]
2023-11-18 22:32:57 +01:00
public void TestNextCellAlongLine() {
HexCell cell = new();
2023-08-13 21:18:46 +02:00
HexCell cellN = HexCell.FromOffsetCoords(new Vector2(0, 1));
HexCell cellNE = HexCell.FromOffsetCoords(new Vector2(1, 0));
HexCell cellSE = HexCell.FromOffsetCoords(new Vector2(1, -1));
HexCell cellS = HexCell.FromOffsetCoords(new Vector2(0, -1));
HexCell cellSW = HexCell.FromOffsetCoords(new Vector2(-1, -1));
HexCell cellNW = HexCell.FromOffsetCoords(new Vector2(-1, 0));
HexCell nextCell = cell.NextCellAlongLine(new Vector2(0, 0), new Vector2(0, 1));
Assert.Equal(cellS, nextCell);
nextCell = cell.NextCellAlongLine(new Vector2(0, 0), new Vector2(1, 1).Normalized());
Assert.Equal(cellSE, nextCell);
nextCell = cell.NextCellAlongLine(new Vector2(0, 0), new Vector2(1, -1).Normalized());
Assert.Equal(cellNE, nextCell);
nextCell = cell.NextCellAlongLine(new Vector2(0, 0), new Vector2(0, -1));
Assert.Equal(cellN, nextCell);
nextCell = cell.NextCellAlongLine(new Vector2(0, 0), new Vector2(-1, -1).Normalized());
Assert.Equal(cellNW, nextCell);
nextCell = cell.NextCellAlongLine(new Vector2(0, 0), new Vector2(-1, 1).Normalized());
Assert.Equal(cellSW, nextCell);
}
2022-12-02 21:09:40 +01:00
}