CombatSenceController.cs 6.9 KB

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