RecastPathProcessor.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace ET
  4. {
  5. public class RecastPathDestorySystem : DestroySystem<RecastPath>
  6. {
  7. public override void Destroy(RecastPath self)
  8. {
  9. self.Clear();
  10. }
  11. }
  12. public class RecastPath: Entity
  13. {
  14. public Vector3 StartPos = Vector3.zero;
  15. public Vector3 EndPos = Vector3.zero;
  16. public List<Vector3> Results = new List<Vector3>();
  17. public void Clear()
  18. {
  19. StartPos = Vector3.zero;
  20. EndPos = Vector3.zero;
  21. Results.Clear();
  22. }
  23. }
  24. /// <summary>
  25. /// 寻路处理者
  26. /// </summary>
  27. public class RecastPathProcessor: Entity
  28. {
  29. /// <summary>
  30. /// 归属的地图Id
  31. /// </summary>
  32. public int MapId;
  33. public void CalculatePath(RecastPath recastPath)
  34. {
  35. if (RecastInterface.FindPath(this.MapId, recastPath.StartPos, recastPath.EndPos))
  36. {
  37. RecastInterface.Smooth(this.MapId, 2f, 0.5f);
  38. {
  39. int smoothCount = 0;
  40. float[] smooths = RecastInterface.GetPathSmooth(this.MapId, out smoothCount);
  41. for (int i = 0; i < smoothCount; ++i)
  42. {
  43. Vector3 node = new Vector3(smooths[i * 3], smooths[i * 3 + 1], smooths[i * 3 + 2]);
  44. recastPath.Results.Add(node);
  45. }
  46. }
  47. }
  48. }
  49. public void Clear()
  50. {
  51. MapId = 0;
  52. }
  53. // #region Benchmark
  54. //
  55. // public static void BenchmarkSample()
  56. // {
  57. // BenchmarkHelper.Profile("寻路100000次", BenchmarkRecast, 100);
  58. // }
  59. //
  60. // private static void BenchmarkRecast()
  61. // {
  62. // if (RecastInterface.FindPath(100,
  63. // new System.Numerics.Vector3(-RandomHelper.RandomNumber(2, 50) - RandomHelper.RandFloat(),
  64. // RandomHelper.RandomNumber(-1, 5) + RandomHelper.RandFloat(), RandomHelper.RandomNumber(3, 20) + RandomHelper.RandFloat()),
  65. // new System.Numerics.Vector3(-RandomHelper.RandomNumber(2, 50) - RandomHelper.RandFloat(),
  66. // RandomHelper.RandomNumber(-1, 5) + RandomHelper.RandFloat(), RandomHelper.RandomNumber(3, 20) + RandomHelper.RandFloat())))
  67. // {
  68. // RecastInterface.Smooth(100, 2f, 0.5f);
  69. // {
  70. // int smoothCount = 0;
  71. // float[] smooths = RecastInterface.GetPathSmooth(100, out smoothCount);
  72. // List<Vector3> results = new List<Vector3>();
  73. // for (int i = 0; i < smoothCount; ++i)
  74. // {
  75. // Vector3 node = new Vector3(smooths[i * 3], smooths[i * 3 + 1], smooths[i * 3 + 2]);
  76. // results.Add(node);
  77. // }
  78. // }
  79. // }
  80. // }
  81. //
  82. // #endregion
  83. }
  84. }