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._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._speed = Globals._rng.Next(2,4+1); newEnemy._speed = 3; 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 List GetEnemyPath(Enemy ENEMY, Vector2I PATH) // { // } public Vector2I GetBestGoal(Enemy ENEMY, Vector2I GOAL) { Vector2I goal = GOAL; while (_playArea._map.IsRowFull(goal.Y)) { goal.Y++; } int loop = 0; while (_playArea._map.HasOccupant(goal)) { Vector2I leftGoal = new (Math.Clamp(ENEMY._address.X - loop / 2 - 1, _playArea._map._minX, _playArea._map._maxX), goal.Y); Vector2I rightGoal = new (Math.Clamp(ENEMY._address.X + loop / 2 + 1, _playArea._map._minX, _playArea._map._maxX), goal.Y); List leftPath = _playArea._map.GetPath(ENEMY._address, leftGoal); List rightPath = _playArea._map.GetPath(ENEMY._address, rightGoal); if (rightPath.Count < leftPath.Count && !_playArea._map.HasOccupant(rightGoal)) { goal = rightGoal; } else if (leftPath.Count < rightPath.Count && !_playArea._map.HasOccupant(leftGoal)) { goal = leftGoal; } else if (!_playArea._map.HasOccupant(rightGoal)) { goal = rightGoal; } else if (!_playArea._map.HasOccupant(leftGoal)) { goal = leftGoal; } loop++; } return goal; } public void Initiate() { List positions = [.. _playArea.GetNode("InitialPositions").GetUsedCells()]; AddEnemies(positions); } public void MoveEnemies() { for (int i = 0; i < _enemies.Count; i++) { Enemy enemy = _enemies[i]; enemy._speedRemaining = enemy._address.Y <= _playArea._map._minY ? 0 : enemy._speed; if (enemy._speedRemaining > 0) { Vector2I goal = GetBestGoal(enemy, new(enemy._address.X, Math.Max(enemy._address.Y - enemy._visibilityRange, _playArea._map._minY))); enemy._path = _playArea._map.GetPath(enemy._address, goal); } } List remainingEnemies = SortEnemies(_enemies); _enemies.ForEach(e => e._path.Clear()); if (remainingEnemies.Count == 0) { return; } while (remainingEnemies.Count > 0) { remainingEnemies = SortEnemies(remainingEnemies); for (int i = 0; i < remainingEnemies.Count; i++) { Enemy enemy = remainingEnemies[i]; if (enemy._speedRemaining <= 0) { continue; } if (enemy._address.Y <= _playArea._map._minY) { continue; } Vector2I goal = GetBestGoal(enemy, new(enemy._address.X, Math.Max(enemy._address.Y - enemy._visibilityRange, _playArea._map._minY))); List path = _playArea._map.GetPath(enemy._address, goal); Vector2I cell = path[0]; _playArea._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", _playArea._map.GetCellPositionFromAddress(mEnemy._path[i]), .25f); } } tween.TweenCallback(Callable.From(() => EmitSignal(SignalName.TurnDone))); _playArea._map.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 SortEnemies(List ENEMIES) { return [.. ENEMIES.Where(e => e._speedRemaining > 0 && _playArea._map.GetPath(e._address, new Vector2I(e._address.X, Math.Max(e._address.Y - e._visibilityRange, _playArea._map._minY))).Count > 0).OrderByDescending(e => e._path.Count).ThenBy(e => e._address.Y).ThenBy(e => Math.Abs(e._address.X - _playArea._map._maxX / 2))]; } public override void StartTurn() { AddEnemies(1); MoveEnemies(); } public void SetEnemy(Enemy ENEMY, Vector2I CELL) { if (CELL == new Vector2I(5, 7)) { ENEMY._track = true; } _playArea._map.SetCellEnemy(CELL, ENEMY); ENEMY.GlobalPosition = _playArea._map.GetCellPositionFromAddress(CELL); } }