PathModifyHelper.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System.Collections.Generic;
  2. using PF;
  3. using UnityEngine;
  4. namespace ETModel
  5. {
  6. public static class PathModifyHelper
  7. {
  8. public static void StartEndModify(PF.ABPath abPath)
  9. {
  10. if (abPath.vectorPath.Count == 1)
  11. {
  12. abPath.vectorPath.Add(abPath.vectorPath[0]);
  13. }
  14. abPath.vectorPath[0] = abPath.startPoint;
  15. abPath.vectorPath[abPath.vectorPath.Count - 1] = abPath.endPoint;
  16. }
  17. public static void FunnelModify (Path p) {
  18. if (p.path == null || p.path.Count == 0 || p.vectorPath == null || p.vectorPath.Count == 0) {
  19. return;
  20. }
  21. List<Vector3> funnelPath = ListPool<Vector3>.Claim();
  22. // Split the path into different parts (separated by custom links)
  23. // and run the funnel algorithm on each of them in turn
  24. var parts = Funnel.SplitIntoParts(p);
  25. if (parts.Count == 0) {
  26. // As a really special case, it might happen that the path contained only a single node
  27. // and that node was part of a custom link (e.g added by the NodeLink2 component).
  28. // In that case the SplitIntoParts method will not know what to do with it because it is
  29. // neither a link (as only 1 of the 2 nodes of the link was part of the path) nor a normal
  30. // path part. So it will skip it. This will cause it to return an empty list.
  31. // In that case we want to simply keep the original path, which is just a single point.
  32. return;
  33. }
  34. for (int i = 0; i < parts.Count; i++) {
  35. var part = parts[i];
  36. if (!part.isLink) {
  37. var portals = Funnel.ConstructFunnelPortals(p.path, part);
  38. var result = Funnel.Calculate(portals, true, false);
  39. funnelPath.AddRange(result);
  40. ListPool<Vector3>.Release(ref portals.left);
  41. ListPool<Vector3>.Release(ref portals.right);
  42. ListPool<Vector3>.Release(ref result);
  43. } else {
  44. // non-link parts will add the start/end points for the adjacent parts.
  45. // So if there is no non-link part before this one, then we need to add the start point of the link
  46. // and if there is no non-link part after this one, then we need to add the end point.
  47. if (i == 0 || parts[i-1].isLink) {
  48. funnelPath.Add(part.startPoint);
  49. }
  50. if (i == parts.Count - 1 || parts[i+1].isLink) {
  51. funnelPath.Add(part.endPoint);
  52. }
  53. }
  54. }
  55. ListPool<Funnel.PathPart>.Release(ref parts);
  56. // Pool the previous vectorPath
  57. ListPool<Vector3>.Release(ref p.vectorPath);
  58. p.vectorPath = funnelPath;
  59. }
  60. }
  61. }