From 9f75ec525c2116a3d6062106909bfa697bb17f06 Mon Sep 17 00:00:00 2001 From: Conor Edmonds Date: Fri, 12 Jun 2026 01:02:00 -0400 Subject: [PATCH] Gave Get best path to enemy, enemy controller hands it a map --- Enemy.cs | 33 +++++++++++++++++++++++++++++++++ EnemyController.cs | 41 ++++------------------------------------- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/Enemy.cs b/Enemy.cs index 6d2edfa..142860e 100644 --- a/Enemy.cs +++ b/Enemy.cs @@ -53,6 +53,39 @@ public partial class Enemy : StaticBody2D } + public virtual List GetBestPath(Map MAP) + { + Vector2I goal = Vector2I.One * -1000; + + goal.Y = MAP.GetFirstOpenRow(); + List openCells = [.. MAP._cells.Where(c => c.Y == goal.Y && !MAP._astar.IsPointSolid(c))]; + List 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) { _health -= DAMAGE; diff --git a/EnemyController.cs b/EnemyController.cs index 37d5f7b..9dcda18 100644 --- a/EnemyController.cs +++ b/EnemyController.cs @@ -60,39 +60,6 @@ public partial class EnemyController : TurnController } } - public List GetBestPath(Enemy ENEMY) - { - Vector2I goal = Vector2I.One * -1000; - - goal.Y = _playArea._map.GetFirstOpenRow(); - List openCells = [.. _playArea._map._cells.Where(c => c.Y == goal.Y && !_playArea._map._astar.IsPointSolid(c))]; - List 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) { 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; } TileMapLayer pathLayer = _playArea.GetNode("PathLayer"); - List newPath = GetBestPath(ENEMY); + List newPath = ENEMY.GetBestPath(_playArea._map); 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) { - enemy._path = GetBestPath(enemy); + enemy._path = enemy.GetBestPath(map); if (enemy._path.Count <= 0) { @@ -166,7 +133,7 @@ public partial class EnemyController : TurnController enemy._speedRemaining = 0; continue; } - List path = GetBestPath(enemy); + List path = enemy.GetBestPath(map); if (path.Count == 0) { @@ -222,7 +189,7 @@ public partial class EnemyController : TurnController public List GetRemainingEnemies(List 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()