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
+42 -34
View File
@@ -22,7 +22,7 @@ public partial class EnemyController : TurnController
newEnemy._enemyController = this;
List<Vector2I> unoccupied = [.. _playArea._map._bottomRow.Where(c => _enemies.All(e => e._address != c))];
Vector2I randomCell = unoccupied[Globals._rng.Next(unoccupied.Count)];
WarpEnemy(newEnemy, randomCell);
SetEnemy(newEnemy, randomCell);
_enemies.Add(newEnemy);
AddChild(newEnemy);
}
@@ -39,7 +39,7 @@ public partial class EnemyController : TurnController
newEnemy.Modulate = new Color(newEnemy._speed == 2 ? "#FF0000" : newEnemy._speed == 3 ? "#00FF00" : "#0000FF");
newEnemy._enemyController = this;
WarpEnemy(newEnemy, POSITIONS[i]);
SetEnemy(newEnemy, POSITIONS[i]);
_enemies.Add(newEnemy);
AddChild(newEnemy);
}
@@ -51,6 +51,15 @@ public partial class EnemyController : TurnController
List<Vector2I> positions = [.. _playArea.GetNode<TileMapLayer>("InitialPositions").GetUsedCells()];
AddEnemies(positions);
// for (int i = 0; i < _playArea._map._cells.Count; i++)
// {
// if (_playArea._map._astar.IsPointSolid(_playArea._map._cells[i]))
// {
// GD.Print(_playArea._map._cells[i]);
// }
// }
}
public void MoveEnemies()
@@ -61,27 +70,20 @@ public partial class EnemyController : TurnController
for (int i = 0; i < _enemies.Count; i++)
{
Enemy enemy = _enemies[i];
Vector2I goal = new(enemy._address.X, _playArea._map._minY);
if (Math.Abs(goal.Y - enemy._address.Y) <= enemy._visibilityRange)
Vector2I goal = new(enemy._address.X, Math.Max(enemy._address.Y - enemy._visibilityRange, _playArea._map._minY));
while(_playArea._map.IsRowFull(goal.Y))
{
// GD.Print(goal.Y, enemy._address.Y,enemy._visibilityRange);
while(_playArea._map.IsRowFull(goal.Y))
{
goal.Y++;
}
// if (_playArea._map.GetOccupant(goal) != null)
// {
// goal.X = Math.Clamp(goal.X+1, _playArea._map._minX+1, _playArea._map._maxX-1);
// }
int loop = 0;
while (_playArea._map.GetOccupant(goal) != null)
{
goal.X = Math.Clamp(enemy._address.X + ((int)MathF.Floor(loop / 2) + 1) * (loop % 2 == 0 ? 1 : -1), _playArea._map._minX, _playArea._map._maxX);
loop++;
}
goal.Y++;
}
List<Vector2I> path = _playArea._map.GetPath(enemy._address, goal);
enemy._path = path;
int loop = 0;
while (_playArea._map.GetOccupant(goal) != null)
{
goal.X = Math.Clamp(enemy._address.X + ((int)MathF.Floor(loop / 2) + 1) * (loop % 2 == 0 ? 1 : -1), _playArea._map._minX, _playArea._map._maxX);
loop++;
}
enemy._path = _playArea._map.GetPath(enemy._address, goal, false, i==0);
enemy._speedRemaining = enemy._address.Y <= _playArea._map._minY ? 0 : enemy._speed;
}
_enemies = [.. _enemies.OrderByDescending(e => e._path.Count).ThenBy(e => e._address.Y).ThenBy(e => Math.Abs(e._address.X))];
@@ -99,17 +101,29 @@ public partial class EnemyController : TurnController
remainingEnemies.Remove(enemy);
continue;
}
List<Vector2I> path = enemy._path;
int pathIndex = enemy._speed - enemy._speedRemaining;
Vector2I cell = path[pathIndex];
Vector2I cell = enemy._path[pathIndex];
Enemy obstacle = _playArea._map.GetOccupant(cell);
if (obstacle != null)
{
int obstacleRemainingMoves = obstacle._speedRemaining;
if (obstacleRemainingMoves <= 0)
{
_playArea._map.EvaluateSolidCells();
List<Vector2I> newPath = _playArea._map.GetPath(enemy._address, new Vector2I(enemy._address.X, _playArea._map._minY));
Vector2I newGoal = new(enemy._address.X, Math.Max(enemy._address.Y - enemy._visibilityRange, _playArea._map._minY));
while(_playArea._map.IsRowFull(newGoal.Y))
{
newGoal.Y++;
}
int loop = 0;
while (_playArea._map.GetOccupant(newGoal) != null)
{
newGoal.X = Math.Clamp(enemy._address.X + ((int)MathF.Floor(loop / 2) + 1) * (loop % 2 == 0 ? 1 : -1), _playArea._map._minX, _playArea._map._maxX);
loop++;
}
List<Vector2I> newPath = _playArea._map.GetPath(enemy._address, newGoal);
enemy._path = newPath;
newPath.InsertRange(0, [.. Enumerable.Repeat(newPath[0], pathIndex)]);
@@ -121,9 +135,7 @@ public partial class EnemyController : TurnController
if (obstacle == null)
{
_playArea._map.SetCellEnemy(enemy._address, null);
enemy._address = cell;
_playArea._map.SetCellEnemy(enemy._address, enemy);
_playArea._map.SetCellEnemy(cell, enemy);
if (j == 0)
{
tween.Chain();
@@ -162,13 +174,9 @@ public partial class EnemyController : TurnController
}
public void WarpEnemy(Enemy ENEMY, Vector2I CELL)
public void SetEnemy(Enemy ENEMY, Vector2I CELL)
{
_playArea._map.SetCellEnemy(ENEMY._address, null);
ENEMY._address = CELL;
_playArea._map.SetCellEnemy(ENEMY._address, ENEMY);
_playArea._map.SetCellEnemy(CELL, ENEMY);
ENEMY.GlobalPosition = _playArea._map.GetCellPositionFromAddress(CELL);
}
}