Files
peggle-roguelike/Enemy.cs
T

106 lines
2.8 KiB
C#

using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
public partial class Enemy : StaticBody2D
{
[Signal]
public delegate void DeathEventHandler(Enemy THIS);
[Signal]
public delegate void ClickedEventHandler(Enemy THIS);
[Signal]
public delegate void RightClickedEventHandler(Enemy THIS);
public bool _hovered = false, _track = false;
public int _damage = 1, _health = 2, _speed, _speedRemaining, _visibilityRange = 4, _hitRange = 1;
public Vector2I _address = -Vector2I.One, _range = Vector2I.Up;
public List<Vector2I> _path = new();
public float _movement = 0;
public EnemyController _enemyController;
public override void _Ready()
{
base._Ready();
}
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 override void _PhysicsProcess(double delta)
{
base._PhysicsProcess(delta);
}
public void Attack(Commander COMMANDER)
{
COMMANDER.TakeDamage(_damage, this);
}
public void CounterAttack(Commander COMMANDER)
{
}
public virtual List<Vector2I> GetBestPath(Map MAP)
{
Vector2I goal = Vector2I.One * -1000;
goal.Y = MAP.GetFirstOpenRow();
List<Vector2I> openCells = [.. MAP._cells.Where(c => c.Y == goal.Y && !MAP._astar.IsPointSolid(c))];
List<Vector2I> testPath, bestPath = [];
int bestLength = 999999;
for (int i = 0; i < openCells.Count; i++)
{
testPath = MAP.GetPath(_address, openCells[i]);
int testLength = testPath.Count;
if (testLength < bestLength)
{
bestPath = testPath;
bestLength = testLength;
goal = openCells[i];
}
else if (testLength == bestLength)
{
if (Math.Abs(openCells[i].X - _address.X) < Math.Abs(goal.X - _address.X))
{
bestPath = testPath;
bestLength = testLength;
goal = openCells[i];
}
}
}
return bestPath;
}
public void TakeDamage(int DAMAGE, Commander ATTACKER)
{
_health -= DAMAGE;
CounterAttack(ATTACKER);
if (_health <= 0)
{
EmitSignal(SignalName.Death, this);
}
}
public void OnMouseEntered(){
_hovered = true;
}
public void OnMouseExited(){
_hovered = false;
}
}