still attempting to allow enemies to handle their own animations. still a matter of improving performance and there's some issue when enemies reach rank 0?

This commit is contained in:
2026-06-24 03:00:23 -04:00
parent 8ae8ec3e58
commit 98ca292901
6 changed files with 84 additions and 86 deletions
+13 -7
View File
@@ -2,6 +2,7 @@ using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
public partial class Enemy : StaticBody2D
{
@@ -12,7 +13,12 @@ public partial class Enemy : StaticBody2D
[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, _priority = 1;
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;
@@ -49,7 +55,8 @@ public partial class Enemy : StaticBody2D
{
Tween subtween = CreateTween();
subtween.TweenProperty(this, "global_position", CONTROLLER._playArea._map.GetCellPositionFromAddress(_path[i]), 0.25f);
float key = _priority + i/1000;
int key = _priorities["movement"] + i;
if (!CONTROLLER._tweenStages.ContainsKey(key))
{
CONTROLLER._tweenStages[key] = new();
@@ -76,8 +83,7 @@ public partial class Enemy : StaticBody2D
public virtual List<Vector2I> GetGoals(Map MAP)
{
List<Vector2I> firstOpenRow = [.. MAP._cells.Where(c => c.Y == MAP.GetFirstOpenRow() && !MAP._astar.IsPointSolid(c))];
return firstOpenRow;
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)
@@ -89,13 +95,13 @@ public partial class Enemy : StaticBody2D
{
paths.Add(MAP.GetPath(_address, goals[i]));
}
List<Vector2I> bestPath = paths.OrderBy(p => p.Count).ThenBy(p => p.Last().X % 2).ToList()[0];
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 GetRemainingStamina(EnemyController CONTROLLER)
public void StartTurn()
{
_staminaRemaining = _address.Y <= CONTROLLER._playArea._map.GetFirstOpenRow() ? 0 : _staminaRemaining;
_staminaRemaining = _stamina;
}
public void TakeDamage(int DAMAGE, Commander ATTACKER)
+50 -50
View File
@@ -11,7 +11,7 @@ public partial class EnemyController : TurnController
public PackedScene _enemyScene = GD.Load<PackedScene>("res://Enemy.tscn");
public List<Enemy> _enemies = new();
public PlayerController _playerController;
public Dictionary<float, List<Tween>> _tweenStages = new();
public Dictionary<int, List<Tween>> _tweenStages = new();
public Tween _tween;
public void AddEnemies(int ENEMY_COUNT = 1)
@@ -54,41 +54,30 @@ public partial class EnemyController : TurnController
}
public void EnemyAttacks()
public void BuildAnimationTween()
{
List<Enemy> attackingEnemies = [.. _enemies.Where(e => e.CanAttack())];
for (int i = 0; i < attackingEnemies.Count; i++)
{
Enemy enemy = attackingEnemies[i];
enemy.Attack(_playerController);
enemy._staminaRemaining = Math.Max(enemy._staminaRemaining - 2, 0);
}
}
public async Task BuildEnemyPaths()
{
Map map = _playArea._map;
for (int i = 0; i < _enemies.Count; i++)
_tweenStages.Clear();
for (int i= 0; i < _enemies.Count; i++)
{
Enemy enemy = _enemies[i];
enemy.GetRemainingStamina(this);
enemy.AnimateMovement(this);
}
_tweenStages.OrderBy(s => s.Key);
}
public void BuildEnemyPaths()
{
Map map = _playArea._map;
_enemies.ForEach(e => e._path.Clear());
if (_enemies.All(e => e._staminaRemaining <= 0))
{
return;
}
while (_enemies.Any(e => e._staminaRemaining > 0))
{
for (int i = 0; i < _enemies.Count; i++)
{
Enemy enemy = _enemies[i];
if (enemy._staminaRemaining <= 0 || enemy._address.Y <= map.GetFirstOpenRow())
if (enemy._staminaRemaining <= 0 || enemy._address.Y <= map._firstOpenRow)
{
GD.Print(enemy._staminaRemaining <= 0, enemy._address.Y <= map._firstOpenRow);
enemy._path.Add(enemy._path.LastOrDefault(enemy._address));
continue;
}
@@ -100,9 +89,7 @@ public partial class EnemyController : TurnController
enemy._path.Add(enemy._path.LastOrDefault(enemy._address));
continue;
}
Vector2I cell = path[0];
map.SetCellEnemy(cell, enemy);
enemy._path.Add(cell);
@@ -111,6 +98,18 @@ public partial class EnemyController : TurnController
}
}
public void EnemyAttacks()
{
List<Enemy> attackingEnemies = [.. _enemies.Where(e => e.CanAttack())];
for (int i = 0; i < attackingEnemies.Count; i++)
{
Enemy enemy = attackingEnemies[i];
enemy.Attack(_playerController);
enemy._staminaRemaining = Math.Max(enemy._staminaRemaining - 2, 0);
}
}
public void HandleEnemyClick(Enemy ENEMY)
{
if (ENEMY._staminaRemaining <= 0){
@@ -141,37 +140,31 @@ public partial class EnemyController : TurnController
public void ProcessTween()
{
if (_tweenStages.Count <= 0)
{
return;
}
if (_tween != null)
{
_tween.Kill();
}
_tween = CreateTween();
_tween.TweenInterval(0.5f);
List<float> tweenPriorities = [.. _tweenStages.Keys];
for (int i = 0; i < tweenPriorities.Count; i++)
_tween = CreateTween();
for (int i = 0; i < _tweenStages.Count; i++)
{
float stage = tweenPriorities[i];
List<Tween> stageTweens = _tweenStages[stage];
_tween.Chain().TweenSubtween(stageTweens[0]);
for (int j = 1; j < stageTweens.Count; j++)
_tween.Chain().TweenInterval(0.5f);
List<Tween> tweenSteps = _tweenStages.Values.ElementAt(i);
for (int j = 0; j < tweenSteps.Count; j++)
{
Tween step = stageTweens[j];
Tween step = tweenSteps[j];
_tween.Parallel().TweenSubtween(step);
}
}
_tween.TweenCallback(Callable.From(() => EndTurn()));
}
_tween.TweenCallback(Callable.From(EndTurn));
public void StepEnemies(int STEP)
{
Map map = _playArea._map;
for (int i= 0; i < _enemies.Count; i++)
{
Enemy enemy = _enemies[i];
enemy.AnimateMovement(this);
}
}
public void RemoveEnemy(Enemy ENEMY_TO_REMOVE)
@@ -183,20 +176,27 @@ public partial class EnemyController : TurnController
public void SortEnemies()
{
_enemies = [.. _enemies.OrderByDescending(e => e._priority).ThenBy(e => e._address.Y).ThenBy(e => e._path.Count).ThenBy(e => Math.Abs(e._address.X - _playArea._map._maxX / 2))];
_enemies = [.. _enemies.OrderBy(e => e._address.Y).ThenBy(e => e._path.Count).ThenBy(e => Math.Abs(e._address.X - _playArea._map._maxX / 2))];
}
public override async Task StartTurn()
public override void StartTurn()
{
AddEnemies(1);
for (int i = 0; i < _enemies.Count; i++)
{
_enemies[i].StartTurn();
}
SortEnemies();
EnemyAttacks();
await BuildEnemyPaths();
BuildEnemyPaths();
StepEnemies(0);
BuildAnimationTween();
ProcessTween();
}
public void SetEnemy(Enemy ENEMY, Vector2I CELL)
+4 -13
View File
@@ -43,24 +43,15 @@ public partial class Main : Node
{
if(_turnController == _playerController)
{
ChangeTurn();
_playerController.EndTurn();
}
}
}
public async void ChangeTurn()
public void ChangeTurn()
{
if (_turnController != _playerController)
{
_turnController = _playerController;
await _playerController.StartTurn();
}
else
{
_turnController = _enemyController;
await _enemyController.StartTurn();
}
_turnController = _turnController == _playerController ? _enemyController : _playerController;
_turnController.StartTurn();
}
public void EndGame(PlayerController PLAYER)
+15 -14
View File
@@ -13,7 +13,20 @@ public partial class Map : TileMapLayer
public List<Vector2I> _cells = new(), _leftmostColumn = new(), _rightmostColumn = new(), _topRow = new(), _bottomRow = new();
public AStarGrid2D _astar = new();
public Dictionary<Vector2I, Enemy> _addressOccupants = new();
public int _firstOpenRow
{
get
{
for (int i = 0; i < _maxY; i++)
{
if (!IsRowFull(i))
{
return i;
}
}
return 1;
}
}
public override void _Ready()
{
base._Ready();
@@ -51,18 +64,6 @@ public partial class Map : TileMapLayer
return GlobalPosition + CELL_ADDRESS * _cellSize + _cellSize / 2;
}
public int GetFirstOpenRow()
{
for (int i = 0; i < _maxY; i++)
{
if (!IsRowFull(i))
{
return i;
}
}
return 1;
}
public Enemy GetOccupant(Vector2I CELL_TO_CHECK)
{
return _addressOccupants[CELL_TO_CHECK];
@@ -130,7 +131,7 @@ public partial class Map : TileMapLayer
{
_astar.SetPointSolid(FROM, false);
List<Vector2I> pathTaken = [.. _astar.GetIdPath(FROM, TO, true)];
List<Vector2I> pathTaken = [.. _astar.GetIdPath(FROM, TO)];
_astar.SetPointSolid(FROM, true);
if (!INCLUDE_FROM)
+1 -1
View File
@@ -63,7 +63,7 @@ public partial class PlayerController : TurnController
_towers[3].AddChild(newCommander);
}
public override async Task StartTurn()
public override void StartTurn()
{
_towers.ForEach(t => t.StartTurn());
}
+1 -1
View File
@@ -8,7 +8,7 @@ public partial class TurnController : Node2D
public delegate void TurnDoneEventHandler();
public PlayArea _playArea;
public virtual async Task StartTurn()
public virtual void StartTurn()
{
}