Files
peggle-roguelike/Peg.cs
T

120 lines
3.5 KiB
C#

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, _movementCost = 1;
public Dictionary<string, int> _priorities = new()
{
{"ball", 1000000}, // 1000000 to 1999999 reserved for ball priorities
{"movement", 0} // 0 to 999999 reserved for movement priorities
};
public Vector2I _address = -Vector2I.One, _range = Vector2I.Up;
public List<Vector2I> _path = new();
public PegController _pegController;
public PegAction _action;
public override void _Ready()
{
base._Ready();
}
public virtual void Act()
{
if (_action == null)
{
GD.Print("NO ACTION");
return;
}
Tween subtween = _action.CreateAnimation(this);
if (!_pegController._tweenStages.ContainsKey(_action._priority))
{
_pegController._tweenStages[_action._priority] = new();
}
_pegController._tweenStages[_action._priority].Add(subtween);
_staminaRemaining = Math.Max(_staminaRemaining - _action._cost, 0);
}
public virtual bool CanAct()
{
return _staminaRemaining > 0
&& _address.Y <= _action._range
&& _action._usesRemaining > 0
&& _staminaRemaining <= _action._cost;
}
public virtual bool CanMove()
{
return _staminaRemaining > 0
&& _address.Y > _pegController._playArea._map._firstOpenRow;
}
public virtual void CounterAct(Commander COMMANDER)
{
}
public virtual List<Vector2I> GetBestPath()
{
Map map = _pegController._playArea._map;
List<Vector2I> goals = GetGoals();
List<List<Vector2I>> paths = new();
for (int i = 0; i < goals.Count; i++)
{
paths.Add(map.GetPath(_address, goals[i]));
}
return paths.Where(p => p.Count > 0).OrderBy(p => p.Count).ToList()[0];
}
public virtual List<Vector2I> GetGoals()
{
Map map = _pegController._playArea._map;
return [.. map._cells.Where(c => c.Y == map._firstOpenRow).Where(c => !map._astar.IsPointSolid(c))];
}
public virtual void Move(Vector2I PATH_STEP)
{
_pegController._playArea._map.SetCellPeg(PATH_STEP, this);
_path.Add(PATH_STEP);
Tween subtween = CreateTween();
subtween.TweenProperty(this, "global_position", _pegController._playArea._map.GetCellPositionFromAddress(PATH_STEP), 0.25f);
int key = _priorities["movement"] + _pegController._actionLoop;
if (!_pegController._tweenStages.ContainsKey(key))
{
_pegController._tweenStages[key] = new();
}
_pegController._tweenStages[key].Add(subtween);
_staminaRemaining -= _movementCost;
}
public virtual void StartTurn()
{
_staminaRemaining = _stamina;
_action._usesRemaining = _action._usesMax;
GD.Print("ACTION ",_action._usesRemaining,", ",_action._usesRemaining);
}
public virtual void ChangeHealth(int DELTA, Commander BALLER)
{
_health += DELTA;
CounterAct(BALLER);
if (_health <= 0)
{
EmitSignal(SignalName.Death, this);
}
else if (_health > _healthMax)
{
_health = _healthMax;
}
}
}