From 1ba28ab06c8a38710d6b8d45dedeececf48c1d6c Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Fri, 10 Nov 2023 10:44:18 +0100 Subject: [PATCH] HexGrid: allow setting bounds via offset coordinate rectangle. --- HexGrid.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/HexGrid.cs b/HexGrid.cs index 0c3f4dc..8547c15 100644 --- a/HexGrid.cs +++ b/HexGrid.cs @@ -7,7 +7,8 @@ using AxialCoordDirectionPair = System.Tuple; public class HexGrid : Resource { private readonly Vector2 _baseHexSize = new(1, Mathf.Sqrt(3) / 2); - private Rect2 _boundsAxialCoords; + private Rect2 _boundsAxialCoords = new(-Vector2.Inf, Vector2.Inf); + private Rect2 _boundsOffsetCoords = new(-Vector2.Inf, Vector2.Inf); private Vector2 _hexScale = new(1, 1); private Vector2 _hexSize = new(1, Mathf.Sqrt(3) / 2); private Transform2D _hexTransform; @@ -100,6 +101,12 @@ public class HexGrid : Resource GetHexAtOffset(centerOffset + Vector2.One * size / 2)); } + public void SetBoundsOffset(HexCell cellSouthEast, Vector2 size) + { + _boundsOffsetCoords = new Rect2(cellSouthEast.OffsetCoords, size); + _boundsAxialCoords = new Rect2(-Vector2.Inf, Vector2.Inf); + } + public void AddObstacle(Vector2 axialCoords, float cost = 0) { AddObstacle(new HexCell(axialCoords), cost); @@ -140,6 +147,8 @@ public class HexGrid : Resource { if (!_boundsAxialCoords.HasPoint(axialCoords)) return 0; + if (!_boundsOffsetCoords.HasPoint(new HexCell(axialCoords).OffsetCoords)) return 0; + float value; return Obstacles.TryGetValue(axialCoords, out value) ? value : PathCostDefault; }