reworking pathfinding to update with each step, removing the need to worry about obstacles, but still needs work
This commit is contained in:
+43
-65
@@ -70,20 +70,7 @@ public partial class EnemyController : TurnController
|
||||
for (int i = 0; i < _enemies.Count; i++)
|
||||
{
|
||||
Enemy enemy = _enemies[i];
|
||||
Vector2I goal = new(enemy._address.X, Math.Max(enemy._address.Y - enemy._visibilityRange, _playArea._map._minY));
|
||||
|
||||
while(_playArea._map.IsRowFull(goal.Y))
|
||||
{
|
||||
goal.Y++;
|
||||
}
|
||||
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);
|
||||
ShiftGoal(enemy, new(enemy._address.X, Math.Max(enemy._address.Y - enemy._visibilityRange, _playArea._map._minY)));
|
||||
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))];
|
||||
@@ -96,63 +83,36 @@ public partial class EnemyController : TurnController
|
||||
for (int j = 0; j < remainingEnemies.Count; j++)
|
||||
{
|
||||
Enemy enemy = remainingEnemies[j];
|
||||
|
||||
int pathIndex = enemy._speed - enemy._speedRemaining;
|
||||
|
||||
enemy._path.InsertRange(0, [.. Enumerable.Repeat(enemy._path[0], pathIndex)]);
|
||||
Vector2I cell = enemy._path[pathIndex];
|
||||
Enemy obstacle = _playArea._map.GetOccupant(cell);
|
||||
_playArea._map.SetCellEnemy(cell, enemy);
|
||||
if (j == 0)
|
||||
{
|
||||
tween.Chain();
|
||||
}
|
||||
else
|
||||
{
|
||||
tween.Parallel();
|
||||
}
|
||||
tween.TweenProperty(enemy, "global_position", _playArea._map.GetCellPositionFromAddress(enemy._address), .2f);
|
||||
|
||||
if (enemy._address.Y <= _playArea._map._minY)
|
||||
{
|
||||
remainingEnemies.Remove(enemy);
|
||||
continue;
|
||||
}
|
||||
enemy._speedRemaining--;
|
||||
if (enemy._speedRemaining <= 0)
|
||||
{
|
||||
remainingEnemies.Remove(enemy);
|
||||
continue;
|
||||
}
|
||||
|
||||
int pathIndex = enemy._speed - enemy._speedRemaining;
|
||||
Vector2I cell = enemy._path[pathIndex];
|
||||
Enemy obstacle = _playArea._map.GetOccupant(cell);
|
||||
if (obstacle != null)
|
||||
{
|
||||
int obstacleRemainingMoves = obstacle._speedRemaining;
|
||||
if (obstacleRemainingMoves <= 0)
|
||||
{
|
||||
Vector2I newGoal = new(enemy._address.X, Math.Max(enemy._address.Y - enemy._visibilityRange, _playArea._map._minY));
|
||||
ShiftGoal(enemy, 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)]);
|
||||
|
||||
cell = enemy._path[pathIndex];
|
||||
obstacle = _playArea._map.GetOccupant(cell);
|
||||
}
|
||||
}
|
||||
|
||||
if (obstacle == null)
|
||||
{
|
||||
_playArea._map.SetCellEnemy(cell, enemy);
|
||||
if (j == 0)
|
||||
{
|
||||
tween.Chain();
|
||||
}
|
||||
else
|
||||
{
|
||||
tween.Parallel();
|
||||
}
|
||||
tween.TweenProperty(enemy, "global_position", _playArea._map.GetCellPositionFromAddress(enemy._address), .2f);
|
||||
|
||||
if (enemy._address.Y <= _playArea._map._minY)
|
||||
{
|
||||
remainingEnemies.Remove(enemy);
|
||||
continue;
|
||||
}
|
||||
enemy._speedRemaining--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,4 +139,22 @@ public partial class EnemyController : TurnController
|
||||
_playArea._map.SetCellEnemy(CELL, ENEMY);
|
||||
ENEMY.GlobalPosition = _playArea._map.GetCellPositionFromAddress(CELL);
|
||||
}
|
||||
|
||||
public void ShiftGoal(Enemy ENEMY, Vector2I GOAL)
|
||||
{
|
||||
while(_playArea._map.IsRowFull(GOAL.Y))
|
||||
{
|
||||
GOAL.Y++;
|
||||
}
|
||||
int loop = 0;
|
||||
while (_playArea._map.GetOccupant(GOAL) != null)
|
||||
{
|
||||
GD.Print(ENEMY._address, "-->", GOAL);
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user