Gave Get best path to enemy, enemy controller hands it a map

This commit is contained in:
2026-06-12 01:02:00 -04:00
parent a96d935ad4
commit 9f75ec525c
2 changed files with 37 additions and 37 deletions
+33
View File
@@ -53,6 +53,39 @@ public partial class Enemy : StaticBody2D
} }
public virtual List<Vector2I> GetBestPath(Map MAP)
{
Vector2I goal = Vector2I.One * -1000;
goal.Y = MAP.GetFirstOpenRow();
List<Vector2I> openCells = [.. MAP._cells.Where(c => c.Y == goal.Y && !MAP._astar.IsPointSolid(c))];
List<Vector2I> testPath, bestPath = [];
int bestLength = 999999;
for (int i = 0; i < openCells.Count; i++)
{
testPath = MAP.GetPath(_address, openCells[i]);
int testLength = testPath.Count;
if (testLength < bestLength)
{
bestPath = testPath;
bestLength = testLength;
goal = openCells[i];
}
else if (testLength == bestLength)
{
if (Math.Abs(openCells[i].X - _address.X) < Math.Abs(goal.X - _address.X))
{
bestPath = testPath;
bestLength = testLength;
goal = openCells[i];
}
}
}
return bestPath;
}
public void TakeDamage(int DAMAGE, Commander ATTACKER) public void TakeDamage(int DAMAGE, Commander ATTACKER)
{ {
_health -= DAMAGE; _health -= DAMAGE;
+4 -37
View File
@@ -60,39 +60,6 @@ public partial class EnemyController : TurnController
} }
} }
public List<Vector2I> GetBestPath(Enemy ENEMY)
{
Vector2I goal = Vector2I.One * -1000;
goal.Y = _playArea._map.GetFirstOpenRow();
List<Vector2I> openCells = [.. _playArea._map._cells.Where(c => c.Y == goal.Y && !_playArea._map._astar.IsPointSolid(c))];
List<Vector2I> testPath, bestPath = new();
int bestLength = 999999;
for (int i = 0; i < openCells.Count; i++)
{
testPath = _playArea._map.GetPath(ENEMY._address, openCells[i]);
int testLength = testPath.Count;
if (testLength < bestLength)
{
bestPath = testPath;
bestLength = testLength;
goal = openCells[i];
}
else if (testLength == bestLength)
{
if (Math.Abs(openCells[i].X - ENEMY._address.X) < Math.Abs(goal.X - ENEMY._address.X))
{
bestPath = testPath;
bestLength = testLength;
goal = openCells[i];
}
}
}
return bestPath;
}
public void GetRemainingSpeed(Enemy ENEMY) public void GetRemainingSpeed(Enemy ENEMY)
{ {
ENEMY._speedRemaining = ENEMY._address.Y <= _playArea._map.GetFirstOpenRow() || ENEMY._address.Y <= ENEMY._hitRange ? 0 : ENEMY._speed; ENEMY._speedRemaining = ENEMY._address.Y <= _playArea._map.GetFirstOpenRow() || ENEMY._address.Y <= ENEMY._hitRange ? 0 : ENEMY._speed;
@@ -104,7 +71,7 @@ public partial class EnemyController : TurnController
return; return;
} }
TileMapLayer pathLayer = _playArea.GetNode<TileMapLayer>("PathLayer"); TileMapLayer pathLayer = _playArea.GetNode<TileMapLayer>("PathLayer");
List<Vector2I> newPath = GetBestPath(ENEMY); List<Vector2I> newPath = ENEMY.GetBestPath(_playArea._map);
pathLayer.GetUsedCells().ToList().ForEach(c => pathLayer.SetCell(c,0,Vector2I.Down*4)); pathLayer.GetUsedCells().ToList().ForEach(c => pathLayer.SetCell(c,0,Vector2I.Down*4));
@@ -136,7 +103,7 @@ public partial class EnemyController : TurnController
if (enemy._speedRemaining > 0) if (enemy._speedRemaining > 0)
{ {
enemy._path = GetBestPath(enemy); enemy._path = enemy.GetBestPath(map);
if (enemy._path.Count <= 0) if (enemy._path.Count <= 0)
{ {
@@ -166,7 +133,7 @@ public partial class EnemyController : TurnController
enemy._speedRemaining = 0; enemy._speedRemaining = 0;
continue; continue;
} }
List<Vector2I> path = GetBestPath(enemy); List<Vector2I> path = enemy.GetBestPath(map);
if (path.Count == 0) if (path.Count == 0)
{ {
@@ -222,7 +189,7 @@ public partial class EnemyController : TurnController
public List<Enemy> GetRemainingEnemies(List<Enemy> ENEMIES) public List<Enemy> GetRemainingEnemies(List<Enemy> ENEMIES)
{ {
return [.. ENEMIES.Where(e => e._speedRemaining > 0).OrderByDescending(e => GetBestPath(e).Count).ThenBy(e => e._address.Y).ThenBy(e => Math.Abs(e._address.X - _playArea._map._maxX / 2))]; return [.. ENEMIES.Where(e => e._speedRemaining > 0).OrderByDescending(e => e.GetBestPath(_playArea._map).Count).ThenBy(e => e._address.Y).ThenBy(e => Math.Abs(e._address.X - _playArea._map._maxX / 2))];
} }
public override void StartTurn() public override void StartTurn()