CombatSenceController.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using Core.Utility;
  4. using Fort23.Core;
  5. using Fort23.UTool;
  6. using GameLogic.CombatScenesTool;
  7. using UnityEngine;
  8. using Random = UnityEngine.Random;
  9. namespace GameLogic.Combat.CombatTool
  10. {
  11. public class CombatSenceController : IDisposable
  12. {
  13. // private AssetHandle scenesHandle;
  14. // public GameObject scenes1;
  15. public CombatSencePath currBesselPath;
  16. public CombatSencePath NextBesselPath;
  17. private float moveTime;
  18. private Vector3 lasetPos;
  19. private float maxD = 100;
  20. private Vector3 lasetDir;
  21. public float currTime;
  22. public Transform moveRoot;
  23. private List<CombatSencePath> reclaimSence = new List<CombatSencePath>();
  24. public async CTask InitScenes()
  25. {
  26. GameObject gameObject = new GameObject("mvoeRoot");
  27. moveRoot = gameObject.transform;
  28. CTaskAwaitBuffer cTaskAwaitBuffer = new CTaskAwaitBuffer();
  29. await GObjectPool.Instance.FetchAsync<GameObjectPool>("shan1", null, null, true, "shan1");
  30. lasetDir = Vector3.forward;
  31. lasetPos = new Vector3(0, 5, 0);
  32. await cTaskAwaitBuffer.WaitAll();
  33. currBesselPath = InitBesselPath(lasetPos);
  34. }
  35. private CombatSencePath InitBesselPath(Vector3 satrtPos)
  36. {
  37. CombatSencePath besselPath = new CombatSencePath();
  38. Vector3 target = satrtPos + lasetDir.normalized * maxD;
  39. // if (offx == 0)
  40. {
  41. besselPath.SetPos(satrtPos, target);
  42. }
  43. Vector3 p = besselPath.GetValue(0.99f);
  44. lasetDir = (target - p).normalized;
  45. GameObject gameObject = new GameObject("path");
  46. gameObject.transform.position = satrtPos;
  47. CombatPathMono combatPathMono = gameObject.AddComponent<CombatPathMono>();
  48. combatPathMono.BesselPath = besselPath;
  49. InitZhuangSHi(besselPath);
  50. return besselPath;
  51. }
  52. private CombatSencePath InitBesselPath(Vector3 satrtPos, float offx)
  53. {
  54. CombatSencePath besselPath = new CombatSencePath();
  55. Vector3 dir = Quaternion.LookRotation(lasetDir) * new Vector3(offx, 0, 1);
  56. Vector3 target = satrtPos + dir.normalized * maxD;
  57. Vector3 dir2 = Quaternion.LookRotation(lasetDir) * new Vector3(offx * -1, 0, 1);
  58. Vector3 centre = satrtPos + dir2.normalized * maxD;
  59. // if (offx == 0)
  60. {
  61. besselPath.SetPos(satrtPos, centre, target);
  62. }
  63. Vector3 p = besselPath.GetValue(0.99f);
  64. lasetDir = (target - p).normalized;
  65. GameObject gameObject = new GameObject("path");
  66. gameObject.transform.position = satrtPos;
  67. CombatPathMono combatPathMono = gameObject.AddComponent<CombatPathMono>();
  68. combatPathMono.BesselPath = besselPath;
  69. InitZhuangSHi(besselPath);
  70. return besselPath;
  71. }
  72. private async CTask InitZhuangSHi(CombatSencePath besselPath)
  73. {
  74. int count = Random.Range(30, 40);
  75. for (int i = 0; i < count; i++)
  76. {
  77. float md = Random.Range(0.1f, 0.9f);
  78. Vector3 p = besselPath.GetValue(md);
  79. Vector3 p2 = besselPath.GetValue(md - 0.01f);
  80. Vector3 dir = (p - p2);
  81. Vector3 cross = Vector3.Cross(dir, Vector3.up);
  82. cross = cross.normalized;
  83. int odds = Random.Range(0, 100);
  84. p += cross * Random.Range(8, 25) * (odds < 50 ? 1 : -1);
  85. GameObjectPool gameObjectPool =
  86. await GObjectPool.Instance.FetchAsync<GameObjectPool>("shan1", null, poolName: "shan1");
  87. GameObject g = gameObjectPool.own;
  88. g.SetActive(true);
  89. besselPath.allPool.Add(gameObjectPool);
  90. g.transform.position = new Vector3(p.x, 0, p.z) + new Vector3(0, Random.Range(-4, -1), 0);
  91. g.transform.eulerAngles = new Vector3(-90, 0, Random.Range(0, 360));
  92. }
  93. }
  94. public Vector3 GetTarget(float time)
  95. {
  96. if (time > 1)
  97. {
  98. if (NextBesselPath == null)
  99. {
  100. NextBesselPath = InitBesselPath(currBesselPath.b, 0);
  101. }
  102. time = time % 1;
  103. return NextBesselPath.GetValue(time);
  104. }
  105. else
  106. {
  107. return currBesselPath.GetValue(time);
  108. }
  109. }
  110. public Vector3 GetNextPoint(float time)
  111. {
  112. if (NextBesselPath == null)
  113. {
  114. NextBesselPath = InitBesselPath(currBesselPath.b, 0);
  115. }
  116. return NextBesselPath.GetValue(time);
  117. }
  118. public Vector3 Move(float dis)
  119. {
  120. float t = dis / currBesselPath.len;
  121. currTime += t;
  122. // Debug.Log("移动速度" + t + "/" + currBesselPath.len);
  123. Vector3 targetPos = Vector3.zero;
  124. if (currTime > 0.2f)
  125. {
  126. if (NextBesselPath == null)
  127. {
  128. int odds = Random.Range(0, 100);
  129. if (odds < 50)
  130. {
  131. NextBesselPath = InitBesselPath(currBesselPath.b, Random.Range(0, 100) < 50 ? -1 : 1);
  132. }
  133. else
  134. {
  135. NextBesselPath = InitBesselPath(currBesselPath.b);
  136. }
  137. }
  138. }
  139. if (currTime > 1)
  140. {
  141. if (NextBesselPath == null)
  142. {
  143. NextBesselPath = InitBesselPath(currBesselPath.b, Random.Range(0, 100) < 50 ? -1 : 1);
  144. }
  145. currTime = currTime % 1;
  146. // targetPos = NextBesselPath.GetValue(currTime);
  147. currBesselPath.reclaimTime = 3;
  148. reclaimSence.Add(currBesselPath);
  149. currBesselPath = NextBesselPath;
  150. NextBesselPath = null;
  151. }
  152. else
  153. {
  154. }
  155. targetPos = currBesselPath.GetValue(currTime);
  156. moveRoot.transform.position = targetPos;
  157. return targetPos;
  158. }
  159. public void Update(float t)
  160. {
  161. for (int i = 0; i < reclaimSence.Count; i++)
  162. {
  163. CombatSencePath sencePath = reclaimSence[i];
  164. sencePath.reclaimTime -= t;
  165. if (sencePath.reclaimTime < 0)
  166. {
  167. sencePath.DormancyObj();
  168. reclaimSence.RemoveAt(i);
  169. }
  170. }
  171. }
  172. public void Dispose()
  173. {
  174. // GObjectPool.Instance.
  175. }
  176. }
  177. }