/* ------------------- Code Monkey ------------------- Thank you for downloading this package I hope you find it useful in your projects If you have any questions let me know Cheers! unitycodemonkey.com -------------------------------------------------- */ using System; using System.Collections; using System.Collections.Generic; using CodeMonkey.Utils; using TMPro; using UnityEngine; public class PathfindingDebugStepVisual : MonoBehaviour { public static PathfindingDebugStepVisual Instance { get; private set; } [SerializeField] private Transform pfPathfindingDebugStepVisualNode; private List visualNodeList; private List gridSnapshotActionList; private bool autoShowSnapshots; private float autoShowSnapshotsTimer; private Transform[,] visualNodeArray; private void Awake() { Instance = this; visualNodeList = new List(); gridSnapshotActionList = new List(); } public void Setup(Grid grid) { visualNodeArray = new Transform[grid.GetWidth(), grid.GetHeight()]; for (int x = 0; x < grid.GetWidth(); x++) { for (int y = 0; y < grid.GetHeight(); y++) { Vector3 gridPosition = new Vector3(x, y) * grid.GetCellSize() + Vector3.one * grid.GetCellSize() * .5f; Transform visualNode = CreateVisualNode(gridPosition); visualNodeArray[x, y] = visualNode; visualNodeList.Add(visualNode); } } HideNodeVisuals(); } private void Update() { if (Input.GetKeyDown(KeyCode.Space)) { ShowNextSnapshot(); } if (Input.GetKeyDown(KeyCode.Return)) { autoShowSnapshots = true; } if (autoShowSnapshots) { float autoShowSnapshotsTimerMax = .05f; autoShowSnapshotsTimer -= Time.deltaTime; if (autoShowSnapshotsTimer <= 0f) { autoShowSnapshotsTimer += autoShowSnapshotsTimerMax; ShowNextSnapshot(); if (gridSnapshotActionList.Count == 0) { autoShowSnapshots = false; } } } } private void ShowNextSnapshot() { if (gridSnapshotActionList.Count > 0) { GridSnapshotAction gridSnapshotAction = gridSnapshotActionList[0]; gridSnapshotActionList.RemoveAt(0); gridSnapshotAction.TriggerAction(); } } public void ClearSnapshots() { gridSnapshotActionList.Clear(); } public void TakeSnapshot(Grid grid, PathNode current, List openList, List closedList) { GridSnapshotAction gridSnapshotAction = new GridSnapshotAction(); gridSnapshotAction.AddAction(HideNodeVisuals); for (int x = 0; x < grid.GetWidth(); x++) { for (int y = 0; y < grid.GetHeight(); y++) { PathNode pathNode = grid.GetGridObject(x, y); int gCost = pathNode.gCost; int hCost = pathNode.hCost; int fCost = pathNode.fCost; Vector3 gridPosition = new Vector3(pathNode.x, pathNode.z) * grid.GetCellSize() + Vector3.one * grid.GetCellSize() * .5f; bool isCurrent = pathNode == current; bool isInOpenList = openList.Contains(pathNode); bool isInClosedList = closedList.Contains(pathNode); int tmpX = x; int tmpY = y; gridSnapshotAction.AddAction(() => { Transform visualNode = visualNodeArray[tmpX, tmpY]; SetupVisualNode(visualNode, gCost, hCost, fCost); Color backgroundColor = UtilsClass.GetColorFromString("636363"); if (isInClosedList) { backgroundColor = new Color(1, 0, 0); } if (isInOpenList) { backgroundColor = UtilsClass.GetColorFromString("009AFF"); } if (isCurrent) { backgroundColor = new Color(0, 1, 0); } visualNode.Find("sprite").GetComponent().color = backgroundColor; }); } } gridSnapshotActionList.Add(gridSnapshotAction); } public void TakeSnapshotFinalPath(Grid grid, List path) { GridSnapshotAction gridSnapshotAction = new GridSnapshotAction(); gridSnapshotAction.AddAction(HideNodeVisuals); for (int x = 0; x < grid.GetWidth(); x++) { for (int y = 0; y < grid.GetHeight(); y++) { PathNode pathNode = grid.GetGridObject(x, y); int gCost = pathNode.gCost; int hCost = pathNode.hCost; int fCost = pathNode.fCost; Vector3 gridPosition = new Vector3(pathNode.x, pathNode.z) * grid.GetCellSize() + Vector3.one * grid.GetCellSize() * .5f; bool isInPath = path.Contains(pathNode); int tmpX = x; int tmpY = y; gridSnapshotAction.AddAction(() => { Transform visualNode = visualNodeArray[tmpX, tmpY]; SetupVisualNode(visualNode, gCost, hCost, fCost); Color backgroundColor; if (isInPath) { backgroundColor = new Color(0, 1, 0); } else { backgroundColor = UtilsClass.GetColorFromString("636363"); } visualNode.Find("sprite").GetComponent().color = backgroundColor; }); } } gridSnapshotActionList.Add(gridSnapshotAction); } private void HideNodeVisuals() { foreach (Transform visualNodeTransform in visualNodeList) { SetupVisualNode(visualNodeTransform, 9999, 9999, 9999); } } private Transform CreateVisualNode(Vector3 position) { Transform visualNodeTransform = Instantiate(pfPathfindingDebugStepVisualNode, position, Quaternion.identity); return visualNodeTransform; } private void SetupVisualNode(Transform visualNodeTransform, int gCost, int hCost, int fCost) { if (fCost < 1000) { visualNodeTransform.Find("gCostText").GetComponent().SetText(gCost.ToString()); visualNodeTransform.Find("hCostText").GetComponent().SetText(hCost.ToString()); visualNodeTransform.Find("fCostText").GetComponent().SetText(fCost.ToString()); } else { visualNodeTransform.Find("gCostText").GetComponent().SetText(""); visualNodeTransform.Find("hCostText").GetComponent().SetText(""); visualNodeTransform.Find("fCostText").GetComponent().SetText(""); } } private class GridSnapshotAction { private Action action; public GridSnapshotAction() { action = () => { }; } public void AddAction(Action action) { this.action += action; } public void TriggerAction() { action(); } } }