CombatSenceController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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.Combat.CombatTool.SceneTool;
  8. using GameLogic.CombatScenesTool;
  9. using UnityEngine;
  10. using Random = UnityEngine.Random;
  11. namespace GameLogic.Combat.CombatTool
  12. {
  13. public class CombatSenceController : IDisposable
  14. {
  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. protected GameObjectPool ShengShiQiuTi;
  25. private float shengShiStartSize = 3;
  26. private float shengShiEndSize = 300;
  27. private float _shengShiAddTime;
  28. private bool _isUpdateShengShi;
  29. private SceneMonoConfig SceneMonoConfig;
  30. public async CTask InitScenes()
  31. {
  32. GameObject gameObject = new GameObject("mvoeRoot");
  33. moveRoot = gameObject.transform;
  34. CTaskAwaitBuffer cTaskAwaitBuffer = new CTaskAwaitBuffer();
  35. var task_a = AssetBundleLoadManager.Instance.LoadAssetAsyncTask<GameObject>("LevelBattle01.prefab",
  36. delegate(AssetHandle assetHandle)
  37. {
  38. GameObject gameObject = assetHandle.AssetObject<GameObject>();
  39. SceneMonoConfig = gameObject.GetComponent<SceneMonoConfig>();
  40. });
  41. cTaskAwaitBuffer.AddTask(task_a);
  42. ShengShiQiuTi =
  43. await GObjectPool.Instance.FetchAsync<GameObjectPool>("ShengShiQiuTi", null, null, false,
  44. "ShengShiQiuTi");
  45. ShengShiQiuTi.SetActive(false);
  46. lasetDir = Vector3.forward;
  47. lasetPos = new Vector3(0, 20, 0);
  48. await cTaskAwaitBuffer.WaitAll();
  49. currBesselPath = InitBesselPath(lasetPos);
  50. SceneMonoConfig.Init(Camera.main.transform.parent);
  51. }
  52. private CombatSencePath InitBesselPath(Vector3 satrtPos)
  53. {
  54. CombatSencePath besselPath = new CombatSencePath();
  55. Vector3 target = satrtPos + lasetDir.normalized * maxD;
  56. // if (offx == 0)
  57. {
  58. besselPath.SetPos(satrtPos, target);
  59. }
  60. Vector3 p = besselPath.GetValue(0.99f);
  61. lasetDir = (target - p).normalized;
  62. // GameObject gameObject = new GameObject("path");
  63. // gameObject.transform.position = satrtPos;
  64. // CombatPathMono combatPathMono = gameObject.AddComponent<CombatPathMono>();
  65. // combatPathMono.BesselPath = besselPath;
  66. InitZhuangSHi(besselPath);
  67. return besselPath;
  68. }
  69. private CombatSencePath InitBesselPath(Vector3 satrtPos, float offx)
  70. {
  71. CombatSencePath besselPath = new CombatSencePath();
  72. Vector3 dir = Quaternion.LookRotation(lasetDir) * new Vector3(offx, 0, 1);
  73. Vector3 target = satrtPos + dir.normalized * maxD;
  74. Vector3 dir2 = Quaternion.LookRotation(lasetDir) * new Vector3(offx * -1, 0, 1);
  75. Vector3 centre = satrtPos + dir2.normalized * maxD;
  76. // if (offx == 0)
  77. {
  78. besselPath.SetPos(satrtPos, centre, target);
  79. }
  80. Vector3 p = besselPath.GetValue(0.99f);
  81. lasetDir = (target - p).normalized;
  82. // GameObject gameObject = new GameObject("path");
  83. // gameObject.transform.position = satrtPos;
  84. // CombatPathMono combatPathMono = gameObject.AddComponent<CombatPathMono>();
  85. // combatPathMono.BesselPath = besselPath;
  86. InitZhuangSHi(besselPath);
  87. return besselPath;
  88. }
  89. private async CTask InitZhuangSHi(CombatSencePath besselPath)
  90. {
  91. InitRoadside(besselPath);
  92. InitRoadside_1(besselPath);
  93. InitButton(besselPath);
  94. }
  95. private async CTask InitRoadside(CombatSencePath besselPath)
  96. {
  97. SceneConfig sceneConfig = SceneMonoConfig.roadside;
  98. int count = Random.Range(sceneConfig.minCount, sceneConfig.maxCount);
  99. for (int i = 0; i < count; i++)
  100. {
  101. SceneObjectConfig sceneObjectConfig = sceneConfig.GetSceneObjectConfig();
  102. if (sceneObjectConfig == null)
  103. {
  104. continue;
  105. }
  106. float md = Random.Range(0.05f, 1f);
  107. Vector3 p = besselPath.GetValue(md);
  108. Vector3 p2 = besselPath.GetValue(md - 0.01f);
  109. Vector3 dir = (p - p2);
  110. Vector3 cross = Vector3.Cross(dir, Vector3.up);
  111. cross = cross.normalized;
  112. int odds = Random.Range(0, 100);
  113. p += cross * Random.Range(8, 25) * (odds < 50 ? 1 : -1);
  114. GameObjectPool gameObjectPool = await sceneObjectConfig.GetNewPrefab();
  115. GameObject g = gameObjectPool.own;
  116. g.SetActive(true);
  117. besselPath.allPool.Add(gameObjectPool);
  118. float y = Random.Range(sceneObjectConfig.minY, sceneObjectConfig.maxY);
  119. g.transform.position = new Vector3(p.x, y, p.z);
  120. g.transform.eulerAngles = new Vector3(-90, 0, Random.Range(0, 360));
  121. }
  122. }
  123. private async CTask InitRoadside_1(CombatSencePath besselPath)
  124. {
  125. SceneConfig sceneConfig = SceneMonoConfig.roadside_1;
  126. int count = Random.Range(sceneConfig.minCount, sceneConfig.maxCount);
  127. for (int i = 0; i < count; i++)
  128. {
  129. SceneObjectConfig sceneObjectConfig = sceneConfig.GetSceneObjectConfig();
  130. if (sceneObjectConfig == null)
  131. {
  132. continue;
  133. }
  134. float md = Random.Range(0.05f, 1f);
  135. Vector3 p = besselPath.GetValue(md);
  136. Vector3 p2 = besselPath.GetValue(md - 0.01f);
  137. Vector3 dir = (p - p2);
  138. Vector3 cross = Vector3.Cross(dir, Vector3.up);
  139. cross = cross.normalized;
  140. int odds = Random.Range(0, 100);
  141. p += cross * Random.Range(25, 40) * (odds < 50 ? 1 : -1);
  142. GameObjectPool gameObjectPool = await sceneObjectConfig.GetNewPrefab();
  143. GameObject g = gameObjectPool.own;
  144. g.SetActive(true);
  145. besselPath.allPool.Add(gameObjectPool);
  146. float y = Random.Range(sceneObjectConfig.minY, sceneObjectConfig.maxY);
  147. g.transform.position = new Vector3(p.x, y, p.z);
  148. g.transform.eulerAngles = new Vector3(-90, 0, Random.Range(0, 360));
  149. }
  150. }
  151. private async CTask InitButton(CombatSencePath besselPath)
  152. {
  153. SceneConfig sceneConfig = SceneMonoConfig.button;
  154. int count = Random.Range(sceneConfig.minCount, sceneConfig.maxCount);
  155. for (int i = 0; i < count; i++)
  156. {
  157. SceneObjectConfig sceneObjectConfig = sceneConfig.GetSceneObjectConfig();
  158. if (sceneObjectConfig == null)
  159. {
  160. continue;
  161. }
  162. float md = Random.Range(0.05f, 1f);
  163. Vector3 p = besselPath.GetValue(md);
  164. Vector3 p2 = besselPath.GetValue(md - 0.01f);
  165. Vector3 dir = (p - p2);
  166. Vector3 cross = Vector3.Cross(dir, Vector3.up);
  167. cross = cross.normalized;
  168. // int odds = Random.Range(0, 100);
  169. p += cross * Random.Range(-20, 20);
  170. GameObjectPool gameObjectPool = await sceneObjectConfig.GetNewPrefab();
  171. GameObject g = gameObjectPool.own;
  172. g.SetActive(true);
  173. besselPath.allPool.Add(gameObjectPool);
  174. float y = Random.Range(sceneObjectConfig.minY, sceneObjectConfig.maxY);
  175. g.transform.position = new Vector3(p.x, y, p.z);
  176. g.transform.eulerAngles = new Vector3(-90, 0, Random.Range(0, 360));
  177. }
  178. }
  179. public Vector3 GetTarget(float time)
  180. {
  181. if (time > 1)
  182. {
  183. if (NextBesselPath == null)
  184. {
  185. NextBesselPath = InitBesselPath(currBesselPath.b, 0);
  186. }
  187. time = time % 1;
  188. return NextBesselPath.GetValue(time);
  189. }
  190. else
  191. {
  192. return currBesselPath.GetValue(time);
  193. }
  194. }
  195. public Vector3 GetNextPoint(float time)
  196. {
  197. if (NextBesselPath == null)
  198. {
  199. NextBesselPath = InitBesselPath(currBesselPath.b, 0);
  200. }
  201. return NextBesselPath.GetValue(time);
  202. }
  203. public Vector3 Move(float dis)
  204. {
  205. float t = dis / currBesselPath.len;
  206. currTime += t;
  207. // Debug.Log("移动速度" + t + "/" + currBesselPath.len);
  208. Vector3 targetPos = Vector3.zero;
  209. if (currTime > 0.2f)
  210. {
  211. if (NextBesselPath == null)
  212. {
  213. int odds = Random.Range(0, 100);
  214. if (odds < 50)
  215. {
  216. NextBesselPath = InitBesselPath(currBesselPath.b, Random.Range(0, 100) < 50 ? -1 : 1);
  217. }
  218. else
  219. {
  220. NextBesselPath = InitBesselPath(currBesselPath.b);
  221. }
  222. }
  223. }
  224. if (currTime > 1)
  225. {
  226. if (NextBesselPath == null)
  227. {
  228. NextBesselPath = InitBesselPath(currBesselPath.b, Random.Range(0, 100) < 50 ? -1 : 1);
  229. }
  230. currTime = currTime % 1;
  231. // targetPos = NextBesselPath.GetValue(currTime);
  232. currBesselPath.reclaimTime = 3;
  233. reclaimSence.Add(currBesselPath);
  234. currBesselPath = NextBesselPath;
  235. NextBesselPath = null;
  236. CombatEventManager.Instance.Dispatch(CombatEventType.SencenBesselPathAlter, null);
  237. }
  238. else
  239. {
  240. }
  241. targetPos = currBesselPath.GetValue(currTime);
  242. moveRoot.transform.position = targetPos;
  243. return targetPos;
  244. }
  245. public void StartPayShengShi(Transform root)
  246. {
  247. ShengShiQiuTi.own.transform.SetParent(root);
  248. ShengShiQiuTi.own.transform.localPosition = Vector3.zero;
  249. ShengShiQiuTi.SetActive(true);
  250. _isUpdateShengShi = true;
  251. ShengShiQiuTi.own.transform.localScale = Vector3.one * shengShiStartSize;
  252. _shengShiAddTime = 0;
  253. }
  254. public void Update(float t)
  255. {
  256. SceneMonoConfig?.Update();
  257. if (_isUpdateShengShi)
  258. {
  259. _shengShiAddTime += t * 1f;
  260. float v = Mathf.Lerp(shengShiStartSize, shengShiEndSize, _shengShiAddTime);
  261. ShengShiQiuTi.own.transform.localScale = Vector3.one * v;
  262. if (_shengShiAddTime >= 1)
  263. {
  264. ShengShiQiuTi.SetActive(false);
  265. _isUpdateShengShi = false;
  266. }
  267. }
  268. for (int i = 0; i < reclaimSence.Count; i++)
  269. {
  270. CombatSencePath sencePath = reclaimSence[i];
  271. sencePath.reclaimTime -= t;
  272. if (sencePath.reclaimTime < 0)
  273. {
  274. sencePath.DormancyObj();
  275. reclaimSence.RemoveAt(i);
  276. }
  277. }
  278. }
  279. public void Dispose()
  280. {
  281. // GObjectPool.Instance.
  282. }
  283. }
  284. }