GodotComponentTest/tests/HexGridTests.cs

94 lines
3.7 KiB
C#
Raw Normal View History

2023-08-13 21:18:46 +02:00
using System.Collections.Generic;
2023-11-18 22:32:57 +01:00
using System.Diagnostics;
2022-12-02 21:09:40 +01:00
using Godot;
using GoDotTest;
2023-08-13 21:18:46 +02:00
using Xunit;
2022-12-02 21:09:40 +01:00
2023-11-18 22:32:57 +01:00
public class HexGridTests : TestClass {
public HexGridTests(Node testScene) : base(testScene) { }
2022-12-02 21:09:40 +01:00
[Test]
2023-11-18 22:32:57 +01:00
public void TestGetAt() {
HexGrid grid = new();
2022-12-02 21:09:40 +01:00
float w = grid.HexSize.x;
float h = grid.HexSize.y;
2022-12-02 21:09:40 +01:00
Debug.Assert(grid.GetHexAt(new Vector2(0, 0)).AxialCoords == new Vector2(0, 0));
Debug.Assert(grid.GetHexAt(new Vector2(w / 2 - 0.01f, 0)).AxialCoords == new Vector2(0, 0));
Debug.Assert(grid.GetHexAt(new Vector2(w / 2 - 0.01f, -h / 2)).AxialCoords == new Vector2(1, 0));
Debug.Assert(grid.GetHexAt(new Vector2(w / 2 - 0.01f, h / 2)).AxialCoords == new Vector2(1, -1));
2022-12-02 21:09:40 +01:00
}
2023-08-13 21:18:46 +02:00
[Test]
2023-11-18 22:32:57 +01:00
public void TestGetCellsForLineSimple() {
HexGrid grid = new();
2023-08-13 21:18:46 +02:00
List<HexCell> lineCells =
grid.GetCellsForLine(new Vector2(0, 0), grid.GetHexCenterFromOffset(new Vector2(0, 2)));
Assert.Equal(3, lineCells.Count);
Assert.Equal(HexCell.FromOffsetCoords(new Vector2(0, 0)), lineCells[0]);
Assert.Equal(HexCell.FromOffsetCoords(new Vector2(0, 1)), lineCells[1]);
Assert.Equal(HexCell.FromOffsetCoords(new Vector2(0, 2)), lineCells[2]);
lineCells =
grid.GetCellsForLine(grid.GetHexCenterFromOffset(new Vector2(0, 2)), new Vector2(0, 0));
Assert.Equal(3, lineCells.Count);
Assert.Equal(HexCell.FromOffsetCoords(new Vector2(0, 2)), lineCells[0]);
Assert.Equal(HexCell.FromOffsetCoords(new Vector2(0, 1)), lineCells[1]);
Assert.Equal(HexCell.FromOffsetCoords(new Vector2(0, 0)), lineCells[2]);
}
[Test]
2023-11-18 22:32:57 +01:00
public void TestGetCellsDiagonal() {
HexGrid grid = new();
2023-08-13 21:18:46 +02:00
List<HexCell> lineCells =
grid.GetCellsForLine(new Vector2(0, 0), grid.GetHexCenterFromOffset(new Vector2(2, 1)));
Assert.Equal(3, lineCells.Count);
Assert.Equal(HexCell.FromOffsetCoords(new Vector2(0, 0)), lineCells[0]);
Assert.Equal(HexCell.FromOffsetCoords(new Vector2(1, 0)), lineCells[1]);
Assert.Equal(HexCell.FromOffsetCoords(new Vector2(2, 1)), lineCells[2]);
lineCells =
grid.GetCellsForLine(grid.GetHexCenterFromOffset(new Vector2(2, 1)), new Vector2(0, 0));
Assert.Equal(3, lineCells.Count);
Assert.Equal(HexCell.FromOffsetCoords(new Vector2(2, 1)), lineCells[0]);
Assert.Equal(HexCell.FromOffsetCoords(new Vector2(1, 0)), lineCells[1]);
Assert.Equal(HexCell.FromOffsetCoords(new Vector2(0, 0)), lineCells[2]);
}
[Test]
2023-11-18 22:32:57 +01:00
public void TestGetCellsForLineAlongEdge() {
HexGrid grid = new();
2023-08-13 21:18:46 +02:00
List<HexCell> lineCells =
grid.GetCellsForLine(new Vector2(0, -0.0001f), grid.GetHexCenterFromOffset(new Vector2(2, 0)));
Assert.Equal(3, lineCells.Count);
Assert.Equal(HexCell.FromOffsetCoords(new Vector2(0, 0)), lineCells[0]);
Assert.Equal(HexCell.FromOffsetCoords(new Vector2(1, 0)), lineCells[1]);
Assert.Equal(HexCell.FromOffsetCoords(new Vector2(2, 0)), lineCells[2]);
lineCells =
grid.GetCellsForLine(new Vector2(0, 0.0001f), grid.GetHexCenterFromOffset(new Vector2(2, 0)));
Assert.Equal(3, lineCells.Count);
Assert.Equal(HexCell.FromOffsetCoords(new Vector2(0, 0)), lineCells[0]);
Assert.Equal(HexCell.FromOffsetCoords(new Vector2(1, -1)), lineCells[1]);
Assert.Equal(HexCell.FromOffsetCoords(new Vector2(2, 0)), lineCells[2]);
}
[Test]
2023-11-18 22:32:57 +01:00
public void GetTestsInfiniteLoop() {
HexGrid grid = new();
Vector2 fromPlane = new(-2.31678f, -5.024752f);
Vector2 toPlane = new(-2.599937f, -6.134028f);
List<HexCell> cellList = grid.GetCellsForLine(fromPlane, toPlane);
}
2022-12-02 21:09:40 +01:00
}