CombatSenceController.cs 8.1 KB

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