using Godot; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; public partial class EnemyController : TurnController { public PackedScene _enemyScene = GD.Load("res://Enemy.tscn"); public List _enemies = new(); public PlayerController _playerController; public void AddEnemies(int ENEMY_COUNT = 1) { for (int i = 0; i < ENEMY_COUNT; i++) { Enemy newEnemy = _enemyScene.Instantiate(); newEnemy.Death += RemoveEnemy; newEnemy.Clicked += HandleEnemyClick; newEnemy.RightClicked += HandleEnemyRightClick; newEnemy._speed = Globals._rng.Next(2,4+1); newEnemy.Modulate = new Color(newEnemy._speed == 2 ? "#FF0000" : newEnemy._speed == 3 ? "#00FF00" : "#0000FF"); newEnemy._enemyController = this; List unoccupied = [.. _playArea._map._bottomRow.Where(c => _enemies.All(e => e._address != c))]; Vector2I randomCell = unoccupied[Globals._rng.Next(unoccupied.Count)]; SetEnemy(newEnemy, randomCell); _enemies.Add(newEnemy); AddChild(newEnemy); } } public void AddEnemies(List POSITIONS) { for (int i = 0; i < POSITIONS.Count; i++) { Enemy newEnemy = _enemyScene.Instantiate(); newEnemy.Death += RemoveEnemy; newEnemy.Clicked += HandleEnemyClick; newEnemy.RightClicked += HandleEnemyRightClick; newEnemy._speed = Globals._rng.Next(2,4+1); newEnemy.Modulate = new Color(newEnemy._speed == 2 ? "#FF0000" : newEnemy._speed == 3 ? "#00FF00" : "#0000FF"); newEnemy._enemyController = this; SetEnemy(newEnemy, POSITIONS[i]); _enemies.Add(newEnemy); AddChild(newEnemy); } } public void EnemyAttacks() { List attackingEnemies = [.. _enemies.Where(e => e._address.Y <= e._hitRange)]; for (int i = 0; i < attackingEnemies.Count; i++) { Enemy enemy = attackingEnemies[i]; // enemy.Attack(); } } 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; } public void HandleEnemyClick(Enemy ENEMY) { if (ENEMY._speedRemaining <= 0){ return; } TileMapLayer pathLayer = _playArea.GetNode("PathLayer"); List newPath = GetBestPath(ENEMY); pathLayer.GetUsedCells().ToList().ForEach(c => pathLayer.SetCell(c,0,Vector2I.Down*4)); for (int i = 0; i < newPath.Count; i++) { pathLayer.SetCell(newPath[i],0,Vector2I.One); } } public void HandleEnemyRightClick(Enemy ENEMY) { RemoveEnemy(ENEMY); } public void Initiate() { List positions = [.. _playArea.GetNode("InitialPositions").GetUsedCells()]; AddEnemies(positions); } public void MoveEnemies() { Map map = _playArea._map; for (int i = 0; i < _enemies.Count; i++) { Enemy enemy = _enemies[i]; GetRemainingSpeed(enemy); if (enemy._speedRemaining > 0) { enemy._path = GetBestPath(enemy); if (enemy._path.Count <= 0) { enemy._speedRemaining = 0; } } } List remainingEnemies = GetRemainingEnemies(_enemies); _enemies.ForEach(e => e._path.Clear()); if (remainingEnemies.Count == 0) { return; } while (remainingEnemies.Count > 0) { remainingEnemies = GetRemainingEnemies(remainingEnemies); for (int i = 0; i < remainingEnemies.Count; i++) { Enemy enemy = remainingEnemies[i]; if (enemy._speedRemaining <= 0) { continue; } if (enemy._address.Y <= map.GetFirstOpenRow()) { enemy._speedRemaining = 0; continue; } List path = GetBestPath(enemy); if (path.Count == 0) { continue; } Vector2I cell = path[0]; if (cell.Y <= enemy._address.Y) { map.SetCellEnemy(cell, enemy); enemy._path.Add(cell); } enemy._speedRemaining--; } } Tween tween = CreateTween(); tween.SetParallel(); List movingEnemies = [.. _enemies.Where(e => e._path.Count > 0)]; int maxSteps = movingEnemies.Max(e => e._path.Count); for (int i = 0; i < maxSteps; i++) { for (int j = 0; j < movingEnemies.Count; j++) { Enemy mEnemy = movingEnemies[j]; if (mEnemy._path.Count <= i) { continue; } if (j == 0) { tween.Chain(); } else { tween.Parallel(); } tween.TweenProperty(mEnemy, "global_position", map.GetCellPositionFromAddress(mEnemy._path[i]), .25f); } } tween.TweenCallback(Callable.From(() => EmitSignal(SignalName.TurnDone))); _playArea.HighlightCells(); } public void RemoveEnemy(Enemy ENEMY_TO_REMOVE) { _enemies.Remove(ENEMY_TO_REMOVE); _playArea._map.SetCellEnemy(ENEMY_TO_REMOVE._address, null); ENEMY_TO_REMOVE.QueueFree(); } 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))]; } public override void StartTurn() { AddEnemies(1); MoveEnemies(); EnemyAttacks(); } public void SetEnemy(Enemy ENEMY, Vector2I CELL) { if (CELL == new Vector2I(16, 14)) { ENEMY._track = true; } _playArea._map.SetCellEnemy(CELL, ENEMY); ENEMY.GlobalPosition = _playArea._map.GetCellPositionFromAddress(CELL); } }