214 lines
6.1 KiB
C#
214 lines
6.1 KiB
C#
using Godot;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
public partial class EnemyController : TurnController
|
|
{
|
|
public int _actionLoop = 0;
|
|
public PackedScene _enemyScene = GD.Load<PackedScene>("res://Enemy.tscn");
|
|
public List<Enemy> _enemies = new();
|
|
public PlayerController _playerController;
|
|
public Dictionary<int, List<Tween>> _tweenStages = new();
|
|
public Tween _tween;
|
|
|
|
public void AddEnemies(int ENEMY_COUNT = 1)
|
|
{
|
|
for (int i = 0; i < ENEMY_COUNT; i++)
|
|
{
|
|
Enemy newEnemy = _enemyScene.Instantiate<Enemy>();
|
|
newEnemy.Death += HandleEnemyRemoval;
|
|
newEnemy.Clicked += HandleEnemyClick;
|
|
newEnemy.RightClicked += HandleEnemyRightClick;
|
|
|
|
newEnemy._stamina = Globals._rng.Next(2,4+1);
|
|
newEnemy.Modulate = new Color(newEnemy._stamina == 2 ? "#FF0000" : newEnemy._stamina == 3 ? "#00FF00" : "#0000FF");
|
|
newEnemy._enemyController = this;
|
|
List<Vector2I> 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<Vector2I> POSITIONS)
|
|
{
|
|
for (int i = 0; i < POSITIONS.Count; i++)
|
|
{
|
|
Enemy newEnemy = _enemyScene.Instantiate<Enemy>();
|
|
newEnemy.Death += HandleEnemyRemoval;
|
|
newEnemy.Clicked += HandleEnemyClick;
|
|
newEnemy.RightClicked += HandleEnemyRightClick;
|
|
|
|
newEnemy._stamina = Globals._rng.Next(2,4+1);
|
|
newEnemy._hitRange = Globals._rng.Next(1,2+1);
|
|
newEnemy.GetNode<Sprite2D>("Sprite2D").SelfModulate = new Color(newEnemy._stamina == 2 ? "#FF0000" : newEnemy._stamina == 3 ? "#00FF00" : "#0000FF");
|
|
newEnemy._enemyController = this;
|
|
|
|
SetEnemy(newEnemy, POSITIONS[i]);
|
|
_enemies.Add(newEnemy);
|
|
AddChild(newEnemy);
|
|
}
|
|
|
|
}
|
|
|
|
public List<Enemy> GetRemainingEnemies()
|
|
{
|
|
return [.. _enemies.Where(e => e.CanMove(this) || e.CanAttack(this))];
|
|
}
|
|
|
|
public void HandleEnemyActions()
|
|
{
|
|
_actionLoop = 0;
|
|
_tweenStages.Clear();
|
|
Map map = _playArea._map;
|
|
|
|
_enemies.ForEach(e => e._path.Clear());
|
|
|
|
List<Enemy> remainingEnemies = GetRemainingEnemies();
|
|
while (remainingEnemies.Count > 0)
|
|
{
|
|
for (int i = 0; i < remainingEnemies.Count; i++)
|
|
{
|
|
Enemy enemy = remainingEnemies[i];
|
|
if (!enemy.CanMove(this) && !enemy.CanAttack(this))
|
|
{
|
|
GD.Print(i);
|
|
}
|
|
HandleEnemyPathing(enemy);
|
|
HandleEnemyAttacking(enemy);
|
|
}
|
|
remainingEnemies = GetRemainingEnemies();
|
|
_actionLoop++;
|
|
}
|
|
}
|
|
|
|
public void HandleEnemyAttacking(Enemy ENEMY)
|
|
{
|
|
if (!ENEMY.CanAttack(this))
|
|
{
|
|
return;
|
|
}
|
|
ENEMY.Attack(this);
|
|
}
|
|
|
|
|
|
public void HandleEnemyClick(Enemy ENEMY)
|
|
{
|
|
if (ENEMY._staminaRemaining <= 0){
|
|
return;
|
|
}
|
|
TileMapLayer pathLayer = _playArea.GetNode<TileMapLayer>("PathLayer");
|
|
List<Vector2I> newPath = ENEMY.GetBestPath(_playArea._map);
|
|
|
|
|
|
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)
|
|
{
|
|
HandleEnemyRemoval(ENEMY);
|
|
}
|
|
|
|
public void HandleEnemyPathing(Enemy ENEMY)
|
|
{
|
|
if (!ENEMY.CanMove(this))
|
|
{
|
|
return;
|
|
}
|
|
List<Vector2I> path = ENEMY.GetBestPath(_playArea._map);
|
|
|
|
if (path.Count == 0)
|
|
{
|
|
// ENEMY.Move(ENEMY._path.LastOrDefault(ENEMY._address), this);
|
|
return;
|
|
}
|
|
|
|
ENEMY.Move(path[0], this);
|
|
}
|
|
|
|
public void HandleEnemyRemoval(Enemy ENEMY_TO_REMOVE)
|
|
{
|
|
_enemies.Remove(ENEMY_TO_REMOVE);
|
|
_playArea._map.SetCellEnemy(ENEMY_TO_REMOVE._address, null);
|
|
ENEMY_TO_REMOVE.QueueFree();
|
|
}
|
|
|
|
public void HandleEnemySort()
|
|
{
|
|
_enemies = [.. _enemies.OrderBy(e => e._address.Y).ThenBy(e => e._path.Count).ThenBy(e => Math.Abs(e._address.X - _playArea._map._maxX / 2))];
|
|
}
|
|
|
|
|
|
public void Initiate()
|
|
{
|
|
List<Vector2I> positions = [.. _playArea.GetNode<TileMapLayer>("InitialPositions").GetUsedCells()];
|
|
|
|
AddEnemies(positions);
|
|
}
|
|
|
|
public void ProcessTween()
|
|
{
|
|
_tweenStages = _tweenStages.OrderBy(s => s.Key).ToDictionary();
|
|
GD.Print(string.Join(", ", _tweenStages.Keys));
|
|
if (_tweenStages.Count <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (_tween != null)
|
|
{
|
|
_tween.Kill();
|
|
}
|
|
|
|
_tween = CreateTween();
|
|
|
|
for (int i = 0; i < _tweenStages.Count; i++)
|
|
{
|
|
_tween.Chain().TweenInterval(0.5f);
|
|
List<Tween> tweenSteps = _tweenStages.Values.ElementAt(i);
|
|
for (int j = 0; j < tweenSteps.Count; j++)
|
|
{
|
|
Tween step = tweenSteps[j];
|
|
_tween.Parallel().TweenSubtween(step);
|
|
}
|
|
}
|
|
_tween.TweenCallback(Callable.From(EndTurn));
|
|
|
|
}
|
|
|
|
public override void StartTurn()
|
|
{
|
|
|
|
AddEnemies(1);
|
|
|
|
for (int i = 0; i < _enemies.Count; i++)
|
|
{
|
|
_enemies[i].StartTurn();
|
|
}
|
|
|
|
HandleEnemySort();
|
|
|
|
HandleEnemyActions();
|
|
|
|
ProcessTween();
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
}
|