PathfindingComponentSystem.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections.Generic;
  3. using Unity.Mathematics;
  4. namespace ET
  5. {
  6. [FriendOf(typeof(PathfindingComponent))]
  7. public static class PathfindingComponentSystem
  8. {
  9. [ObjectSystem]
  10. public class AwakeSystem: AwakeSystem<PathfindingComponent, string>
  11. {
  12. protected override void Awake(PathfindingComponent self, string name)
  13. {
  14. self.Name = name;
  15. self.NavMesh = NavmeshComponent.Instance.Get(name);
  16. if (self.NavMesh == 0)
  17. {
  18. throw new Exception($"nav load fail: {name}");
  19. }
  20. }
  21. }
  22. [ObjectSystem]
  23. public class DestroySystem: DestroySystem<PathfindingComponent>
  24. {
  25. protected override void Destroy(PathfindingComponent self)
  26. {
  27. self.Name = string.Empty;
  28. self.NavMesh = 0;
  29. }
  30. }
  31. public static void Find(this PathfindingComponent self, float3 start, float3 target, List<float3> result)
  32. {
  33. if (self.NavMesh == 0)
  34. {
  35. Log.Debug("寻路| Find 失败 pathfinding ptr is zero");
  36. throw new Exception($"pathfinding ptr is zero: {self.DomainScene().Name}");
  37. }
  38. self.StartPos[0] = -start.x;
  39. self.StartPos[1] = start.y;
  40. self.StartPos[2] = start.z;
  41. self.EndPos[0] = -target.x;
  42. self.EndPos[1] = target.y;
  43. self.EndPos[2] = target.z;
  44. //Log.Debug($"start find path: {self.GetParent<Unit>().Id}");
  45. int n = Recast.RecastFind(self.NavMesh, PathfindingComponent.extents, self.StartPos, self.EndPos, self.Result);
  46. for (int i = 0; i < n; ++i)
  47. {
  48. int index = i * 3;
  49. result.Add(new float3(-self.Result[index], self.Result[index + 1], self.Result[index + 2]));
  50. }
  51. //Log.Debug($"finish find path: {self.GetParent<Unit>().Id} {result.ListToString()}");
  52. }
  53. public static void FindWithAdjust(this PathfindingComponent self, float3 start, float3 target, List<float3> result,float adjustRaduis)
  54. {
  55. self.Find(start, target, result);
  56. for (int i = 0; i < result.Count; i++)
  57. {
  58. float3 adjust = self.FindRandomPointWithRaduis(result[i], adjustRaduis);
  59. result[i] = adjust;
  60. }
  61. }
  62. public static float3 FindRandomPointWithRaduis(this PathfindingComponent self, float3 pos, float raduis)
  63. {
  64. if (self.NavMesh == 0)
  65. {
  66. throw new Exception($"pathfinding ptr is zero: {self.DomainScene().Name}");
  67. }
  68. if (raduis > PathfindingComponent.FindRandomNavPosMaxRadius * 0.001f)
  69. {
  70. throw new Exception($"pathfinding raduis is too large,cur: {raduis}, max: {PathfindingComponent.FindRandomNavPosMaxRadius}");
  71. }
  72. int degrees = RandomGenerator.RandomNumber(0, 360);
  73. float r = RandomGenerator.RandomNumber(0, (int) (raduis * 1000)) / 1000f;
  74. float x = r * math.cos(math.radians(degrees));
  75. float z = r * math.sin(math.radians(degrees));
  76. float3 findpos = new float3(pos.x + x, pos.y, pos.z + z);
  77. return self.RecastFindNearestPoint(findpos);
  78. }
  79. /// <summary>
  80. /// 以pos为中心各自在宽和高的左右 前后两个方向延伸
  81. /// </summary>
  82. /// <param name="self"></param>
  83. /// <param name="pos"></param>
  84. /// <param name="width"></param>
  85. /// <param name="height"></param>
  86. /// <returns></returns>
  87. /// <exception cref="Exception"></exception>
  88. public static float3 FindRandomPointWithRectangle(this PathfindingComponent self, float3 pos, int width, int height)
  89. {
  90. if (self.NavMesh == 0)
  91. {
  92. throw new Exception($"pathfinding ptr is zero: {self.DomainScene().Name}");
  93. }
  94. if (width > PathfindingComponent.FindRandomNavPosMaxRadius * 0.001f || height > PathfindingComponent.FindRandomNavPosMaxRadius * 0.001f)
  95. {
  96. throw new Exception($"pathfinding rectangle is too large,width: {width} height: {height}, max: {PathfindingComponent.FindRandomNavPosMaxRadius}");
  97. }
  98. float x = RandomGenerator.RandomNumber(-width, width);
  99. float z = RandomGenerator.RandomNumber(-height, height);
  100. float3 findpos = new float3(pos.x + x, pos.y, pos.z + z);
  101. return self.RecastFindNearestPoint(findpos);
  102. }
  103. public static float3 FindRandomPointWithRaduis(this PathfindingComponent self, float3 pos, float minRadius, float maxRadius)
  104. {
  105. if (self.NavMesh == 0)
  106. {
  107. throw new Exception($"pathfinding ptr is zero: {self.DomainScene().Name}");
  108. }
  109. if (maxRadius > PathfindingComponent.FindRandomNavPosMaxRadius * 0.001f)
  110. {
  111. throw new Exception($"pathfinding raduis is too large,cur: {maxRadius}, max: {PathfindingComponent.FindRandomNavPosMaxRadius}");
  112. }
  113. int degrees = RandomGenerator.RandomNumber(0, 360);
  114. float r = RandomGenerator.RandomNumber((int) (minRadius * 1000), (int) (maxRadius * 1000)) / 1000f;
  115. float x = r * math.cos(math.radians(degrees));
  116. float z = r * math.sin(math.radians(degrees));
  117. float3 findpos = new float3(pos.x + x, pos.y, pos.z + z);
  118. return self.RecastFindNearestPoint(findpos);
  119. }
  120. public static float3 RecastFindNearestPoint(this PathfindingComponent self, float3 pos)
  121. {
  122. if (self.NavMesh == 0)
  123. {
  124. throw new Exception($"pathfinding ptr is zero: {self.DomainScene().Name}");
  125. }
  126. self.StartPos[0] = -pos.x;
  127. self.StartPos[1] = pos.y;
  128. self.StartPos[2] = pos.z;
  129. int ret = Recast.RecastFindNearestPoint(self.NavMesh, PathfindingComponent.extents, self.StartPos, self.EndPos);
  130. if (ret == 0)
  131. {
  132. throw new Exception($"RecastFindNearestPoint fail, 可能是位置配置有问题: sceneName:{self.DomainScene().Name} {pos} {self.Name} {self.GetParent<Unit>().Id} {self.GetParent<Unit>().Config.Id} {self.EndPos.ArrayToString()}");
  133. }
  134. return new float3(-self.EndPos[0], self.EndPos[1], self.EndPos[2]);
  135. }
  136. }
  137. }