MapUnLimited.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class MapUnLimited : MonoBehaviour
  5. {
  6. public Transform runWayPrefab;
  7. public Transform groudPrefab;
  8. public Transform[] extendPrefabs;
  9. public float runWayDistance = 148;
  10. public float groundDistance = 150;
  11. List<Queue<Transform>> runWayList;
  12. List<Queue<Transform>> groundList;
  13. bool bInited = false;
  14. int InitialCol = 2;
  15. int InitialRow = 5;
  16. Transform mCurShowExtendPrefab = null;
  17. void Start()
  18. {
  19. InitQueue();
  20. //EventMgr.AddEventListener<string>(ECoreEventType.EID_SHOW_SCENE_GO, OnShowSceneGo);
  21. //EventMgr.AddEventListener<bool>(ECoreEventType.EID_RESTORE_SCENE, OnRestoreScene);
  22. }
  23. private void OnDestroy()
  24. {
  25. //EventMgr.RemoveEventListener<string>(ECoreEventType.EID_SHOW_SCENE_GO, OnShowSceneGo);
  26. //EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_RESTORE_SCENE, OnRestoreScene);
  27. }
  28. void Update()
  29. {
  30. if (!bInited) return;
  31. for(int i =0; i < runWayList.Count;i++)
  32. {
  33. Queue<Transform> queue = runWayList[i];
  34. float dist = (queue.Count / 2) * runWayDistance;
  35. if (transform.position.z - queue.Peek().position.z >= dist)
  36. {
  37. Transform runway = queue.Dequeue();
  38. Transform last = queue.ToArray()[queue.Count - 1];
  39. runway.position = new Vector3(last.position.x, last.position.y, last.position.z + runWayDistance);
  40. queue.Enqueue(runway);
  41. SceneEventCfg sceneEventCfg = runway.GetComponent<SceneEventCfg>();
  42. if (sceneEventCfg != null)
  43. {
  44. sceneEventCfg.SetSceneEvent();
  45. }
  46. }
  47. }
  48. if (groundList != null && groundList.Count > 0)
  49. {
  50. for(int i = 0; i < groundList.Count;i++)
  51. {
  52. Queue<Transform> queue = groundList[i];
  53. float dist = (queue.Count / 2) * runWayDistance;
  54. if (transform.position.z - queue.Peek().position.z >= dist)
  55. {
  56. Transform ground = queue.Dequeue();
  57. Transform last = queue.ToArray()[queue.Count - 1];
  58. ground.position = new Vector3(last.position.x, last.position.y, last.position.z + runWayDistance);
  59. queue.Enqueue(ground);
  60. }
  61. }
  62. }
  63. }
  64. void InitQueue()
  65. {
  66. if (!bInited)
  67. {
  68. PrefabLightmapData lmData = runWayPrefab.GetComponent<PrefabLightmapData>();
  69. if (lmData != null)
  70. {
  71. lmData.SetUpLightmap();
  72. }
  73. runWayList = new List<Queue<Transform>>();
  74. for(int c = 0; c < InitialCol;c++)
  75. {
  76. Queue<Transform> queue = new Queue<Transform>();
  77. for (int i = 0; i < InitialRow; i++)
  78. {
  79. Transform runway = (Transform)Transform.Instantiate(runWayPrefab, new Vector3(c * runWayDistance, runWayPrefab.localPosition.y, runWayDistance * (i - InitialRow/2)), Quaternion.identity);
  80. PrefabLightmapData lightmapData = runway.GetComponent<PrefabLightmapData>();
  81. if (lightmapData != null)
  82. {
  83. lightmapData.SetUpLightmap();
  84. }
  85. queue.Enqueue(runway);
  86. SceneEventCfg sceneEventCfg = runway.GetComponent<SceneEventCfg>();
  87. if (sceneEventCfg != null)
  88. {
  89. sceneEventCfg.SetSceneEvent();
  90. }
  91. }
  92. runWayList.Add(queue);
  93. }
  94. runWayPrefab.gameObject.SetActive(false);
  95. if (groudPrefab!=null)
  96. {
  97. groundList = new List<Queue<Transform>>();
  98. for (int c = 0; c < InitialCol; c++)
  99. {
  100. Queue<Transform> queue = new Queue<Transform>();
  101. for (int i = 0; i < InitialRow; i++)
  102. {
  103. Transform ground = (Transform)Transform.Instantiate(groudPrefab, new Vector3(c * runWayDistance, groudPrefab.localPosition.y, runWayDistance * (i - InitialRow / 2)), Quaternion.identity);
  104. PrefabLightmapData lightmapData = ground.GetComponent<PrefabLightmapData>();
  105. if (lightmapData != null)
  106. {
  107. lightmapData.SetUpLightmap();
  108. }
  109. queue.Enqueue(ground);
  110. }
  111. groundList.Add(queue);
  112. }
  113. groudPrefab.gameObject.SetActive(false);
  114. }
  115. bInited = true;
  116. }
  117. }
  118. void OnShowSceneGo(CoreEvent<string> ce)
  119. {
  120. if (extendPrefabs == null) return;
  121. Transform trans = GetTrans(ce.Data);
  122. if (trans == null) return;
  123. mCurShowExtendPrefab = trans;
  124. Transform locWay = null;
  125. if (runWayList.Count > 0)
  126. {
  127. for(int idx =0; idx < runWayList.Count;idx++)
  128. {
  129. Queue<Transform> queue = runWayList[idx];
  130. IEnumerator iter = queue.GetEnumerator();
  131. while (iter.MoveNext())
  132. {
  133. Transform t = iter.Current as Transform;
  134. if (t != null)
  135. t.gameObject.SetActive(false);
  136. if(transform.position.z>= t.position.z && transform.position.z <= t.position.z + runWayDistance && idx == 0)
  137. {
  138. locWay = t;
  139. }
  140. }
  141. }
  142. }
  143. if(groundList!=null)
  144. {
  145. for(int idx =0; idx < groundList.Count;idx++)
  146. {
  147. Queue<Transform> queue = groundList[idx];
  148. IEnumerator iter = queue.GetEnumerator();
  149. while (iter.MoveNext())
  150. {
  151. Transform t = iter.Current as Transform;
  152. if (t != null)
  153. t.gameObject.SetActive(false);
  154. }
  155. }
  156. }
  157. mCurShowExtendPrefab.gameObject.SetActive(true);
  158. mCurShowExtendPrefab.position = locWay.position;
  159. SyncPosWithCamPos com = mCurShowExtendPrefab.GetComponentInChildren<SyncPosWithCamPos>(true);
  160. if(com!=null)
  161. {
  162. Vector3 pos = com.transform.position;
  163. pos.z = BattleCamera.Instance.CamPosition.z - com.width * 0.5f;
  164. com.transform.position = pos;
  165. }
  166. //BattleCamera.Instance.FadeIn();
  167. }
  168. void OnRestoreScene(CoreEvent<bool> ce)
  169. {
  170. if (mCurShowExtendPrefab == null) return;
  171. if (runWayList.Count > 0)
  172. {
  173. for(int idx =0; idx < runWayList.Count;idx++)
  174. {
  175. Queue<Transform> queue = runWayList[idx];
  176. IEnumerator iter = queue.GetEnumerator();
  177. while (iter.MoveNext())
  178. {
  179. Transform t = iter.Current as Transform;
  180. if (t != null)
  181. t.gameObject.SetActive(true);
  182. }
  183. }
  184. }
  185. if (groundList !=null && groundList.Count > 0)
  186. {
  187. for(int idx =0; idx < groundList.Count;idx++)
  188. {
  189. Queue<Transform> queue = groundList[idx];
  190. IEnumerator iter = queue.GetEnumerator();
  191. while (iter.MoveNext())
  192. {
  193. Transform t = iter.Current as Transform;
  194. if (t != null)
  195. t.gameObject.SetActive(true);
  196. }
  197. }
  198. }
  199. mCurShowExtendPrefab.gameObject.SetActive(false);
  200. }
  201. Transform GetTrans(string name)
  202. {
  203. if (extendPrefabs == null) return null;
  204. for(int idx =0; idx < extendPrefabs.Length;idx++)
  205. {
  206. if (extendPrefabs[idx].name == name)
  207. return extendPrefabs[idx];
  208. }
  209. return null;
  210. }
  211. }