CombatSenceController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. dir2 = dir2.normalized;
  76. Vector3 centre = satrtPos + dir2.normalized * maxD;
  77. // if (offx == 0)
  78. {
  79. besselPath.SetPos(satrtPos, centre, target);
  80. }
  81. Vector3 p = besselPath.GetValue(0.99f);
  82. lasetDir = (target - p).normalized;
  83. // GameObject gameObject = new GameObject("path");
  84. // gameObject.transform.position = satrtPos;
  85. // CombatPathMono combatPathMono = gameObject.AddComponent<CombatPathMono>();
  86. // combatPathMono.BesselPath = besselPath;
  87. InitZhuangSHi(besselPath);
  88. return besselPath;
  89. }
  90. private async CTask InitZhuangSHi(CombatSencePath besselPath)
  91. {
  92. InitRoadside(besselPath);
  93. InitRoadside_1(besselPath);
  94. InitButton(besselPath);
  95. }
  96. private async CTask InitRoadside(CombatSencePath besselPath)
  97. {
  98. SceneConfig sceneConfig = SceneMonoConfig.roadside;
  99. int count = Random.Range(sceneConfig.minCount, sceneConfig.maxCount);
  100. for (int i = 0; i < count; i++)
  101. {
  102. SceneObjectConfig sceneObjectConfig = sceneConfig.GetSceneObjectConfig();
  103. if (sceneObjectConfig == null)
  104. {
  105. continue;
  106. }
  107. float md = Random.Range(0.05f, 1f);
  108. Vector3 p = besselPath.GetValue(md);
  109. Vector3 p2 = besselPath.GetValue(md - 0.01f);
  110. Vector3 dir = (p - p2);
  111. Vector3 cross = Vector3.Cross(dir, Vector3.up);
  112. cross = cross.normalized;
  113. int odds = Random.Range(0, 100);
  114. p += cross * Random.Range(12, 25) * (odds < 50 ? 1 : -1);
  115. SceneDecoration gameObjectPool = await GetSceneDecoration(sceneObjectConfig);
  116. GameObject g = gameObjectPool.pool.own;
  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(40, 60) * (odds < 50 ? 1 : -1);
  142. SceneDecoration gameObjectPool = await GetSceneDecoration(sceneObjectConfig);
  143. GameObject g = gameObjectPool.pool.own;
  144. besselPath.allPool.Add(gameObjectPool);
  145. float y = Random.Range(sceneObjectConfig.minY, sceneObjectConfig.maxY);
  146. g.transform.position = new Vector3(p.x, y, p.z);
  147. g.transform.eulerAngles = new Vector3(-90, 0, Random.Range(0, 360));
  148. }
  149. }
  150. private async CTask InitButton(CombatSencePath besselPath)
  151. {
  152. SceneConfig sceneConfig = SceneMonoConfig.button;
  153. int count = Random.Range(sceneConfig.minCount, sceneConfig.maxCount);
  154. for (int i = 0; i < count; i++)
  155. {
  156. SceneObjectConfig sceneObjectConfig = sceneConfig.GetSceneObjectConfig();
  157. if (sceneObjectConfig == null)
  158. {
  159. continue;
  160. }
  161. float md = Random.Range(0.05f, 1f);
  162. Vector3 p = besselPath.GetValue(md);
  163. Vector3 p2 = besselPath.GetValue(md - 0.01f);
  164. Vector3 dir = (p - p2);
  165. Vector3 cross = Vector3.Cross(dir, Vector3.up);
  166. cross = cross.normalized;
  167. // int odds = Random.Range(0, 100);
  168. p += cross * Random.Range(-20, 20);
  169. SceneDecoration gameObjectPool = await GetSceneDecoration(sceneObjectConfig);
  170. GameObject g = gameObjectPool.pool.own;
  171. besselPath.allPool.Add(gameObjectPool);
  172. float y = Random.Range(sceneObjectConfig.minY, sceneObjectConfig.maxY);
  173. g.transform.position = new Vector3(p.x, y, p.z);
  174. g.transform.eulerAngles = new Vector3(-90, 0, Random.Range(0, 360));
  175. }
  176. }
  177. private async CTask<SceneDecoration> GetSceneDecoration(SceneObjectConfig sceneObjectConfig)
  178. {
  179. GameObjectPool gameObjectPool = await sceneObjectConfig.GetNewPrefab();
  180. SceneDecoration sceneDecoration = CObjectPool.Instance.Fetch<SceneDecoration>();
  181. sceneDecoration.Init(gameObjectPool);
  182. GameObject g = gameObjectPool.own;
  183. g.SetActive(true);
  184. return sceneDecoration;
  185. }
  186. public Vector3 GetTarget(float time)
  187. {
  188. if (time > 1)
  189. {
  190. if (NextBesselPath == null)
  191. {
  192. NextBesselPath = InitBesselPath(currBesselPath.b, 0);
  193. }
  194. time = time % 1;
  195. return NextBesselPath.GetValue(time);
  196. }
  197. else
  198. {
  199. return currBesselPath.GetValue(time);
  200. }
  201. }
  202. public Vector3 GetNextPoint(float time)
  203. {
  204. if (NextBesselPath == null)
  205. {
  206. NextBesselPath = InitBesselPath(currBesselPath.b, 0);
  207. }
  208. return NextBesselPath.GetValue(time);
  209. }
  210. public Vector3 Move(float dis)
  211. {
  212. float t = dis / currBesselPath.len;
  213. currTime += t;
  214. // Debug.Log("移动速度" + t + "/" + currBesselPath.len);
  215. Vector3 targetPos = Vector3.zero;
  216. if (currTime > 0.01f)
  217. {
  218. if (NextBesselPath == null)
  219. {
  220. int odds = Random.Range(0, 100);
  221. if (odds < 50)
  222. {
  223. NextBesselPath = InitBesselPath(currBesselPath.b, Random.Range(0, 100) < 50 ? -1 : 1);
  224. }
  225. else
  226. {
  227. NextBesselPath = InitBesselPath(currBesselPath.b);
  228. }
  229. }
  230. }
  231. if (currTime > 1)
  232. {
  233. if (NextBesselPath == null)
  234. {
  235. NextBesselPath = InitBesselPath(currBesselPath.b, Random.Range(0, 100) < 50 ? -1 : 1);
  236. }
  237. currTime = currTime % 1;
  238. // targetPos = NextBesselPath.GetValue(currTime);
  239. currBesselPath.reclaimTime = 3;
  240. reclaimSence.Add(currBesselPath);
  241. currBesselPath = NextBesselPath;
  242. NextBesselPath = null;
  243. CombatEventManager.Instance.Dispatch(CombatEventType.SencenBesselPathAlter, null);
  244. }
  245. else
  246. {
  247. }
  248. targetPos = currBesselPath.GetValue(currTime);
  249. moveRoot.transform.position = targetPos;
  250. return targetPos;
  251. }
  252. public void StartPayShengShi(Transform root)
  253. {
  254. ShengShiQiuTi.own.transform.SetParent(root);
  255. ShengShiQiuTi.own.transform.localPosition = Vector3.zero;
  256. ShengShiQiuTi.SetActive(true);
  257. _isUpdateShengShi = true;
  258. ShengShiQiuTi.own.transform.localScale = Vector3.one * shengShiStartSize;
  259. _shengShiAddTime = 0;
  260. }
  261. public void Update(float t)
  262. {
  263. SceneMonoConfig?.Update();
  264. if (currBesselPath != null)
  265. {
  266. currBesselPath.Update(t);
  267. }
  268. if (NextBesselPath != null)
  269. {
  270. NextBesselPath.Update(t);
  271. }
  272. if (_isUpdateShengShi)
  273. {
  274. _shengShiAddTime += t * 1f;
  275. float v = Mathf.Lerp(shengShiStartSize, shengShiEndSize, _shengShiAddTime);
  276. ShengShiQiuTi.own.transform.localScale = Vector3.one * v;
  277. if (_shengShiAddTime >= 1)
  278. {
  279. ShengShiQiuTi.SetActive(false);
  280. _isUpdateShengShi = false;
  281. }
  282. }
  283. for (int i = 0; i < reclaimSence.Count; i++)
  284. {
  285. CombatSencePath sencePath = reclaimSence[i];
  286. sencePath.reclaimTime -= t;
  287. if (sencePath.reclaimTime < 0)
  288. {
  289. sencePath.DormancyObj();
  290. reclaimSence.RemoveAt(i);
  291. }
  292. }
  293. }
  294. public void Dispose()
  295. {
  296. // GObjectPool.Instance.
  297. }
  298. }
  299. }