adding in some example pegs and their actions. organizing folders, still working on pathing for some reason it won't move sometimes, and causes the looping to break

This commit is contained in:
2026-06-26 02:36:01 -04:00
parent fc32baa071
commit 350d2bc4d1
33 changed files with 366 additions and 165 deletions
+40 -30
View File
@@ -2,62 +2,75 @@ 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;
public PackedScene _hostilePegScene = GD.Load<PackedScene>("res://hostile_peg.tscn");
public int _actionLoop = 0, _pegsCreated = 0, _pegsDestroyed = 0;
public List<PackedScene> _hostilePegScenes;
public List<Peg> _pegs = new();
public PlayerController _playerController;
public Dictionary<int, List<Tween>> _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<PackedScene>(f))];
// _pegProbabilities.Load("res://PegProbabilities.xml");
}
public void AddHostilePegs(int PEG_COUNT = 1)
{
for (int i = 0; i < PEG_COUNT; i++)
{
HostilePeg newHostilePeg = _hostilePegScene.Instantiate<HostilePeg>();
HostilePeg newHostilePeg = Globals.GetRandomFromList(_hostilePegScenes).Instantiate<HostilePeg>();
newHostilePeg._id = _pegsCreated;
newHostilePeg.Death += HandlePegRemoval;
newHostilePeg.Click += HandlePegClick;
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);
_pegsCreated++;
}
}
public void AddHostilePegs(List<Vector2I> POSITIONS)
{
for (int i = 0; i < POSITIONS.Count; i++)
{
HostilePeg newHostilePeg = _hostilePegScene.Instantiate<HostilePeg>();
HostilePeg newHostilePeg = Globals.GetRandomFromList(_hostilePegScenes).Instantiate<HostilePeg>();
newHostilePeg._id = _pegsCreated;
newHostilePeg.Death += HandlePegRemoval;
newHostilePeg.Click += HandlePegClick;
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);
_pegsCreated++;
}
}
public List<Peg> GetRemainingPegs()
{
return [.. _pegs.Where(e => e.CanMove() || e.CanAttack())];
return [.. _pegs.Where(e => e.CanMove() || e.CanAct())];
}
public void HandlePegActions()
public void HandlePegTurn()
{
_actionLoop = 0;
_tweenStages.Clear();
@@ -66,30 +79,31 @@ public partial class PegController : TurnController
_pegs.ForEach(e => e._path.Clear());
List<Peg> remainingPegs = GetRemainingPegs();
while (remainingPegs.Count > 0 && _actionLoop <100)
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);
HandlePegAction(peg);
}
remainingPegs = GetRemainingPegs();
_actionLoop++;
if (_actionLoop == 100)
{
GD.Print(string.Join(", ",remainingPegs.Select(p => p._address.ToString() + p._staminaRemaining+p.CanMove()+p.CanAct())));
GD.Print("LOOPMAXEDOUT");
}
}
}
public void HandlePegAttacking(Peg PEG)
public void HandlePegAction(Peg PEG)
{
if (!PEG.CanAttack())
if (PEG.CanAct())
{
return;
PEG.Act();
}
PEG.Attack();
}
@@ -107,7 +121,6 @@ public partial class PegController : TurnController
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++)
{
@@ -127,10 +140,10 @@ public partial class PegController : TurnController
return;
}
List<Vector2I> path = PEG.GetBestPath();
if (path?.Count == 0)
{
// PEG.Move(PEG._path.LastOrDefault(PEG._address), this);
GD.Print(PEG._address, " PATH0");
return;
}
@@ -142,6 +155,7 @@ public partial class PegController : TurnController
_pegs.Remove(PEG_TO_REMOVE);
_playArea._map.SetCellPeg(PEG_TO_REMOVE._address, null);
PEG_TO_REMOVE.QueueFree();
_pegsDestroyed++;
}
public void HandlePegSort()
@@ -191,7 +205,7 @@ public partial class PegController : TurnController
public override void StartTurn()
{
AddHostilePegs(1);
AddHostilePegs(4);
for (int i = 0; i < _pegs.Count; i++)
{
@@ -200,17 +214,13 @@ public partial class PegController : TurnController
HandlePegSort();
HandlePegActions();
HandlePegTurn();
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);
}