diff --git a/Enemy.cs b/Enemy.cs index 46d0c5c..f2c077a 100644 --- a/Enemy.cs +++ b/Enemy.cs @@ -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 _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 _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 GetGoals(Map MAP) { - List 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 GetBestPath(Map MAP) @@ -89,13 +95,13 @@ public partial class Enemy : StaticBody2D { paths.Add(MAP.GetPath(_address, goals[i])); } - List bestPath = paths.OrderBy(p => p.Count).ThenBy(p => p.Last().X % 2).ToList()[0]; + List 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) diff --git a/EnemyController.cs b/EnemyController.cs index 54c1025..e2d1a6c 100644 --- a/EnemyController.cs +++ b/EnemyController.cs @@ -11,7 +11,7 @@ public partial class EnemyController : TurnController public PackedScene _enemyScene = GD.Load("res://Enemy.tscn"); public List _enemies = new(); public PlayerController _playerController; - public Dictionary> _tweenStages = new(); + public Dictionary> _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 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 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 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 stageTweens = _tweenStages[stage]; - _tween.Chain().TweenSubtween(stageTweens[0]); - for (int j = 1; j < stageTweens.Count; j++) + _tween.Chain().TweenInterval(0.5f); + List 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) diff --git a/Main.cs b/Main.cs index c6f521a..f35f15a 100644 --- a/Main.cs +++ b/Main.cs @@ -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) diff --git a/Map.cs b/Map.cs index 51e252c..0a51a3a 100644 --- a/Map.cs +++ b/Map.cs @@ -13,7 +13,20 @@ public partial class Map : TileMapLayer public List _cells = new(), _leftmostColumn = new(), _rightmostColumn = new(), _topRow = new(), _bottomRow = new(); public AStarGrid2D _astar = new(); public Dictionary _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 pathTaken = [.. _astar.GetIdPath(FROM, TO, true)]; + List pathTaken = [.. _astar.GetIdPath(FROM, TO)]; _astar.SetPointSolid(FROM, true); if (!INCLUDE_FROM) diff --git a/PlayerController.cs b/PlayerController.cs index 8f1f636..caa6ff3 100644 --- a/PlayerController.cs +++ b/PlayerController.cs @@ -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()); } diff --git a/TurnController.cs b/TurnController.cs index 9adc554..df1713d 100644 --- a/TurnController.cs +++ b/TurnController.cs @@ -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() { }