124 lines
3.4 KiB
C#
124 lines
3.4 KiB
C#
using Godot;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
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, _warp = false;
|
|
public int _damage = 1, _health = 2, _stamina, _staminaRemaining, _visibilityRange = 4, _hitRange;
|
|
public Dictionary<string, int> _priorities = new()
|
|
{
|
|
{"attack", 0}, // 0 to 999999 reserved for attacking priorities
|
|
{"movement", 1000000} // 1000000 to 1999999 reserved for movement priorities
|
|
};
|
|
public Vector2I _address = -Vector2I.One, _range = Vector2I.Up;
|
|
public List<Vector2I> _path = new(), _pathStored = 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 AnimateMovement(EnemyController CONTROLLER)
|
|
{
|
|
for (int i = 0; i < _path.Count; i++)
|
|
{
|
|
Tween subtween = CreateTween();
|
|
subtween.TweenProperty(this, "global_position", CONTROLLER._playArea._map.GetCellPositionFromAddress(_path[i]), 0.25f);
|
|
int key = _priorities["movement"] + i;
|
|
|
|
if (!CONTROLLER._tweenStages.ContainsKey(key))
|
|
{
|
|
CONTROLLER._tweenStages[key] = new();
|
|
}
|
|
CONTROLLER._tweenStages[key].Add(subtween);
|
|
}
|
|
|
|
}
|
|
|
|
public void Attack(PlayerController PLAYER)
|
|
{
|
|
PLAYER.ChangeHealth(-1, this);
|
|
}
|
|
|
|
public bool CanAttack()
|
|
{
|
|
return _address.Y <= _hitRange;
|
|
}
|
|
|
|
public void CounterAttack(Commander COMMANDER)
|
|
{
|
|
|
|
}
|
|
|
|
public virtual List<Vector2I> GetGoals(Map MAP)
|
|
{
|
|
return [.. MAP._cells.Where(c => c.Y == Math.Max(_address.Y - _visibilityRange, MAP._firstOpenRow)).Where(c => !MAP._astar.IsPointSolid(c))];
|
|
}
|
|
|
|
public virtual List<Vector2I> GetBestPath(Map MAP)
|
|
{
|
|
List<Vector2I> goals = GetGoals(MAP);
|
|
List<List<Vector2I>> paths = new();
|
|
|
|
for (int i = 0; i < goals.Count; i++)
|
|
{
|
|
paths.Add(MAP.GetPath(_address, goals[i]));
|
|
}
|
|
List<Vector2I> bestPath = paths.Where(p => p.Count > 0).OrderBy(p => p.Count).ThenBy(p => p.Last().X % 2).ToList()[0];
|
|
return bestPath;
|
|
}
|
|
|
|
public void StartTurn()
|
|
{
|
|
_staminaRemaining = _stamina;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|