FloodPathTracer.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using UnityEngine;
  2. namespace Pathfinding {
  3. /// <summary>
  4. /// Restrict suitable nodes by if they have been searched by a FloodPath.
  5. ///
  6. /// Suitable nodes are in addition to the basic contraints, only the nodes which return true on a FloodPath.HasPathTo (node) call.
  7. /// See: Pathfinding.FloodPath
  8. /// See: Pathfinding.FloodPathTracer
  9. /// </summary>
  10. public class FloodPathConstraint : NNConstraint {
  11. readonly FloodPath path;
  12. public FloodPathConstraint (FloodPath path) {
  13. if (path == null) { Debug.LogWarning("FloodPathConstraint should not be used with a NULL path"); }
  14. this.path = path;
  15. }
  16. public override bool Suitable (GraphNode node) {
  17. return base.Suitable(node) && path.HasPathTo(node);
  18. }
  19. }
  20. /// <summary>
  21. /// Traces a path created with the Pathfinding.FloodPath.
  22. ///
  23. /// See Pathfinding.FloodPath for examples on how to use this path type
  24. ///
  25. /// [Open online documentation to see images]
  26. /// </summary>
  27. public class FloodPathTracer : ABPath {
  28. /// <summary>Reference to the FloodPath which searched the path originally</summary>
  29. protected FloodPath flood;
  30. protected override bool hasEndPoint {
  31. get {
  32. return false;
  33. }
  34. }
  35. /// <summary>
  36. /// Default constructor.
  37. /// Do not use this. Instead use the static Construct method which can handle path pooling.
  38. /// </summary>
  39. public FloodPathTracer () {}
  40. public static FloodPathTracer Construct (Vector3 start, FloodPath flood, OnPathDelegate callback = null) {
  41. var p = PathPool.GetPath<FloodPathTracer>();
  42. p.Setup(start, flood, callback);
  43. return p;
  44. }
  45. protected void Setup (Vector3 start, FloodPath flood, OnPathDelegate callback) {
  46. this.flood = flood;
  47. if (flood == null || flood.PipelineState < PathState.Returned) {
  48. throw new System.ArgumentException("You must supply a calculated FloodPath to the 'flood' argument");
  49. }
  50. base.Setup(start, flood.originalStartPoint, callback);
  51. nnConstraint = new FloodPathConstraint(flood);
  52. }
  53. protected override void Reset () {
  54. base.Reset();
  55. flood = null;
  56. }
  57. /// <summary>
  58. /// Initializes the path.
  59. /// Traces the path from the start node.
  60. /// </summary>
  61. protected override void Initialize () {
  62. if (startNode != null && flood.HasPathTo(startNode)) {
  63. Trace(startNode);
  64. CompleteState = PathCompleteState.Complete;
  65. } else {
  66. FailWithError("Could not find valid start node");
  67. }
  68. }
  69. protected override void CalculateStep (long targetTick) {
  70. if (CompleteState != PathCompleteState.Complete) throw new System.Exception("Something went wrong. At this point the path should be completed");
  71. }
  72. /// <summary>
  73. /// Traces the calculated path from the start node to the end.
  74. /// This will build an array (<see cref="path)"/> of the nodes this path will pass through and also set the <see cref="vectorPath"/> array to the <see cref="path"/> arrays positions.
  75. /// This implementation will use the <see cref="flood"/> (FloodPath) to trace the path from precalculated data.
  76. /// </summary>
  77. public void Trace (GraphNode from) {
  78. GraphNode c = from;
  79. int count = 0;
  80. while (c != null) {
  81. path.Add(c);
  82. vectorPath.Add((Vector3)c.position);
  83. c = flood.GetParent(c);
  84. count++;
  85. if (count > 1024) {
  86. Debug.LogWarning("Inifinity loop? >1024 node path. Remove this message if you really have that long paths (FloodPathTracer.cs, Trace function)");
  87. break;
  88. }
  89. }
  90. }
  91. }
  92. }