Files
peggle-roguelike/Peg.cs
T

92 lines
2.8 KiB
C#

using Godot;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
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<MapCell> _path = new();
public PegController _pegController;
public Map _map;
public List<PegAction> _actions;
public int _distanceToGoal
{
get
{
return GetBestPath().Count;
}
}
public override void _Ready()
{
base._Ready();
_actions = [.. GetNode<Node2D>("Actions").GetChildren().Cast<PegAction>()];
_map = _pegController._playArea._map;
}
public virtual PegAction SelectAction()
{
return _actions.FirstOrDefault(a => a.MeetsCriteria(this), null);
}
public virtual void CounterAct(Commander COMMANDER)
{
}
public virtual List<MapCell> GetBestPath(bool PARTIAL = false)
{
MapCell goal = Goal();
return [.. _map.GetPath(_address, goal._address, false, PARTIAL).Select(a => _map._cells[a])];
}
public virtual Dictionary<Vector2I, MapCell> GetEnemies()
{
return _map._cells.Where(c => (int)(c.Value._occupant?._disposition ?? Disposition.NONE) == -(int)_disposition).ToDictionary();
}
public virtual Dictionary<Vector2I, MapCell> GetVisibleCells()
{
Map map = _pegController._playArea._map;
return map._cells.Where(c => (c.Value._address - _address).Length() <= _visibility).ToDictionary();
}
public virtual Dictionary<Vector2I, MapCell> GetVisibleEnemies()
{
Dictionary<Vector2I, MapCell> visible = GetVisibleCells();
Dictionary<Vector2I, MapCell> enemies = GetEnemies();
return enemies.Keys.Intersect(visible.Keys).ToDictionary(e => e, e => enemies[e]);
}
public virtual MapCell Goal()
{
Dictionary<Vector2I, MapCell> enemyTerritory = _map._cells.Where(c => _map.GetCellDisposition(c.Value._address) == (_disposition == Disposition.FRIENDLY ? -1 : 1)).ToDictionary();
Dictionary<Vector2I, MapCell> closest = enemyTerritory.OrderByDescending(c => c.Value._address.Y).ThenBy(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;
}
}
}