215 lines
6.0 KiB
C#
215 lines
6.0 KiB
C#
using Godot;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
public partial class PegController : TurnController
|
|
{
|
|
public int _actionLoop = 0;
|
|
public PackedScene _hostilePegScene = GD.Load<PackedScene>("res://hostile_peg.tscn");
|
|
public List<Peg> _pegs = new();
|
|
public PlayerController _playerController;
|
|
public Dictionary<int, List<Tween>> _tweenStages = new();
|
|
public Tween _tween;
|
|
|
|
public void AddHostilePegs(int PEG_COUNT = 1)
|
|
{
|
|
for (int i = 0; i < PEG_COUNT; i++)
|
|
{
|
|
HostilePeg newHostilePeg = _hostilePegScene.Instantiate<HostilePeg>();
|
|
newHostilePeg.Death += HandlePegRemoval;
|
|
newHostilePeg.Clicked += HandlePegClick;
|
|
newHostilePeg.RightClicked += HandlePegRightClick;
|
|
|
|
newHostilePeg._stamina = Globals._rng.Next(2,4+1);
|
|
newHostilePeg.Modulate = new Color(newHostilePeg._stamina == 2 ? "#FF0000" : newHostilePeg._stamina == 3 ? "#00FF00" : "#0000FF");
|
|
newHostilePeg._pegController = this;
|
|
List<Vector2I> unoccupied = [.. _playArea._map._bottomRow.Where(c => _pegs.All(e => e._address != c))];
|
|
Vector2I randomCell = unoccupied[Globals._rng.Next(unoccupied.Count)];
|
|
SetPeg(newHostilePeg, randomCell);
|
|
_pegs.Add(newHostilePeg);
|
|
AddChild(newHostilePeg);
|
|
}
|
|
}
|
|
public void AddHostilePegs(List<Vector2I> POSITIONS)
|
|
{
|
|
for (int i = 0; i < POSITIONS.Count; i++)
|
|
{
|
|
HostilePeg newHostilePeg = _hostilePegScene.Instantiate<HostilePeg>();
|
|
newHostilePeg.Death += HandlePegRemoval;
|
|
newHostilePeg.Clicked += HandlePegClick;
|
|
newHostilePeg.RightClicked += HandlePegRightClick;
|
|
|
|
newHostilePeg._stamina = Globals._rng.Next(2,4+1);
|
|
newHostilePeg._hitRange = Globals._rng.Next(1,2+1);
|
|
newHostilePeg.GetNode<Sprite2D>("Sprite2D").SelfModulate = new Color(newHostilePeg._stamina == 2 ? "#FF0000" : newHostilePeg._stamina == 3 ? "#00FF00" : "#0000FF");
|
|
newHostilePeg._pegController = this;
|
|
|
|
SetPeg(newHostilePeg, POSITIONS[i]);
|
|
_pegs.Add(newHostilePeg);
|
|
AddChild(newHostilePeg);
|
|
}
|
|
|
|
}
|
|
|
|
public List<Peg> GetRemainingPegs()
|
|
{
|
|
return [.. _pegs.Where(e => e.CanMove() || e.CanAttack())];
|
|
}
|
|
|
|
public void HandlePegActions()
|
|
{
|
|
_actionLoop = 0;
|
|
_tweenStages.Clear();
|
|
Map map = _playArea._map;
|
|
|
|
_pegs.ForEach(e => e._path.Clear());
|
|
|
|
List<Peg> remainingPegs = GetRemainingPegs();
|
|
while (remainingPegs.Count > 0 && _actionLoop <100)
|
|
{
|
|
for (int i = 0; i < remainingPegs.Count; i++)
|
|
{
|
|
Peg peg = remainingPegs[i];
|
|
if (!peg.CanMove() && !peg.CanAttack())
|
|
{
|
|
GD.Print(i);
|
|
}
|
|
HandlePegPathing(peg);
|
|
HandlePegAttacking(peg);
|
|
}
|
|
remainingPegs = GetRemainingPegs();
|
|
_actionLoop++;
|
|
}
|
|
}
|
|
|
|
public void HandlePegAttacking(Peg PEG)
|
|
{
|
|
if (!PEG.CanAttack())
|
|
{
|
|
return;
|
|
}
|
|
PEG.Attack();
|
|
}
|
|
|
|
|
|
public void HandlePegClick(Peg PEG)
|
|
{
|
|
if (PEG._staminaRemaining <= 0){
|
|
return;
|
|
}
|
|
TileMapLayer pathLayer = _playArea.GetNode<TileMapLayer>("PathLayer");
|
|
List<Vector2I> newPath = PEG.GetBestPath();
|
|
|
|
|
|
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 HandlePegRightClick(Peg PEG)
|
|
{
|
|
HandlePegRemoval(PEG);
|
|
}
|
|
|
|
public void HandlePegPathing(Peg PEG)
|
|
{
|
|
if (!PEG.CanMove())
|
|
{
|
|
return;
|
|
}
|
|
List<Vector2I> path = PEG.GetBestPath();
|
|
|
|
if (path?.Count == 0)
|
|
{
|
|
// PEG.Move(PEG._path.LastOrDefault(PEG._address), this);
|
|
return;
|
|
}
|
|
|
|
PEG.Move(path[0]);
|
|
}
|
|
|
|
public void HandlePegRemoval(Peg PEG_TO_REMOVE)
|
|
{
|
|
_pegs.Remove(PEG_TO_REMOVE);
|
|
_playArea._map.SetCellPeg(PEG_TO_REMOVE._address, null);
|
|
PEG_TO_REMOVE.QueueFree();
|
|
}
|
|
|
|
public void HandlePegSort()
|
|
{
|
|
_pegs = [.. _pegs.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()];
|
|
|
|
AddHostilePegs(positions);
|
|
}
|
|
|
|
public void ProcessTween()
|
|
{
|
|
_tweenStages = _tweenStages.OrderBy(s => s.Key).ToDictionary();
|
|
GD.Print(string.Join(", ", _tweenStages.Keys));
|
|
if (_tweenStages.Count <= 0)
|
|
{
|
|
EndTurn();
|
|
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()
|
|
{
|
|
|
|
AddHostilePegs(1);
|
|
|
|
for (int i = 0; i < _pegs.Count; i++)
|
|
{
|
|
_pegs[i].StartTurn();
|
|
}
|
|
|
|
HandlePegSort();
|
|
|
|
HandlePegActions();
|
|
|
|
ProcessTween();
|
|
}
|
|
|
|
public void SetPeg(Peg PEG, Vector2I CELL)
|
|
{
|
|
if (CELL == new Vector2I(16, 14))
|
|
{
|
|
PEG._track = true;
|
|
}
|
|
_playArea._map.SetCellPeg(CELL, PEG);
|
|
PEG.GlobalPosition = _playArea._map.GetCellPositionFromAddress(CELL);
|
|
}
|
|
|
|
}
|