using Godot; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection.Metadata; using System.Reflection.Metadata.Ecma335; using System.Threading; using System.Threading.Tasks; using System.Xml; public partial class PegController : TurnController { public int _pegsCreated = 0, _pegsDestroyed = 0; public List _hostilePegScenes, _friendlyPegScenes, _neutralPegScenes; 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))]; _friendlyPegScenes = [.. Directory.GetFiles("Pegs/FriendlyPegs/", "*.tscn").Select(f => GD.Load(f))]; // _neutralPegScenes = [.. Directory.GetFiles("Pegs/NeutralPegs/", "*.tscn").Select(f => GD.Load(f))]; // _pegProbabilities.Load("res://PegProbabilities.xml"); } public void AddFriendlyPegs(int PEG_COUNT = 1) { for (int i = 0; i < PEG_COUNT; i++) { FriendlyPeg newFriendlyPeg = Globals.GetRandomFromList(_friendlyPegScenes).Instantiate(); newFriendlyPeg._id = _pegsCreated; newFriendlyPeg.Death += HandlePegRemoval; newFriendlyPeg.Click += HandlePegClick; newFriendlyPeg._pegController = this; Dictionary unoccupied = _playArea._map._bottomRow.Where(c => c.Value._occupant == null).ToDictionary(); MapCell randomCell = unoccupied.ElementAt(Globals._rng.Next(unoccupied.Keys.Count)).Value; SetPeg(newFriendlyPeg, randomCell); _pegs.Add(newFriendlyPeg); AddChild(newFriendlyPeg); _pegsCreated++; } } public void AddFriendlyPegs(List POSITIONS) { for (int i = 0; i < POSITIONS.Count; i++) { FriendlyPeg newFriendlyPeg = Globals.GetRandomFromList(_friendlyPegScenes).Instantiate(); newFriendlyPeg._id = _pegsCreated; newFriendlyPeg.Scale *= _pegsCreated == 4 ? 1.5f : 1f; newFriendlyPeg.Death += HandlePegRemoval; newFriendlyPeg.Click += HandlePegClick; newFriendlyPeg._pegController = this; SetPeg(newFriendlyPeg, _map._cells[POSITIONS[i]]); _pegs.Add(newFriendlyPeg); AddChild(newFriendlyPeg); _pegsCreated++; } } 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; Dictionary unoccupied = _map._bottomRow.Where(c => c.Value._occupant == null).ToDictionary(); MapCell randomCell = unoccupied.ElementAt(Globals._rng.Next(unoccupied.Count)).Value; 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.Scale *= _pegsCreated == 4 ? 1.5f : 1f; newHostilePeg.Death += HandlePegRemoval; newHostilePeg.Click += HandlePegClick; newHostilePeg._pegController = this; newHostilePeg._map = _playArea._map; SetPeg(newHostilePeg, _map._cells[POSITIONS[i]]); _pegs.Add(newHostilePeg); AddChild(newHostilePeg); _pegsCreated++; } } public void HandlePegTurn() { _tweenStages.Clear(); int loop = 0; bool anyPegsActed = true; while (anyPegsActed) { anyPegsActed = false; for (int i = 0; i < _pegs.Count; i++) { Peg peg = _pegs[i]; PegAction action = peg.SelectAction(); if (action != null) { action.DoImmediately(peg); string key = action._priority + ":" + loop; if (!_tweenStages.ContainsKey(key)) { _tweenStages[key] = new(); } _tweenStages[key].Add(new(peg, action)); if (!anyPegsActed) { anyPegsActed = true; } peg._staminaRemaining -= action._cost; } } loop++; } ProcessTween(); } public void HandlePegClick(Node CLICKED_NODE, int CLICK_TYPE) { if (CLICKED_NODE is not Peg peg) { return; } if (CLICK_TYPE == 0) { 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); // } Map map = _playArea._map; TileMapLayer pathLayer = _playArea.GetNode("PathLayer"); List pl = [.. pathLayer.GetUsedCells()]; Dictionary visible = peg.GetVisibleCells(); for (int i = 0; i < pl.Count; i++) { Vector2I cell = pl[i]; pathLayer.GetUsedCells().ToList().ForEach(c => pathLayer.SetCell(cell,0,Vector2I.Down + Vector2I.Right*(visible.ContainsKey(cell) ? 1 : 4))); } } else if (CLICK_TYPE == 2) { HandlePegRemoval(peg); } } public void HandlePegRemoval(Peg PEG_TO_REMOVE) { _pegs.Remove(PEG_TO_REMOVE); _playArea._map.SetCellPeg(_map._cells[PEG_TO_REMOVE._address], null); PEG_TO_REMOVE.QueueFree(); _pegsDestroyed++; } public void HandlePegSort() { _pegs = [.. _pegs.OrderBy(p => p._address.Y).ThenBy(p => p._distanceToGoal).ThenBy(p => Math.Abs(p._address.X - _playArea._map._maxX / 2))]; } public void Initiate() { TileMapLayer init = _playArea.GetNode("InitialPositions"); List initPos = [.. init.GetUsedCells()]; List positions = [.. initPos.OrderBy(c => c.Y).ThenBy(c => c.X)]; List hPositions = [.. positions.Where(c => (int)init.GetCellTileData(c).GetCustomData("disposition") == -1)]; List fPositions = [.. positions.Where(c => (int)init.GetCellTileData(c).GetCustomData("disposition") == 1)]; AddHostilePegs(hPositions); AddFriendlyPegs(fPositions); } public void ProcessTween() { _tweenStages = _tweenStages.OrderBy(s => int.Parse(s.Key.Split(":")[0])).ThenBy(s => int.Parse(s.Key.Split(":")[1])).ToDictionary(); if (_tweenStages.Count <= 0) { EndTurn(); return; } _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++) { Tuple step = tweenSteps[j]; Peg peg = step.Item1; PegAction action = step.Item2; Tween tween = action.CreateAnimation(peg); _tween.Parallel().TweenSubtween(tween); } } _tween.TweenCallback(Callable.From(EndTurn)); } public override void StartTurn() { AddHostilePegs(4); for (int i = 0; i < _pegs.Count; i++) { _pegs[i].StartTurn(); } HandlePegSort(); HandlePegTurn(); } public void SetPeg(Peg PEG, MapCell CELL) { _map.SetCellPeg(CELL, PEG); PEG._cell = CELL; PEG.GlobalPosition = CELL.GlobalPosition; } }