HexGrid::SetBounds by default uses AxialCoords which fixes all tests.

WorldChunkRefactoring
Martin Felis 2023-08-12 14:28:01 +02:00
parent 10afa67d85
commit 3b73480845
2 changed files with 23 additions and 4 deletions

View File

@ -19,7 +19,7 @@ public class HexGrid : Resource
private Godot.Transform2D _hexTransformInv; private Godot.Transform2D _hexTransformInv;
private HexCell _minCoords = new HexCell(); private HexCell _minCoords = new HexCell();
private HexCell _maxCoords = new HexCell(); private HexCell _maxCoords = new HexCell();
private Rect2 _bounds = new Rect2(); private Rect2 _boundsAxialCoords = new Rect2();
public System.Collections.Generic.Dictionary<Vector2, float> Obstacles = new System.Collections.Generic.Dictionary<Vector2, float>(); public System.Collections.Generic.Dictionary<Vector2, float> Obstacles = new System.Collections.Generic.Dictionary<Vector2, float>();
@ -82,6 +82,11 @@ public class HexGrid : Resource
return result; return result;
} }
public void SetBoundsOffset(Vector2 minOffset, Vector2 maxOffset)
{
SetBounds (HexCell.FromOffsetCoords(minOffset), HexCell.FromOffsetCoords(maxOffset));
}
public void SetBounds(Vector2 minAxial, Vector2 maxAxial) public void SetBounds(Vector2 minAxial, Vector2 maxAxial)
{ {
SetBounds(new HexCell(minAxial), new HexCell(maxAxial)); SetBounds(new HexCell(minAxial), new HexCell(maxAxial));
@ -91,7 +96,7 @@ public class HexGrid : Resource
{ {
_minCoords = minCell; _minCoords = minCell;
_maxCoords = maxCell; _maxCoords = maxCell;
_bounds = new Rect2(_minCoords.OffsetCoords, (_maxCoords.OffsetCoords - _minCoords.OffsetCoords) + Vector2.One); _boundsAxialCoords = new Rect2(_minCoords.AxialCoords, (_maxCoords.AxialCoords - _minCoords.AxialCoords) + Vector2.One);
} }
public void AddObstacle(Vector2 axialCoords, float cost = 0) public void AddObstacle(Vector2 axialCoords, float cost = 0)
@ -144,8 +149,7 @@ public class HexGrid : Resource
public float GetHexCost(Vector2 axialCoords) public float GetHexCost(Vector2 axialCoords)
{ {
HexCell cell = new HexCell(axialCoords); if (!_boundsAxialCoords.HasPoint(axialCoords))
if (!_bounds.HasPoint(cell.OffsetCoords))
{ {
return 0; return 0;
} }

View File

@ -3,6 +3,7 @@ using Godot;
using GoDotTest; using GoDotTest;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using Xunit;
public class HexCellTests : TestClass public class HexCellTests : TestClass
{ {
@ -17,6 +18,20 @@ public class HexCellTests : TestClass
Debug.Assert(cell.CubeCoords == new Vector3(0, 0, 0)); Debug.Assert(cell.CubeCoords == new Vector3(0, 0, 0));
} }
[Test]
public void TestHexCellEqualityInequality()
{
HexCell cellA = new HexCell();
HexCell cellB = new HexCell();
cellA.AxialCoords = new Vector2(2, 3);
cellB.AxialCoords = new Vector2(2, 3);
Assert.Equal(cellA, cellB);
cellB.AxialCoords = new Vector2(3, 2);
Assert.NotEqual(cellA, cellB);
}
[Test] [Test]
public void TestAxialCoords() public void TestAxialCoords()
{ {