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._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._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 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 HandleEnemyClick(Enemy ENEMY) { GD.Print(ENEMY._address, _playArea._map._astar.IsPointSolid(ENEMY._address)); // if (ENEMY._speedRemaining <= 0){ // return; // } TileMapLayer pathLayer = _playArea.GetNode("PathLayer"); Vector2I goal = GetBestGoal(ENEMY, new(ENEMY._address.X, Math.Max(ENEMY._address.Y - ENEMY._visibilityRange, _playArea._map._minY))); if (ENEMY._address == Vector2I.Zero) { GD.Print(0,goal); } List newPath = [.. _playArea._map.GetPath(ENEMY._address, goal)]; 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 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]; enemy._speedRemaining = enemy._address.Y <= map._minY ? 0 : enemy._speed; if (enemy._speedRemaining > 0) { Vector2I goal = GetBestGoal(enemy, new(enemy._address.X, Math.Max(enemy._address.Y - enemy._visibilityRange, map._minY))); if (enemy._address == Vector2I.Zero) { GD.Print(1,goal); } enemy._path = map.GetPath(enemy._address, goal); if (enemy._path.Count <= 0) { enemy._speedRemaining = 0; } } } 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 <= map._minY) { enemy._speedRemaining = 0; continue; } Vector2I goal = GetBestGoal(enemy, new(enemy._address.X, Math.Max(enemy._address.Y - enemy._visibilityRange, map._minY))); if (enemy._address == Vector2I.Zero) { GD.Print(2,goal); } List path = map.GetPath(enemy._address, goal); if (path.Count == 0) { continue; } Vector2I cell = path[0]; if (enemy._address == Vector2I.Zero) { GD.Print(3, enemy._address); } 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 SortEnemies(List ENEMIES) { return [.. ENEMIES.Where(e => e._speedRemaining > 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(16, 14)) { ENEMY._track = true; } _playArea._map.SetCellEnemy(CELL, ENEMY); ENEMY.GlobalPosition = _playArea._map.GetCellPositionFromAddress(CELL); } }