using Godot; using System; using System.Collections.Generic; using System.Linq; public partial class Enemy : StaticBody2D { [Signal] public delegate void DeathEventHandler(Enemy THIS); public int _damage = 1, _health = 2, _speed, _visibilityRange = 10; public Vector2I _range = Vector2I.Up; public float _movement = 0; public GridMarker _gridMarker; public EnemyController _enemyController; public override void _Ready() { base._Ready(); } public override void _PhysicsProcess(double delta) { base._PhysicsProcess(delta); } public void Attack(Commander COMMANDER) { COMMANDER.TakeDamage(_damage, this); } public void CounterAttack(Commander COMMANDER) { } public List Pathfinding(GridMarker DESTINATION, bool INCLUDE_SELF = false) { PriorityQueue queue = new(); queue.Enqueue(_gridMarker, 0f); Dictionary cameFrom = new(); Dictionary costSoFar = new(); cameFrom[_gridMarker] = null; costSoFar[_gridMarker] = 0f; GridMarker current; float newCost, priority; while (queue.Count > 0) { current = queue.Dequeue(); if (current == DESTINATION) { break; } List neighbors = current.GetMarkersInRange(1.5f); for (int j = 0; j < neighbors.Count; j++) { GridMarker next = neighbors[j]; if (next._occupant != null) { continue; } newCost = costSoFar[current] + 1; if (!costSoFar.TryGetValue(next, out float value) || newCost < value) { costSoFar[next] = newCost; priority = newCost; queue.Enqueue(next, priority); cameFrom[next] = current; } } } List path = new(); GridMarker pathMarker = DESTINATION; if (cameFrom.ContainsKey(pathMarker)) { while (pathMarker != null) { path.Add(pathMarker); pathMarker = cameFrom[pathMarker]; } path.Reverse(); if (!INCLUDE_SELF) { path.Remove(_gridMarker); } } return path; } public void TakeDamage(int DAMAGE, Commander ATTACKER) { _health -= DAMAGE; CounterAttack(ATTACKER); if (_health <= 0) { EmitSignal(SignalName.Death, this); } } }