From 3b7348084530a776210f3c4eaf92e368ab9da319 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Sat, 12 Aug 2023 14:28:01 +0200 Subject: [PATCH] HexGrid::SetBounds by default uses AxialCoords which fixes all tests. --- HexGrid.cs | 12 ++++++++---- tests/HexCellTests.cs | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/HexGrid.cs b/HexGrid.cs index c63e668..816765b 100644 --- a/HexGrid.cs +++ b/HexGrid.cs @@ -19,7 +19,7 @@ public class HexGrid : Resource private Godot.Transform2D _hexTransformInv; private HexCell _minCoords = new HexCell(); private HexCell _maxCoords = new HexCell(); - private Rect2 _bounds = new Rect2(); + private Rect2 _boundsAxialCoords = new Rect2(); public System.Collections.Generic.Dictionary Obstacles = new System.Collections.Generic.Dictionary(); @@ -82,6 +82,11 @@ public class HexGrid : Resource return result; } + public void SetBoundsOffset(Vector2 minOffset, Vector2 maxOffset) + { + SetBounds (HexCell.FromOffsetCoords(minOffset), HexCell.FromOffsetCoords(maxOffset)); + } + public void SetBounds(Vector2 minAxial, Vector2 maxAxial) { SetBounds(new HexCell(minAxial), new HexCell(maxAxial)); @@ -91,7 +96,7 @@ public class HexGrid : Resource { _minCoords = minCell; _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) @@ -144,8 +149,7 @@ public class HexGrid : Resource public float GetHexCost(Vector2 axialCoords) { - HexCell cell = new HexCell(axialCoords); - if (!_bounds.HasPoint(cell.OffsetCoords)) + if (!_boundsAxialCoords.HasPoint(axialCoords)) { return 0; } diff --git a/tests/HexCellTests.cs b/tests/HexCellTests.cs index edf1dfa..65a5bd5 100644 --- a/tests/HexCellTests.cs +++ b/tests/HexCellTests.cs @@ -3,6 +3,7 @@ using Godot; using GoDotTest; using System.Diagnostics; using System.Linq; +using Xunit; public class HexCellTests : TestClass { @@ -17,6 +18,20 @@ public class HexCellTests : TestClass 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] public void TestAxialCoords() {