Files
peggle-roguelike/Peg.cs
T

148 lines
4.6 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);
[Signal]
public delegate void ClickedEventHandler(Peg THIS);
[Signal]
public delegate void RightClickedEventHandler(Peg THIS);
public bool _track = false, _warp = false;
public int _damage = 1, _health = 2, _stamina, _staminaRemaining, _visibilityRange = 4, _hitRange, _attackCost = 1;
public Dictionary<string, int> _priorities = new()
{
{"attack", 1000000}, // 1000000 to 1999999 reserved for attack priorities
{"movement", 0} // 0 to 999999 reserved for movement priorities
};
public Vector2I _address = -Vector2I.One, _range = Vector2I.Up;
public List<Vector2I> _path = new();
public float _movement = 0;
public PegController _pegController;
public Sprite2D _attack;
public override void _Ready()
{
base._Ready();
_attack = GetNode<Sprite2D>("Attack");
}
public override void _Process(double delta)
{
base._Process(delta);
if (_hovered)
{
if (Input.IsActionJustPressed("leftClick"))
{
EmitSignal(SignalName.Clicked, this);
}
if (Input.IsActionJustPressed("rightClick"))
{
EmitSignal(SignalName.RightClicked, this);
}
}
}
public void Attack()
{
// _attack.Visible = true;
Tween subtween = CreateTween();
subtween.TweenProperty(_attack, "visible", true, 0.0f);
subtween.TweenProperty(_attack, "global_position", Vector2.Zero, 0.5f);
subtween.TweenCallback(Callable.From(() =>
{
_pegController._playerController.ChangeHealth(-1, this);
_attack.Position = Vector2.Zero;
_attack.Visible = false;
}));
int key = _priorities["attack"];
if (!_pegController._tweenStages.ContainsKey(key))
{
_pegController._tweenStages[key] = new();
}
_pegController._tweenStages[key].Add(subtween);
_staminaRemaining = Math.Max(_staminaRemaining - 2, 0);
}
public bool CanAttack()
{
return _staminaRemaining > 0 && _address.Y <= _hitRange;
}
public bool CanMove()
{
// GD.Print(_staminaRemaining," ",_address.Y," ", CONTROLLER._playArea._map._firstOpenRow);
return _staminaRemaining > 0 && _address.Y > _pegController._playArea._map._firstOpenRow;
}
public void CounterAttack(Commander COMMANDER)
{
}
public virtual List<Vector2I> GetBestPath()
{
Map map = _pegController._playArea._map;
List<Vector2I> goals = GetGoals();
// List<List<Vector2I>> paths = new();
Vector2I bestGoal = goals[0];
float bestCost = map._astar._EstimateCost(_address, bestGoal);
for (int i = 1; i < goals.Count; i++)
{
Vector2I testGoal = goals[i];
float testCost = map._astar._EstimateCost(_address, testGoal);
if (testCost < bestCost)
{
bestCost = testCost;
bestGoal = testGoal;
}
}
// List<Vector2I> bestPath = paths.Where(p => p.Count > 0).OrderBy(p => p.Count).ToList()[0];
List<Vector2I> bestPath = map.GetPath(_address, bestGoal);
return bestPath;
}
public virtual List<Vector2I> GetGoals()
{
Map map = _pegController._playArea._map;
return [.. map._cells.Where(c => c.Y == Math.Max(_address.Y - _visibilityRange, map._firstOpenRow)).Where(c => !map._astar.IsPointSolid(c))];
}
public 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;
GD.Print(key);
if (!_pegController._tweenStages.ContainsKey(key))
{
_pegController._tweenStages[key] = new();
}
_pegController._tweenStages[key].Add(subtween);
_staminaRemaining--;
}
public void StartTurn()
{
_staminaRemaining = _stamina;
}
public void TakeDamage(int DAMAGE, Commander ATTACKER)
{
_health -= DAMAGE;
CounterAttack(ATTACKER);
if (_health <= 0)
{
EmitSignal(SignalName.Death, this);
}
}
}