PathfindingDebugStepVisual.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. ------------------- Code Monkey -------------------
  3. Thank you for downloading this package
  4. I hope you find it useful in your projects
  5. If you have any questions let me know
  6. Cheers!
  7. unitycodemonkey.com
  8. --------------------------------------------------
  9. */
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using CodeMonkey.Utils;
  14. using TMPro;
  15. using UnityEngine;
  16. public class PathfindingDebugStepVisual : MonoBehaviour {
  17. public static PathfindingDebugStepVisual Instance { get; private set; }
  18. [SerializeField] private Transform pfPathfindingDebugStepVisualNode;
  19. private List<Transform> visualNodeList;
  20. private List<GridSnapshotAction> gridSnapshotActionList;
  21. private bool autoShowSnapshots;
  22. private float autoShowSnapshotsTimer;
  23. private Transform[,] visualNodeArray;
  24. private void Awake() {
  25. Instance = this;
  26. visualNodeList = new List<Transform>();
  27. gridSnapshotActionList = new List<GridSnapshotAction>();
  28. }
  29. public void Setup(Grid<PathNode> grid) {
  30. visualNodeArray = new Transform[grid.GetWidth(), grid.GetHeight()];
  31. for (int x = 0; x < grid.GetWidth(); x++) {
  32. for (int y = 0; y < grid.GetHeight(); y++) {
  33. Vector3 gridPosition = new Vector3(x, y) * grid.GetCellSize() + Vector3.one * grid.GetCellSize() * .5f;
  34. Transform visualNode = CreateVisualNode(gridPosition);
  35. visualNodeArray[x, y] = visualNode;
  36. visualNodeList.Add(visualNode);
  37. }
  38. }
  39. HideNodeVisuals();
  40. }
  41. private void Update() {
  42. if (Input.GetKeyDown(KeyCode.Space)) {
  43. ShowNextSnapshot();
  44. }
  45. if (Input.GetKeyDown(KeyCode.Return)) {
  46. autoShowSnapshots = true;
  47. }
  48. if (autoShowSnapshots) {
  49. float autoShowSnapshotsTimerMax = .05f;
  50. autoShowSnapshotsTimer -= Time.deltaTime;
  51. if (autoShowSnapshotsTimer <= 0f) {
  52. autoShowSnapshotsTimer += autoShowSnapshotsTimerMax;
  53. ShowNextSnapshot();
  54. if (gridSnapshotActionList.Count == 0) {
  55. autoShowSnapshots = false;
  56. }
  57. }
  58. }
  59. }
  60. private void ShowNextSnapshot() {
  61. if (gridSnapshotActionList.Count > 0) {
  62. GridSnapshotAction gridSnapshotAction = gridSnapshotActionList[0];
  63. gridSnapshotActionList.RemoveAt(0);
  64. gridSnapshotAction.TriggerAction();
  65. }
  66. }
  67. public void ClearSnapshots() {
  68. gridSnapshotActionList.Clear();
  69. }
  70. public void TakeSnapshot(Grid<PathNode> grid, PathNode current, List<PathNode> openList, List<PathNode> closedList) {
  71. GridSnapshotAction gridSnapshotAction = new GridSnapshotAction();
  72. gridSnapshotAction.AddAction(HideNodeVisuals);
  73. for (int x = 0; x < grid.GetWidth(); x++) {
  74. for (int y = 0; y < grid.GetHeight(); y++) {
  75. PathNode pathNode = grid.GetGridObject(x, y);
  76. int gCost = pathNode.gCost;
  77. int hCost = pathNode.hCost;
  78. int fCost = pathNode.fCost;
  79. Vector3 gridPosition = new Vector3(pathNode.x, pathNode.z) * grid.GetCellSize() + Vector3.one * grid.GetCellSize() * .5f;
  80. bool isCurrent = pathNode == current;
  81. bool isInOpenList = openList.Contains(pathNode);
  82. bool isInClosedList = closedList.Contains(pathNode);
  83. int tmpX = x;
  84. int tmpY = y;
  85. gridSnapshotAction.AddAction(() => {
  86. Transform visualNode = visualNodeArray[tmpX, tmpY];
  87. SetupVisualNode(visualNode, gCost, hCost, fCost);
  88. Color backgroundColor = UtilsClass.GetColorFromString("636363");
  89. if (isInClosedList) {
  90. backgroundColor = new Color(1, 0, 0);
  91. }
  92. if (isInOpenList) {
  93. backgroundColor = UtilsClass.GetColorFromString("009AFF");
  94. }
  95. if (isCurrent) {
  96. backgroundColor = new Color(0, 1, 0);
  97. }
  98. visualNode.Find("sprite").GetComponent<SpriteRenderer>().color = backgroundColor;
  99. });
  100. }
  101. }
  102. gridSnapshotActionList.Add(gridSnapshotAction);
  103. }
  104. public void TakeSnapshotFinalPath(Grid<PathNode> grid, List<PathNode> path) {
  105. GridSnapshotAction gridSnapshotAction = new GridSnapshotAction();
  106. gridSnapshotAction.AddAction(HideNodeVisuals);
  107. for (int x = 0; x < grid.GetWidth(); x++) {
  108. for (int y = 0; y < grid.GetHeight(); y++) {
  109. PathNode pathNode = grid.GetGridObject(x, y);
  110. int gCost = pathNode.gCost;
  111. int hCost = pathNode.hCost;
  112. int fCost = pathNode.fCost;
  113. Vector3 gridPosition = new Vector3(pathNode.x, pathNode.z) * grid.GetCellSize() + Vector3.one * grid.GetCellSize() * .5f;
  114. bool isInPath = path.Contains(pathNode);
  115. int tmpX = x;
  116. int tmpY = y;
  117. gridSnapshotAction.AddAction(() => {
  118. Transform visualNode = visualNodeArray[tmpX, tmpY];
  119. SetupVisualNode(visualNode, gCost, hCost, fCost);
  120. Color backgroundColor;
  121. if (isInPath) {
  122. backgroundColor = new Color(0, 1, 0);
  123. } else {
  124. backgroundColor = UtilsClass.GetColorFromString("636363");
  125. }
  126. visualNode.Find("sprite").GetComponent<SpriteRenderer>().color = backgroundColor;
  127. });
  128. }
  129. }
  130. gridSnapshotActionList.Add(gridSnapshotAction);
  131. }
  132. private void HideNodeVisuals() {
  133. foreach (Transform visualNodeTransform in visualNodeList) {
  134. SetupVisualNode(visualNodeTransform, 9999, 9999, 9999);
  135. }
  136. }
  137. private Transform CreateVisualNode(Vector3 position) {
  138. Transform visualNodeTransform = Instantiate(pfPathfindingDebugStepVisualNode, position, Quaternion.identity);
  139. return visualNodeTransform;
  140. }
  141. private void SetupVisualNode(Transform visualNodeTransform, int gCost, int hCost, int fCost) {
  142. if (fCost < 1000) {
  143. visualNodeTransform.Find("gCostText").GetComponent<TextMeshPro>().SetText(gCost.ToString());
  144. visualNodeTransform.Find("hCostText").GetComponent<TextMeshPro>().SetText(hCost.ToString());
  145. visualNodeTransform.Find("fCostText").GetComponent<TextMeshPro>().SetText(fCost.ToString());
  146. } else {
  147. visualNodeTransform.Find("gCostText").GetComponent<TextMeshPro>().SetText("");
  148. visualNodeTransform.Find("hCostText").GetComponent<TextMeshPro>().SetText("");
  149. visualNodeTransform.Find("fCostText").GetComponent<TextMeshPro>().SetText("");
  150. }
  151. }
  152. private class GridSnapshotAction {
  153. private Action action;
  154. public GridSnapshotAction() {
  155. action = () => { };
  156. }
  157. public void AddAction(Action action) {
  158. this.action += action;
  159. }
  160. public void TriggerAction() {
  161. action();
  162. }
  163. }
  164. }