/Assets/Plugins/AstarPathfindingProject/Modifiers/StartEndModifier.cs

https://bitbucket.org/Werring/unity-indusim · C# · 171 lines · 88 code · 36 blank · 47 comment · 41 complexity · dd0c52c37f5a9155836e4a4812bd6be4 MD5 · raw file

  1. using UnityEngine;
  2. using System.Collections;
  3. using Pathfinding;
  4. [System.Serializable]
  5. /** Adjusts start and end points of a path.
  6. * \ingroup modifiers
  7. */
  8. public class StartEndModifier : Modifier {
  9. public override ModifierData input {
  10. get { return ModifierData.Vector; }
  11. }
  12. public override ModifierData output {
  13. get { return (addPoints ? ModifierData.None : ModifierData.StrictVectorPath) | ModifierData.VectorPath; }
  14. }
  15. /** Add points to the path instead of replacing. */
  16. public bool addPoints = false;
  17. public Exactness exactStartPoint = Exactness.Original;
  18. public Exactness exactEndPoint = Exactness.Original;
  19. /** Sets where the start and end points of a path should be placed */
  20. public enum Exactness {
  21. SnapToNode, /**< The point is snapped to the first/last node in the path*/
  22. Original, /**< The point is set to the exact point which was passed when calling the pathfinding */
  23. Interpolate, /**< The point is set to the closest point on the line between either the two first points or the two last points */
  24. ClosestOnNode /**< The point is set to the closest point on the node. Note that for many node types the "closest point" is the node's position which makes this identical to Exactness.SnapToNode */
  25. }
  26. public bool useRaycasting = false;
  27. public LayerMask mask = -1;
  28. public bool useGraphRaycasting = false;
  29. /*public override void ApplyOriginal (Path p) {
  30. if (exactStartPoint) {
  31. pStart = GetClampedPoint (p.path[0].position, p.originalStartPoint, p.path[0]);
  32. if (!addPoints) {
  33. p.startPoint = pStart;
  34. }
  35. }
  36. if (exactEndPoint) {
  37. pEnd = GetClampedPoint (p.path[p.path.Length-1].position, p.originalEndPoint, p.path[p.path.Length-1]);
  38. if (!addPoints) {
  39. p.endPoint = pEnd;
  40. }
  41. }
  42. }*/
  43. public override void Apply (Path _p, ModifierData source) {
  44. ABPath p = _p as ABPath;
  45. //Only for ABPaths
  46. if (p == null) return;
  47. if (p.vectorPath.Count == 0) {
  48. return;
  49. } else if (p.vectorPath.Count < 2 && !addPoints) {
  50. //Vector3[] arr = new Vector3[2];
  51. //arr[0] = p.vectorPath[0];
  52. //arr[1] = p.vectorPath[0];
  53. //p.vectorPath = arr;
  54. p.vectorPath.Add (p.vectorPath[0]);
  55. }
  56. //Debug.DrawRay (p.originalEndPoint,Vector3.up,Color.red);
  57. //Debug.DrawRay (p.startPoint,Vector3.up,Color.red);
  58. //Debug.DrawRay (p.endPoint,Vector3.up,Color.green);
  59. Vector3 pStart = Vector3.zero,
  60. pEnd = Vector3.zero;
  61. if (exactStartPoint == Exactness.Original) {
  62. pStart = GetClampedPoint ((Vector3)p.path[0].position, p.originalStartPoint, p.path[0]);
  63. } else if (exactStartPoint == Exactness.ClosestOnNode) {
  64. pStart = GetClampedPoint ((Vector3)p.path[0].position, p.startPoint, p.path[0]);
  65. } else if (exactStartPoint == Exactness.Interpolate) {
  66. pStart = GetClampedPoint ((Vector3)p.path[0].position, p.originalStartPoint, p.path[0]);
  67. pStart = Mathfx.NearestPointStrict ((Vector3)p.path[0].position,(Vector3)p.path[1>=p.path.Count?0:1].position,pStart);
  68. } else {
  69. pStart = (Vector3)p.path[0].position;
  70. }
  71. if (exactEndPoint == Exactness.Original) {
  72. pEnd = GetClampedPoint ((Vector3)p.path[p.path.Count-1].position, p.originalEndPoint, p.path[p.path.Count-1]);
  73. } else if (exactEndPoint == Exactness.ClosestOnNode) {
  74. pEnd = GetClampedPoint ((Vector3)p.path[p.path.Count-1].position, p.endPoint, p.path[p.path.Count-1]);
  75. } else if (exactEndPoint == Exactness.Interpolate) {
  76. pEnd = GetClampedPoint ((Vector3)p.path[p.path.Count-1].position, p.originalEndPoint, p.path[p.path.Count-1]);
  77. pEnd = Mathfx.NearestPointStrict ((Vector3)p.path[p.path.Count-1].position,(Vector3)p.path[p.path.Count-2<0?0:p.path.Count-2].position,pEnd);
  78. } else {
  79. pEnd = (Vector3)p.path[p.path.Count-1].position;
  80. }
  81. if (!addPoints) {
  82. //p.vectorPath[0] = p.startPoint;
  83. //p.vectorPath[p.vectorPath.Length-1] = p.endPoint;
  84. //Debug.DrawLine (p.vectorPath[0],pStart,Color.green);
  85. //Debug.DrawLine (p.vectorPath[p.vectorPath.Length-1],pEnd,Color.green);
  86. p.vectorPath[0] = pStart;
  87. p.vectorPath[p.vectorPath.Count-1] = pEnd;
  88. } else {
  89. //Vector3[] newPath = new Vector3[p.vectorPath.Length+(exactStartPoint != Exactness.SnapToNode ? 1 : 0) + (exactEndPoint != Exactness.SnapToNode ? 1 : 0)];
  90. if (exactStartPoint != Exactness.SnapToNode) {
  91. //newPath[0] = pStart;
  92. p.vectorPath.Insert (0,pStart);
  93. }
  94. if (exactEndPoint != Exactness.SnapToNode) {
  95. //newPath[newPath.Length-1] = pEnd;
  96. p.vectorPath.Add (pEnd);
  97. }
  98. /*int offset = exactStartPoint != Exactness.SnapToNode ? 1 : 0;
  99. for (int i=0;i<p.vectorPath.Length;i++) {
  100. newPath[i+offset] = p.vectorPath[i];
  101. }
  102. p.vectorPath = newPath;*/
  103. }
  104. }
  105. public Vector3 GetClampedPoint (Vector3 from, Vector3 to, Node hint) {
  106. //float minDistance = Mathf.Infinity;
  107. Vector3 minPoint = to;
  108. if (useRaycasting) {
  109. RaycastHit hit;
  110. if (Physics.Linecast (from,to,out hit,mask)) {
  111. minPoint = hit.point;
  112. //minDistance = hit.distance;
  113. }
  114. }
  115. if (useGraphRaycasting && hint != null) {
  116. NavGraph graph = AstarData.GetGraph (hint);
  117. if (graph != null) {
  118. IRaycastableGraph rayGraph = graph as IRaycastableGraph;
  119. if (rayGraph != null) {
  120. GraphHitInfo hit;
  121. if (rayGraph.Linecast (from,minPoint, hint, out hit)) {
  122. //if ((hit.point-from).magnitude < minDistance) {
  123. minPoint = hit.point;
  124. //}
  125. }
  126. }
  127. }
  128. }
  129. return minPoint;
  130. }
  131. }