Navigation: filtering out invalid start and end points.
parent
b17531e504
commit
87f7af88f8
20
HexGrid.cs
20
HexGrid.cs
|
@ -199,6 +199,26 @@ public class HexGrid : Resource
|
|||
return cost;
|
||||
}
|
||||
|
||||
|
||||
public HexCell GetClosestWalkableCell(HexCell fromCell, HexCell toCell)
|
||||
{
|
||||
if (GetHexCost(toCell) == 0)
|
||||
{
|
||||
HexCell[] line = fromCell.LineTo(toCell);
|
||||
|
||||
foreach (int i in Enumerable.Range(1, line.Length))
|
||||
{
|
||||
if (GetHexCost(line[i]) == 0)
|
||||
{
|
||||
toCell = line[i - 1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return toCell;
|
||||
}
|
||||
|
||||
public List<HexCell> FindPath(HexCell startHex, HexCell goalHex)
|
||||
{
|
||||
Vector2 startAxialCoords = startHex.AxialCoords;
|
||||
|
|
|
@ -104,7 +104,27 @@ public class NavigationComponent : Spatial
|
|||
public void FindPath(KinematicBody body, Vector3 fromPositionWorld, Vector3 toPositionWorld)
|
||||
{
|
||||
HexCell fromCell = TileWorld.HexGrid.GetHexAt(new Vector2(fromPositionWorld.x, fromPositionWorld.z));
|
||||
if (TileWorld.HexGrid.GetHexCost(fromCell) == 0)
|
||||
{
|
||||
GD.Print("Invalid starting point for FindPath(): returning empty path.");
|
||||
_planningPathWorldNavigationPoints = new List<NavigationPoint>();
|
||||
_planningPathWorldNavigationPoints.Add(new NavigationPoint(fromPositionWorld));
|
||||
_planningPathSmoothedWorldNavigationPoints = _planningPathWorldNavigationPoints;
|
||||
return;
|
||||
}
|
||||
|
||||
HexCell toCell = TileWorld.HexGrid.GetHexAt(new Vector2(toPositionWorld.x, toPositionWorld.z));
|
||||
toCell = TileWorld.HexGrid.GetClosestWalkableCell(fromCell, toCell);
|
||||
|
||||
if (TileWorld.HexGrid.GetHexCost(toCell) == 0)
|
||||
{
|
||||
GD.Print("Invalid target point for FindPath(): returning empty path.");
|
||||
_planningPathWorldNavigationPoints = new List<NavigationPoint>();
|
||||
_planningPathWorldNavigationPoints.Add(new NavigationPoint(fromPositionWorld));
|
||||
_planningPathSmoothedWorldNavigationPoints = _planningPathWorldNavigationPoints;
|
||||
return;
|
||||
}
|
||||
|
||||
List<HexCell> path = TileWorld.HexGrid.FindPath(fromCell, toCell);
|
||||
|
||||
// GD.Print("Planning path and have " + TileWorld.HexGrid.Obstacles.Count + " obstacles:");
|
||||
|
|
Loading…
Reference in New Issue