still working on movement, for some reason some cells do not get set as solid that's why multiple can go on a cell

This commit is contained in:
2026-06-09 03:15:35 -04:00
parent 813ca5f633
commit 7a10f4a9fc
10 changed files with 288 additions and 326 deletions
+102 -62
View File
@@ -1,6 +1,7 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
public partial class EnemyController : TurnController
@@ -46,50 +47,110 @@ public partial class EnemyController : TurnController
}
public Vector2I GetBestGoal(Enemy ENEMY, Vector2I GOAL)
{
Vector2I goal = GOAL;
while (_playArea._map.IsRowFull(goal.Y))
{
goal.Y++;
}
int loop = 0;
while (_playArea._map.HasOccupant(goal))
{
Vector2I leftGoal = new (Math.Clamp(ENEMY._address.X - loop / 2 - 1, _playArea._map._minX, _playArea._map._maxX), goal.Y);
Vector2I rightGoal = new (Math.Clamp(ENEMY._address.X + loop / 2 + 1, _playArea._map._minX, _playArea._map._maxX), goal.Y);
List<Vector2I> leftPath = _playArea._map.GetPath(ENEMY._address, leftGoal);
List<Vector2I> rightPath = _playArea._map.GetPath(ENEMY._address, rightGoal);
if (rightPath.Count < leftPath.Count && !_playArea._map.HasOccupant(rightGoal))
{
goal = rightGoal;
}
else if (leftPath.Count < rightPath.Count && !_playArea._map.HasOccupant(leftGoal))
{
goal = leftGoal;
}
else if (!_playArea._map.HasOccupant(rightGoal))
{
goal = rightGoal;
}
else if (!_playArea._map.HasOccupant(leftGoal))
{
goal = leftGoal;
}
loop++;
}
return goal;
}
public void Initiate()
{
List<Vector2I> positions = [.. _playArea.GetNode<TileMapLayer>("InitialPositions").GetUsedCells()];
AddEnemies(positions);
// for (int i = 0; i < _playArea._map._cells.Count; i++)
// {
// if (_playArea._map._astar.IsPointSolid(_playArea._map._cells[i]))
// {
// GD.Print(_playArea._map._cells[i]);
// }
// }
}
public void MoveEnemies()
{
Tween tween = CreateTween();
tween.SetParallel();
for (int i = 0; i < _enemies.Count; i++)
{
Enemy enemy = _enemies[i];
ShiftGoal(enemy, new(enemy._address.X, Math.Max(enemy._address.Y - enemy._visibilityRange, _playArea._map._minY)));
enemy._speedRemaining = enemy._address.Y <= _playArea._map._minY ? 0 : enemy._speed;
}
_enemies = [.. _enemies.OrderByDescending(e => e._path.Count).ThenBy(e => e._address.Y).ThenBy(e => Math.Abs(e._address.X))];
int maxSpeed = _enemies.Max(e => e._speedRemaining);
List<Enemy> remainingEnemies = [.. _enemies];
for (int i = 0; i < maxSpeed; i++)
{
for (int j = 0; j < remainingEnemies.Count; j++)
if (enemy._speedRemaining > 0)
{
Enemy enemy = remainingEnemies[j];
int pathIndex = enemy._speed - enemy._speedRemaining;
enemy._path.InsertRange(0, [.. Enumerable.Repeat(enemy._path[0], pathIndex)]);
Vector2I cell = enemy._path[pathIndex];
Enemy obstacle = _playArea._map.GetOccupant(cell);
Vector2I goal = GetBestGoal(enemy, new(enemy._address.X, Math.Max(enemy._address.Y - enemy._visibilityRange, _playArea._map._minY)));
enemy._path = _playArea._map.GetPath(enemy._address, goal);
}
}
List<Enemy> remainingEnemies = SortEnemies(_enemies);
_enemies.ForEach(e => e._path.Clear());
if (remainingEnemies.Count == 0)
{
return;
}
while (remainingEnemies.Count > 0)
{
remainingEnemies = SortEnemies(remainingEnemies);
for (int i = 0; i < remainingEnemies.Count; i++)
{
Enemy enemy = remainingEnemies[i];
if (enemy._speedRemaining <= 0)
{
continue;
}
if (enemy._address.Y <= _playArea._map._minY)
{
continue;
}
Vector2I goal = GetBestGoal(enemy, new(enemy._address.X, Math.Max(enemy._address.Y - enemy._visibilityRange, _playArea._map._minY)));
List<Vector2I> path = _playArea._map.GetPath(enemy._address, goal);
Vector2I cell = path[0];
if (enemy._track)
{
GD.Print(cell, _playArea._map._astar.IsPointSolid(cell));
}
_playArea._map.SetCellEnemy(cell, enemy);
enemy._path.Add(cell);
enemy._speedRemaining--;
}
}
Tween tween = CreateTween();
tween.SetParallel();
List<Enemy> movingEnemies = [.. _enemies.Where(e => e._path.Count > 0)];
int maxSteps = movingEnemies.Max(e => e._path.Count);
for (int i = 0; i < maxSteps; i++)
{
for (int j = 0; j < movingEnemies.Count; j++)
{
Enemy mEnemy = movingEnemies[j];
if (mEnemy._path.Count <= i)
{
continue;
}
if (j == 0)
{
tween.Chain();
@@ -98,25 +159,12 @@ public partial class EnemyController : TurnController
{
tween.Parallel();
}
tween.TweenProperty(enemy, "global_position", _playArea._map.GetCellPositionFromAddress(enemy._address), .2f);
if (enemy._address.Y <= _playArea._map._minY)
{
remainingEnemies.Remove(enemy);
continue;
}
enemy._speedRemaining--;
if (enemy._speedRemaining <= 0)
{
remainingEnemies.Remove(enemy);
continue;
}
ShiftGoal(enemy, new(enemy._address.X, Math.Max(enemy._address.Y - enemy._visibilityRange, _playArea._map._minY)));
tween.TweenProperty(mEnemy, "global_position", _playArea._map.GetCellPositionFromAddress(mEnemy._path[i]), .25f);
}
}
tween.TweenCallback(Callable.From(() => EmitSignal(SignalName.TurnDone)));
_playArea._map.HighlightCells();
}
public void RemoveEnemy(Enemy ENEMY_TO_REMOVE)
@@ -126,6 +174,11 @@ public partial class EnemyController : TurnController
ENEMY_TO_REMOVE.QueueFree();
}
public List<Enemy> SortEnemies(List<Enemy> ENEMIES)
{
return [.. ENEMIES.Where(e => e._speedRemaining > 0 && _playArea._map.GetPath(e._address, new Vector2I(e._address.X, Math.Max(e._address.Y - e._visibilityRange, _playArea._map._minY))).Count > 0).OrderByDescending(e => e._path.Count).ThenBy(e => e._address.Y).ThenBy(e => Math.Abs(e._address.X - _playArea._map._maxX / 2))];
}
public override void StartTurn()
{
AddEnemies(1);
@@ -136,25 +189,12 @@ public partial class EnemyController : TurnController
public void SetEnemy(Enemy ENEMY, Vector2I CELL)
{
if (CELL == new Vector2I(8, 13))
{
ENEMY._track = true;
}
_playArea._map.SetCellEnemy(CELL, ENEMY);
ENEMY.GlobalPosition = _playArea._map.GetCellPositionFromAddress(CELL);
}
public void ShiftGoal(Enemy ENEMY, Vector2I GOAL)
{
while(_playArea._map.IsRowFull(GOAL.Y))
{
GOAL.Y++;
}
int loop = 0;
while (_playArea._map.GetOccupant(GOAL) != null)
{
GD.Print(ENEMY._address, "-->", GOAL);
GOAL.X = Math.Clamp(ENEMY._address.X + ((int)MathF.Floor(loop / 2) + 1) * (loop % 2 == 0 ? 1 : -1), _playArea._map._minX, _playArea._map._maxX);
loop++;
}
ENEMY._path = _playArea._map.GetPath(ENEMY._address, GOAL);
}
}