Setting better bounds when performing Path search.

WorldChunkRefactoring
Martin Felis 2023-10-27 21:49:37 +02:00
parent da09c66cb3
commit 01fde34276
2 changed files with 12 additions and 11 deletions

View File

@ -1,7 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Godot; using Godot;
using GodotComponentTest.utils;
using Priority_Queue; using Priority_Queue;
using AxialCoordDirectionPair = System.Tuple<Godot.Vector2, Godot.Vector3>; using AxialCoordDirectionPair = System.Tuple<Godot.Vector2, Godot.Vector3>;
@ -22,7 +21,7 @@ public class HexGrid : Resource
public float PathCostDefault = 1; public float PathCostDefault = 1;
public int FindPathCheckedCells = 0; public int FindPathCheckedCellCount;
public Vector2 HexSize public Vector2 HexSize
{ {
@ -76,11 +75,6 @@ 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));
@ -94,6 +88,13 @@ public class HexGrid : Resource
(_maxCoords.AxialCoords - _minCoords.AxialCoords) + Vector2.One); (_maxCoords.AxialCoords - _minCoords.AxialCoords) + Vector2.One);
} }
public void SetBounds(HexCell center, int size)
{
Vector2 centerOffset = center.OffsetCoords;
SetBounds(GetHexAtOffset(centerOffset - Vector2.One * size / 2),
GetHexAtOffset(centerOffset + Vector2.One * size / 2));
}
public void AddObstacle(Vector2 axialCoords, float cost = 0) public void AddObstacle(Vector2 axialCoords, float cost = 0)
{ {
AddObstacle(new HexCell(axialCoords), cost); AddObstacle(new HexCell(axialCoords), cost);
@ -221,11 +222,11 @@ public class HexGrid : Resource
cameFrom.Add(startHex.AxialCoords, startHex.AxialCoords); cameFrom.Add(startHex.AxialCoords, startHex.AxialCoords);
costSoFar.Add(startHex.AxialCoords, 0); costSoFar.Add(startHex.AxialCoords, 0);
FindPathCheckedCells = 0; FindPathCheckedCellCount = 0;
while (frontier.Any()) while (frontier.Any())
{ {
FindPathCheckedCells++; FindPathCheckedCellCount++;
HexCell currentHex = new HexCell(frontier.Dequeue()); HexCell currentHex = new HexCell(frontier.Dequeue());
Vector2 currentAxial = currentHex.AxialCoords; Vector2 currentAxial = currentHex.AxialCoords;
@ -264,7 +265,7 @@ public class HexGrid : Resource
} }
} }
// GD.Print("Checked Cell Count: " + FindPathCheckedCells); // GD.Print("Checked Cell Count: " + FindPathCheckedCellCount);
List<HexCell> result = new List<HexCell>(); List<HexCell> result = new List<HexCell>();
if (!cameFrom.ContainsKey(goalHex.AxialCoords)) if (!cameFrom.ContainsKey(goalHex.AxialCoords))
@ -293,7 +294,6 @@ public class HexGrid : Resource
{ {
List<HexCell> result = new List<HexCell>(); List<HexCell> result = new List<HexCell>();
HexCell toCell = GetHexAt(toPlane);
float distance = (toPlane - fromPlane).Length(); float distance = (toPlane - fromPlane).Length();
Vector2 direction = (toPlane - fromPlane) / distance; Vector2 direction = (toPlane - fromPlane) / distance;

View File

@ -140,6 +140,7 @@ public class NavigationComponent : Spatial
return; return;
} }
TileWorld.HexGrid.SetBounds(fromCell, 40);
List<HexCell> path = TileWorld.HexGrid.FindPath(fromCell, toCell); List<HexCell> path = TileWorld.HexGrid.FindPath(fromCell, toCell);
// Generate grid navigation points // Generate grid navigation points