using Godot; using System; using System.Collections.Generic; using System.Linq; public partial class Peg : MapCellOccupant { [Signal] public delegate void DeathEventHandler(Peg THIS); public int _id, _health = 2, _healthMax = 2, _stamina, _staminaRemaining, _movement = 0, _visibility; public List _path = new(); public PegController _pegController; public Map _map; public List _actions; public int _distanceToGoal { get { return GetBestPath().Count; } } public override void _Ready() { base._Ready(); _actions = [.. GetNode("Actions").GetChildren().Cast()]; _map = _pegController._playArea._map; } public virtual PegAction SelectAction() { PegAction action = null; for (int i = 0; i < _actions.Count; i++) { if (_actions[i].MeetsCriteria(this)) { action = _actions[i]; break; } } return action; } public virtual void CounterAct(Commander COMMANDER) { } public virtual List GetBestPath(bool PARTIAL = false) { // Map map = _pegController._playArea._map; // List goals = Target(); // for (int i = 0; i < goals.Count; i++) // { // List path = map.GetPath(_address, goals[i], false, PARTIAL); // if (path.Count > 0) // { // return path; // } // } MapCell goal = Goal(); return _pegController._playArea._map.GetPath(_address, goal._address, false, PARTIAL); } public virtual Dictionary GetEnemies() { Map map = _pegController._playArea._map; return map._cells.Where(c => (int)c.Value._occupant._disposition == -(int)_disposition).ToDictionary(); } public virtual Dictionary GetVisibleCells() { Map map = _pegController._playArea._map; return map._cells.Where(c => (c.Value._address - _address).Length() <= _visibility).ToDictionary(); } public virtual Dictionary GetVisibleEnemies() { Dictionary visible = GetVisibleCells(); Dictionary enemies = GetEnemies(); return enemies.Keys.Intersect(visible.Keys).ToDictionary(e => e, e => enemies[e]); } public Vector2 GetPositionFromAddress() { return _pegController._playArea._map.GetCellPositionFromAddress(_address); } public virtual MapCell Goal() { Map map = _pegController._playArea._map; Dictionary visible = GetVisibleCells(); Dictionary unoccupied = visible.Where(c => !map._astar.IsPointSolid(c.Key)).ToDictionary(); Dictionary closest = unoccupied.OrderByDescending(c => c.Value._address.Y * (int)_disposition).ThenByDescending(c => Math.Abs(c.Value._address.X - _address.X)).ToDictionary(); return closest.ElementAt(0).Value; } public virtual void StartTurn() { _staminaRemaining = _stamina; _actions.ForEach(a => a.Reset()); } public virtual void ChangeHealth(int DELTA) { _health += DELTA; if (_health <= 0) { EmitSignal(SignalName.Death, this); } else if (_health > _healthMax) { _health = _healthMax; } } }