CombatSenceController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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(12, 25) * (odds < 50 ? 1 : -1);
  114. SceneDecoration gameObjectPool = await GetSceneDecoration(sceneObjectConfig);
  115. GameObject g = gameObjectPool.pool.own;
  116. besselPath.allPool.Add(gameObjectPool);
  117. float y = Random.Range(sceneObjectConfig.minY, sceneObjectConfig.maxY);
  118. g.transform.position = new Vector3(p.x, y, p.z);
  119. g.transform.eulerAngles = new Vector3(-90, 0, Random.Range(0, 360));
  120. }
  121. }
  122. private async CTask InitRoadside_1(CombatSencePath besselPath)
  123. {
  124. SceneConfig sceneConfig = SceneMonoConfig.roadside_1;
  125. int count = Random.Range(sceneConfig.minCount, sceneConfig.maxCount);
  126. for (int i = 0; i < count; i++)
  127. {
  128. SceneObjectConfig sceneObjectConfig = sceneConfig.GetSceneObjectConfig();
  129. if (sceneObjectConfig == null)
  130. {
  131. continue;
  132. }
  133. float md = Random.Range(0.05f, 1f);
  134. Vector3 p = besselPath.GetValue(md);
  135. Vector3 p2 = besselPath.GetValue(md - 0.01f);
  136. Vector3 dir = (p - p2);
  137. Vector3 cross = Vector3.Cross(dir, Vector3.up);
  138. cross = cross.normalized;
  139. int odds = Random.Range(0, 100);
  140. p += cross * Random.Range(40, 60) * (odds < 50 ? 1 : -1);
  141. SceneDecoration gameObjectPool = await GetSceneDecoration(sceneObjectConfig);
  142. GameObject g = gameObjectPool.pool.own;
  143. besselPath.allPool.Add(gameObjectPool);
  144. float y = Random.Range(sceneObjectConfig.minY, sceneObjectConfig.maxY);
  145. g.transform.position = new Vector3(p.x, y, p.z);
  146. g.transform.eulerAngles = new Vector3(-90, 0, Random.Range(0, 360));
  147. }
  148. }
  149. private async CTask InitButton(CombatSencePath besselPath)
  150. {
  151. SceneConfig sceneConfig = SceneMonoConfig.button;
  152. int count = Random.Range(sceneConfig.minCount, sceneConfig.maxCount);
  153. for (int i = 0; i < count; i++)
  154. {
  155. SceneObjectConfig sceneObjectConfig = sceneConfig.GetSceneObjectConfig();
  156. if (sceneObjectConfig == null)
  157. {
  158. continue;
  159. }
  160. float md = Random.Range(0.05f, 1f);
  161. Vector3 p = besselPath.GetValue(md);
  162. Vector3 p2 = besselPath.GetValue(md - 0.01f);
  163. Vector3 dir = (p - p2);
  164. Vector3 cross = Vector3.Cross(dir, Vector3.up);
  165. cross = cross.normalized;
  166. // int odds = Random.Range(0, 100);
  167. p += cross * Random.Range(-20, 20);
  168. SceneDecoration gameObjectPool = await GetSceneDecoration(sceneObjectConfig);
  169. GameObject g = gameObjectPool.pool.own;
  170. besselPath.allPool.Add(gameObjectPool);
  171. float y = Random.Range(sceneObjectConfig.minY, sceneObjectConfig.maxY);
  172. g.transform.position = new Vector3(p.x, y, p.z);
  173. g.transform.eulerAngles = new Vector3(-90, 0, Random.Range(0, 360));
  174. }
  175. }
  176. private async CTask<SceneDecoration> GetSceneDecoration(SceneObjectConfig sceneObjectConfig)
  177. {
  178. GameObjectPool gameObjectPool = await sceneObjectConfig.GetNewPrefab();
  179. SceneDecoration sceneDecoration = CObjectPool.Instance.Fetch<SceneDecoration>();
  180. sceneDecoration.Init(gameObjectPool);
  181. GameObject g = gameObjectPool.own;
  182. g.SetActive(true);
  183. return sceneDecoration;
  184. }
  185. public Vector3 GetTarget(float time)
  186. {
  187. if (time > 1)
  188. {
  189. if (NextBesselPath == null)
  190. {
  191. NextBesselPath = InitBesselPath(currBesselPath.b, 0);
  192. }
  193. time = time % 1;
  194. return NextBesselPath.GetValue(time);
  195. }
  196. else
  197. {
  198. return currBesselPath.GetValue(time);
  199. }
  200. }
  201. public Vector3 GetNextPoint(float time)
  202. {
  203. if (NextBesselPath == null)
  204. {
  205. NextBesselPath = InitBesselPath(currBesselPath.b, 0);
  206. }
  207. return NextBesselPath.GetValue(time);
  208. }
  209. public Vector3 Move(float dis)
  210. {
  211. float t = dis / currBesselPath.len;
  212. currTime += t;
  213. // Debug.Log("移动速度" + t + "/" + currBesselPath.len);
  214. Vector3 targetPos = Vector3.zero;
  215. if (currTime > 0.01f)
  216. {
  217. if (NextBesselPath == null)
  218. {
  219. int odds = Random.Range(0, 100);
  220. if (odds < 50)
  221. {
  222. NextBesselPath = InitBesselPath(currBesselPath.b, Random.Range(0, 100) < 50 ? -1 : 1);
  223. }
  224. else
  225. {
  226. NextBesselPath = InitBesselPath(currBesselPath.b);
  227. }
  228. }
  229. }
  230. if (currTime > 1)
  231. {
  232. if (NextBesselPath == null)
  233. {
  234. NextBesselPath = InitBesselPath(currBesselPath.b, Random.Range(0, 100) < 50 ? -1 : 1);
  235. }
  236. currTime = currTime % 1;
  237. // targetPos = NextBesselPath.GetValue(currTime);
  238. currBesselPath.reclaimTime = 3;
  239. reclaimSence.Add(currBesselPath);
  240. currBesselPath = NextBesselPath;
  241. NextBesselPath = null;
  242. CombatEventManager.Instance.Dispatch(CombatEventType.SencenBesselPathAlter, null);
  243. }
  244. else
  245. {
  246. }
  247. targetPos = currBesselPath.GetValue(currTime);
  248. moveRoot.transform.position = targetPos;
  249. return targetPos;
  250. }
  251. public void StartPayShengShi(Transform root)
  252. {
  253. ShengShiQiuTi.own.transform.SetParent(root);
  254. ShengShiQiuTi.own.transform.localPosition = Vector3.zero;
  255. ShengShiQiuTi.SetActive(true);
  256. _isUpdateShengShi = true;
  257. ShengShiQiuTi.own.transform.localScale = Vector3.one * shengShiStartSize;
  258. _shengShiAddTime = 0;
  259. }
  260. public void Update(float t)
  261. {
  262. SceneMonoConfig?.Update();
  263. if (currBesselPath != null)
  264. {
  265. currBesselPath.Update(t);
  266. }
  267. if (NextBesselPath != null)
  268. {
  269. NextBesselPath.Update(t);
  270. }
  271. if (_isUpdateShengShi)
  272. {
  273. _shengShiAddTime += t * 1f;
  274. float v = Mathf.Lerp(shengShiStartSize, shengShiEndSize, _shengShiAddTime);
  275. ShengShiQiuTi.own.transform.localScale = Vector3.one * v;
  276. if (_shengShiAddTime >= 1)
  277. {
  278. ShengShiQiuTi.SetActive(false);
  279. _isUpdateShengShi = false;
  280. }
  281. }
  282. for (int i = 0; i < reclaimSence.Count; i++)
  283. {
  284. CombatSencePath sencePath = reclaimSence[i];
  285. sencePath.reclaimTime -= t;
  286. if (sencePath.reclaimTime < 0)
  287. {
  288. sencePath.DormancyObj();
  289. reclaimSence.RemoveAt(i);
  290. }
  291. }
  292. }
  293. public void Dispose()
  294. {
  295. // GObjectPool.Instance.
  296. }
  297. }
  298. }