I’ve been trying to have multiple enemy bases so that more than one stream of enemies can try to attack the friendly base but have been having trouble accomplishing this. What happens is that when I have multiple pathfinders, one of the bases pulls data from the other base and they go on the same path. Is it possible to have more than one pathfinder the way that it is setup right now?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PathFinder : MonoBehaviour
{
Dictionary<Vector2Int, CheckPoint> grid;
Queue<CheckPoint> checkPointQueue = new Queue<CheckPoint>();
public List<CheckPoint> path = new List<CheckPoint>();
[SerializeField] CheckPoint start;
[SerializeField] CheckPoint end;
bool queueIsRunning = true;
CheckPoint searchBase;
Vector2Int[] directions = {
Vector2Int.up,
Vector2Int.down,
Vector2Int.right,
Vector2Int.left
};
// Start is called before the first frame update
void Start()
{
grid = FindObjectOfType<GridGetter>().ReturnGrid();
}
public void MakePath()
{
path.Add(end);
end.isUsable = false;
while (path[path.Count - 1] != start)
{
CheckPoint nextPoint = path[path.Count - 1].exploredFrom;
nextPoint.isUsable = false;
path.Add(nextPoint);
}
path.Reverse();
}
public List<CheckPoint> GetPath()
{
if(path.Count == 0)
{
BreadthFirstSearch();
MakePath();
}
return (path);
}
public void BreadthFirstSearch()
{
checkPointQueue.Enqueue(start);
while (checkPointQueue.Count > 0 && queueIsRunning)
{
searchBase = checkPointQueue.Dequeue();//dequeue deletes first thing from queue
CheckIfEndFound();
FindNeighbors();
searchBase.isExplored = true;
}
}
private void CheckIfEndFound()
{
if (searchBase == end)
{
print("Found end.");
queueIsRunning = false;
}
}
private void FindNeighbors()
{
if (!queueIsRunning) { return; }
foreach (Vector2Int direction in directions)
{
Vector2Int neighborCoords = searchBase.GetSnapLocation() + direction;
if (grid.ContainsKey(neighborCoords))
{
QueueNeighbor(neighborCoords);
}
}
}
private void QueueNeighbor(Vector2Int neighborCoords)
{
CheckPoint neighborBlock = grid[neighborCoords];
if (neighborBlock.isExplored || checkPointQueue.Contains(neighborBlock))
{
}
else {
checkPointQueue.Enqueue(neighborBlock);
neighborBlock.exploredFrom = searchBase;
}
}
}
That is my pathfinder script^^^^, how can I make it so both bases don’t pull the same data off the pathfinder. I’ve even gone as far to create another script that counts how many enemyspawners there are and makes that amount of pathfinders and sets a pathfinder to an enemy spawner, however that seems very complicated.