I think my pathfinding script is returning the path with only the endpoint in it, so I get an error…
there’s 15 blocks in the scene but the list looks like this
here are the scripts mentioned …
PathFinder.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PathFinder : MonoBehaviour
{
// SHIPPING DATA //
Vector2Int[] pathDirections = { Vector2Int.up, Vector2Int.right, Vector2Int.down, Vector2Int.left };
// CHANGING DATA //
[SerializeField] WaypointBlock startWaypointBlock;
[SerializeField] WaypointBlock endWaypointBlock;
[SerializeField] Dictionary<Vector2Int, WaypointBlock> grid = new Dictionary<Vector2Int, WaypointBlock>();
[SerializeField] List<Transform> childList = new List<Transform>();
[SerializeField] List<Transform> blocksToRemove = new List<Transform>();
Queue<WaypointBlock> queue = new Queue<WaypointBlock>();
[SerializeField] List<WaypointBlock> path = new List<WaypointBlock>(); // TODO make private
[Header("DO NOT CHANGE - exposed for debugging")]
[SerializeField] bool isRunning = true; // TODO maybe hide to protect
// * CODE * //
// Start is called before the first frame update
void Start()
{
BuildChildList();
BuildGridDictionary();
RemoveDuplicateBlocksFromChildList();
// TODO i want the child list to be sorted alphanumerically so that we
// could use the child list to find the first and last blocks by grid
// (and reference those in the dictionary)
// and make those the start and goal automatically
ColorStartAndGoalBlocks();
}
public List<WaypointBlock> GetPath()
{
BreadthFirstSearch();
CreatePath();
return path;
}
private void CreatePath()
{
path.Add(endWaypointBlock);
WaypointBlock previous = endWaypointBlock.queuedFrom;
while (previous != startWaypointBlock)
{
path.Add(previous);
previous = previous.queuedFrom;
}
path.Add(startWaypointBlock);
path.Reverse();
}
private void BreadthFirstSearch()
{
queue.Enqueue(startWaypointBlock);
while(queue.Count > 0 && isRunning)
{
WaypointBlock searchCenter = queue.Dequeue();
print("Searching from " + searchCenter); // TODO remove later
HaltIfEndFoundFrom(searchCenter);
ExploreNeighbors(searchCenter);
searchCenter.isExplored = true;
}
print("Fishined Searching");
}
private void HaltIfEndFoundFrom(WaypointBlock searchCenter)
{
if (searchCenter == endWaypointBlock)
{
print("searching from end point, stopping search"); // TODO remove later
isRunning = false;
ColorStartAndGoalBlocks();
}
}
private void ExploreNeighbors(WaypointBlock from)
{
if (!isRunning) { return; }
int i = 0;
int x = from.GetGridPos().x;
int y = from.GetGridPos().y;
foreach (Vector2Int direction in pathDirections)
{
Vector2Int nextDirection = new Vector2Int(x + pathDirections[i].x, y + pathDirections[i].y);
print("Exploring " + nextDirection + " from " + from);
if (grid.ContainsKey(nextDirection))
{
QueueNewNeighbor(nextDirection, from);
}
else
{
print(nextDirection + " does not exist in the current grid");
}
i++;
}
}
private void QueueNewNeighbor(Vector2Int nextDirection, WaypointBlock from)
{
if (grid[nextDirection].isExplored || queue.Contains(grid[nextDirection]))
{
print(grid[nextDirection] + " has been explored alreayd");
}
else
{
grid[nextDirection].SetTopColor(Color.blue); // todo move later?
queue.Enqueue(grid[nextDirection]);
grid[nextDirection].queuedFrom = from;
print("Queueing " + grid[nextDirection] + " from " + from);
}
}
private void ColorStartAndGoalBlocks()
{
startWaypointBlock.SetTopColor(Color.green);
endWaypointBlock.SetTopColor(Color.red);
}
private void RemoveDuplicateBlocksFromChildList()
{
foreach (Transform duplicate in blocksToRemove)
{
childList.Remove(duplicate);
Debug.LogWarning("removed " + duplicate + " from original childlist");
}
}
private void BuildGridDictionary()
{
foreach (Transform gridBlock in childList)
{
WaypointBlock waypointBlock = gridBlock.GetComponent<WaypointBlock>();
bool duplicateWaypoint = grid.ContainsKey(waypointBlock.GetGridPos());
if (duplicateWaypoint)
{
CheckIfDuplicateIsImportant(waypointBlock);
//Debug.LogWarning("Duplicate block " + waypointBlock);
waypointBlock.gameObject.SetActive(false);
blocksToRemove.Add(gridBlock);
//Debug.LogWarning("added " + gridBlock + " to remove list");
}
else
{
grid.Add(waypointBlock.GetGridPos(), waypointBlock);
}
//print(grid);
//print(grid.Count);
}
}
private void CheckIfDuplicateIsImportant(WaypointBlock waypointBlock)
{
if (waypointBlock == startWaypointBlock)
{
startWaypointBlock = grid[waypointBlock.GetGridPos()];
}
if (waypointBlock == endWaypointBlock)
{
endWaypointBlock = grid[waypointBlock.GetGridPos()];
}
}
private void BuildChildList()
{
foreach (Transform child in transform)
{
childList.Add(child);
}
}
}
and EnemyMovement.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMovement : MonoBehaviour
{
// SHIPPING DATA //
// CHANGING DATA //
[SerializeField] PathFinder myGrid;
// * CODE * //
// Start is called before the first frame update
void Start()
{
//myGrid = GetComponent<PathFinder>();
List<WaypointBlock> path = myGrid.GetPath();
StartCoroutine(FollowPath(path));
}
IEnumerator FollowPath(List<WaypointBlock> path)
{
print("Starting Patrol...");
foreach (WaypointBlock cube in path)
{
transform.position = cube.transform.position;
print("Visiting Block: " + cube.name);
yield return new WaitForSeconds(1f);
}
print("Ending Patrol...");
}
}