PathfindingComponentSystem.cs 6.5 KB

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