Я пытаюсь найти диапазон между блоком и узлами в сетке, чтобы увидеть, находятся ли они в пределах диапазона блока.
Здесь я прохожу oop через все единицы и нарисую путь между ними и всеми узлами в квадрате вокруг него, чтобы увидеть, находятся ли они в трех разных диапазонах.
public IEnumerator UnitLoop()
{
//Loop through all units
foreach(GameObject obj in units)
{
UpdateUnits(obj);
yield return new WaitForEndOfFrame();
}
}
public void UpdateUnits(GameObject obj)
{
int maxRange = 0;
Unit unit = obj.GetComponent<Unit>();
//If supportRange is larger than attackRange maxRange will be set to it and vice versa
if(unit.supportRange > unit.attackRange)
{
maxRange = unit.supportRange;
}
else
{
maxRange = unit.attackRange;
}
//grid.grid is a two-dimensional array of nodes
//Node is a class
//Gets a node to start at with a distance of maxRange + movementRange in both the x-axis and the y-axis from the unit
Node startNode = grid.grid[unit.gridX - (maxRange + unit.movementRange), unit.gridY - (maxRange + unit.movementRange)];
//Looks at all nodes in a square around the unit, no node that can be reached will be outside this square
for(int x = 0; x < (unit.movementRange + maxRange) * 2 + 1; x++)
{
for(int y = 0; y < (unit.movementRange + maxRange) * 2 + 1; y++)
{
//Adds the ranges to a list
List<int> ranges = new List<int>();
ranges.Add(unit.movementRange);
ranges.Add(unit.movementRange + unit.attackRange);
ranges.Add(unit.movementRange + unit.supportRange);
//pathfinding.FindPath is pathfinding using a*
//Right now it returns a list of bools with the same size as the list ranges
List<bool> paths = pathfinding.FindPath(unit.transform, grid.grid[startNode.gridX + x, startNode.gridY + y].worldPosition, false, ranges, false);
//Here it checks if the path can reach the node with the corresponding range and adds the node to a list on the unit if it can
if(paths[0])
unit.nodesInMovementRange.Add(grid.grid[startNode.gridX + x, startNode.gridY + y]);
if(paths[1])
unit.nodesInAttackRange.Add(grid.grid[startNode.gridX + x, startNode.gridY + y]);
if(paths[2])
unit.nodesInSupportRange.Add(grid.grid[startNode.gridX + x, startNode.gridY + y]);
}
}
}
Это работает, но есть серьезная проблема оптимизации. Я использую Unity 2019.3.0f3
Буду очень признателен за любую предоставленную помощь, спасибо.