using Godot; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection.Metadata; using System.Threading.Tasks; using System.Xml; public partial class PegController : TurnController { public int _actionLoop = 0, _pegsCreated = 0, _pegsDestroyed = 0; public List _hostilePegScenes; public List _pegs = new(); public PlayerController _playerController; public Dictionary> _tweenStages = new(); public Tween _tween; public XmlDocument _pegProbabilities = new(); public override void _Ready() { base._Ready(); _hostilePegScenes = [.. Directory.GetFiles("Pegs/HostilePegs/", "*.tscn").Select(f => GD.Load(f))]; // _pegProbabilities.Load("res://PegProbabilities.xml"); } public void AddHostilePegs(int PEG_COUNT = 1) { for (int i = 0; i < PEG_COUNT; i++) { HostilePeg newHostilePeg = Globals.GetRandomFromList(_hostilePegScenes).Instantiate(); newHostilePeg._id = _pegsCreated; newHostilePeg.Death += HandlePegRemoval; newHostilePeg.Click += HandlePegClick; newHostilePeg._pegController = this; List 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); _pegsCreated++; } } public void AddHostilePegs(List POSITIONS) { for (int i = 0; i < POSITIONS.Count; i++) { HostilePeg newHostilePeg = Globals.GetRandomFromList(_hostilePegScenes).Instantiate(); newHostilePeg._id = _pegsCreated; newHostilePeg.Death += HandlePegRemoval; newHostilePeg.Click += HandlePegClick; newHostilePeg._pegController = this; SetPeg(newHostilePeg, POSITIONS[i]); _pegs.Add(newHostilePeg); AddChild(newHostilePeg); _pegsCreated++; } } public List GetRemainingPegs() { return [.. _pegs.Where(e => e.CanMove() || e.CanAct())]; } public void HandlePegTurn() { _actionLoop = 0; _tweenStages.Clear(); Map map = _playArea._map; _pegs.ForEach(e => e._path.Clear()); List remainingPegs = GetRemainingPegs(); while (remainingPegs.Count > 0) { for (int i = 0; i < remainingPegs.Count; i++) { Peg peg = remainingPegs[i]; HandlePegPathing(peg); HandlePegAction(peg); } remainingPegs = GetRemainingPegs(); _actionLoop++; if (_actionLoop == 10) { Peg peg_ = remainingPegs[0]; GD.Print("LOOPMAXEDOUT"); } } } public void HandlePegAction(Peg PEG) { if (PEG.CanAct()) { PEG.Act(); } } public void HandlePegClick(Node CLICKED_NODE, int CLICK_TYPE) { if (CLICKED_NODE is not Peg peg) { return; } if (CLICK_TYPE == 0) { if (peg._staminaRemaining <= 0){ return; } TileMapLayer pathLayer = _playArea.GetNode("PathLayer"); List 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); } } else if (CLICK_TYPE == 2) { HandlePegRemoval(peg); } } public void HandlePegPathing(Peg PEG) { if (!PEG.CanMove()) { return; } List path = PEG.GetBestPath(); if (path?.Count == 0) { GD.Print(PEG._address, " PATH0"); 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(); _pegsDestroyed++; } 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 positions = [.. _playArea.GetNode("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 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(4); for (int i = 0; i < _pegs.Count; i++) { _pegs[i].StartTurn(); } HandlePegSort(); HandlePegTurn(); ProcessTween(); } public void SetPeg(Peg PEG, Vector2I CELL) { _playArea._map.SetCellPeg(CELL, PEG); PEG.GlobalPosition = _playArea._map.GetCellPositionFromAddress(CELL); } }