102 lines
3.8 KiB
C#
102 lines
3.8 KiB
C#
using System.Collections.Generic;
|
|
using Godot;
|
|
using GoDotTest;
|
|
using System.Diagnostics;
|
|
using Xunit;
|
|
|
|
public class HexGridTests : TestClass
|
|
{
|
|
public HexGridTests(Node testScene) : base(testScene)
|
|
{
|
|
}
|
|
|
|
[Test]
|
|
public void TestGetAt()
|
|
{
|
|
HexGrid grid = new HexGrid();
|
|
float w = grid.HexSize.x;
|
|
float h = grid.HexSize.y;
|
|
|
|
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));
|
|
}
|
|
|
|
[Test]
|
|
public void TestGetCellsForLineSimple()
|
|
{
|
|
HexGrid grid = new HexGrid();
|
|
|
|
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]
|
|
public void TestGetCellsDiagonal()
|
|
{
|
|
HexGrid grid = new HexGrid();
|
|
|
|
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]
|
|
public void TestGetCellsForLineAlongEdge()
|
|
{
|
|
HexGrid grid = new HexGrid();
|
|
|
|
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]
|
|
public void GetTestsInfiniteLoop()
|
|
{
|
|
HexGrid grid = new HexGrid();
|
|
|
|
Vector2 fromPlane = new Vector2(-2.31678f, -5.024752f);
|
|
Vector2 toPlane = new Vector2(-2.599937f, -6.134028f);
|
|
|
|
List<HexCell> cellList = grid.GetCellsForLine(fromPlane, toPlane);
|
|
}
|
|
} |