still working on some pathfinding, it is seizing up when trying to find a new goal

This commit is contained in:
2026-06-08 03:38:33 -04:00
parent 3c9a51bd6a
commit 648a00ccf1
3 changed files with 99 additions and 70 deletions
+54 -32
View File
@@ -9,7 +9,7 @@ public partial class Map : TileMapLayer
public string _isSolidString = "is_solid";
public Vector2 _cellSize, _sizeInPixels, _sizeInCells;
public Vector2I _pathTakenAtlasCoordinates = new Vector2I(4, 0);
public List<Vector2I> _leftmostColumn, _rightmostColumn, _topRow, _bottomRow;
public List<Vector2I> _cells = new(), _leftmostColumn = new(), _rightmostColumn = new(), _topRow = new(), _bottomRow = new();
public AStarGrid2D _astar = new();
public Dictionary<Vector2I, Enemy> _addressOccupants = new();
@@ -17,17 +17,16 @@ public partial class Map : TileMapLayer
{
base._Ready();
_cellSize = TileSet.TileSize;
List<Vector2I> cells = [.. GetUsedCells()];
_minX = cells.Min(c => c.X);
_maxX = cells.Max(c => c.X);
_minY = cells.Min(c => c.Y);
_maxY = cells.Max(c => c.Y);
// GD.Print(_minX, _maxX, _minY, _maxY);
_leftmostColumn = [.. cells.Where(c => c.X == _minX)];
_rightmostColumn = [.. cells.Where(c => c.X == _maxX)];
_topRow = [.. cells.Where(c => c.Y == _minY)];
_bottomRow = [.. cells.Where(c => c.Y == _maxY)];
cells.ForEach(c => SetCellEnemy(c, null));
_cells = [.. GetUsedCells()];
_cells.ForEach(c => _addressOccupants[c] = null);
_minX = _cells.Min(c => c.X);
_maxX = _cells.Max(c => c.X);
_minY = _cells.Min(c => c.Y);
_maxY = _cells.Max(c => c.Y);
_leftmostColumn = [.. _cells.Where(c => c.X == _minX)];
_rightmostColumn = [.. _cells.Where(c => c.X == _maxX)];
_topRow = [.. _cells.Where(c => c.Y == _minY)];
_bottomRow = [.. _cells.Where(c => c.Y == _maxY)];
_sizeInCells = new Vector2(_topRow.Count, _leftmostColumn.Count);
_sizeInPixels = _sizeInCells * _cellSize;
@@ -48,49 +47,69 @@ public partial class Map : TileMapLayer
public bool IsCellSolid(Vector2I CELL_TO_CHECK)
{
bool hasOccupant = GetOccupant(CELL_TO_CHECK) != null;
if (hasOccupant)
{
// GD.Print(CELL_TO_CHECK, " has occupant");
}
bool isSolid = (bool)GetCellTileData(CELL_TO_CHECK).GetCustomData(_isSolidString);
if (isSolid)
{
// GD.Print(CELL_TO_CHECK, " is solid");
}
return hasOccupant || isSolid;
}
public bool IsRowFull(int ROW_TO_CHECK)
{
List<Vector2I> cells = [.. GetUsedCells()];
List<Vector2I> rowCells = [.. cells.Where(c => c.Y == ROW_TO_CHECK)];
List<Vector2I> rowCells = [.. _cells.Where(c => c.Y == ROW_TO_CHECK)];
return rowCells.All(c => GetOccupant(c) != null);
}
public void SetCellEnemy(Vector2I ADDRESS, Enemy ENEMY)
{
if (ENEMY != null)
{
if (ENEMY._address != -(Vector2I)Vector2.Inf)
{
_addressOccupants[ENEMY._address] = null;
}
ENEMY._address = ADDRESS;
}
_addressOccupants[ADDRESS] = ENEMY;
IsCellSolid(ADDRESS);
SetCellSolid(ADDRESS);
}
public void SetCellSolid(Vector2I ADDRESS)
{
_astar.SetPointSolid(ADDRESS, IsCellSolid(ADDRESS));
}
public void SetupAstar()
{
_astar.Region = new Rect2I(_minX, _minY, _topRow.Count, _leftmostColumn.Count);
_astar.CellSize = _cellSize;
_astar.DefaultComputeHeuristic = AStarGrid2D.Heuristic.Manhattan;
_astar.DefaultComputeHeuristic = AStarGrid2D.Heuristic.Euclidean;
_astar.DiagonalMode = AStarGrid2D.DiagonalModeEnum.Never;
_astar.Update();
List<Vector2I> cells = [.. GetUsedCells()];
for (int i = 0; i < cells.Count; i++)
{
_astar.SetPointSolid(cells[i], IsCellSolid(cells[i]));
}
EvaluateSolidCells();
}
public List<Vector2I> GetPath(Vector2I FROM, Vector2I TO, bool INCLUDE_FROM = false, bool SHOW_PATH = false)
{
List<Vector2I> pathTaken = [.. _astar.GetIdPath(FROM, TO)];
if (SHOW_PATH)
{
for (int i = 0; i < pathTaken.Count; i++)
{
Vector2I cell = pathTaken[i];
SetCell(cell, _mainSource, _pathTakenAtlasCoordinates);
}
}
_astar.SetPointSolid(FROM, false);
List<Vector2I> pathTaken = [.. _astar.GetIdPath(FROM, TO, true)];
// if (SHOW_PATH)
// {
// for (int i = 0; i < pathTaken.Count; i++)
// {
// Vector2I cell = pathTaken[i];
// SetCell(cell, _mainSource, _pathTakenAtlasCoordinates);
// }
// }
if (!INCLUDE_FROM)
{
pathTaken.Remove(FROM);
@@ -99,8 +118,11 @@ public partial class Map : TileMapLayer
}
public void EvaluateSolidCells()
{
GetUsedCells().ToList().ForEach(c => IsCellSolid(c));
{
for (int i = 0; i < _cells.Count; i++)
{
SetCellSolid(_cells[i]);
}
}
}