using Godot; using System; using System.Collections.Generic; using System.Linq; public partial class Peg : HoverableNode { [Signal] public delegate void DeathEventHandler(Peg THIS); public int _id, _health = 2, _healthMax = 2, _stamina, _staminaRemaining, _visibility = 4, _movement = 0, _disposition; public Dictionary _priorities = new() { {"act", 1000000}, // 1000000 to 1999999 reserved for action priorities {"movement", 0} // 0 to 999999 reserved for movement priorities }; public Vector2I _address; public List _path = new(); public PegController _pegController; public List _actions; public int _distanceToGoal { get { return GetBestPath().Count; } } public override void _Ready() { base._Ready(); _actions = [.. GetNode("Actions").GetChildren().Cast()]; } public virtual bool Act(int SUB_STEP) { PegAction action = null; for (int i = 0; i < _actions.Count; i++) { if (_actions[i].MeetsCriteria(this)) { action = _actions[i]; break; } } if (action == null) { return false; } Tween subtween = action.CreateAnimation(this); string key = action._priority + ":" + SUB_STEP ; if (!_pegController._tweenStages.ContainsKey(key)) { _pegController._tweenStages[key] = new(); } _pegController._tweenStages[key].Add(subtween); _staminaRemaining -= action._cost; return true; } public virtual void CounterAct(Commander COMMANDER) { } public virtual List GetBestPath() { Map map = _pegController._playArea._map; List goals = GetGoals(); List> paths = new(); for (int i = 0; i < goals.Count; i++) { paths.Add(map.GetPath(_address, goals[i])); } List> pathsOverZeroCount = [.. paths.Where(p => p.Count > 0)]; if (pathsOverZeroCount.Count == 0) { return []; } return pathsOverZeroCount.OrderBy(p => p.Count).ToList()[0]; } public virtual List GetGoals() { Map map = _pegController._playArea._map; return [.. map._cells.Where(c => c.Y == (_disposition == 1 ? map._lastOpenRow : map._firstOpenRow)).Where(c => !map._astar.IsPointSolid(c))]; } public Vector2 GetPositionFromAddress() { return _pegController._playArea._map.GetCellPositionFromAddress(_address); } public virtual void StartTurn() { _staminaRemaining = _stamina; _actions.ForEach(a => a.Reset()); } public virtual void ChangeHealth(int DELTA, Commander COMMANDER) { _health += DELTA; CounterAct(COMMANDER); if (_health <= 0) { EmitSignal(SignalName.Death, this); } else if (_health > _healthMax) { _health = _healthMax; } } }