SceneGateManager.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using GameWorld;
  5. using UnityEngine;
  6. public class SceneGateManager : SingletonMono<SceneGateManager>
  7. {
  8. public float Progress
  9. {
  10. get
  11. {
  12. return (this.Async == null) ? 0f : this.Async.progress;
  13. }
  14. }
  15. private IEnumerator LoadLevelCoroutine(SwitchLevelGateData data, bool needProgressBar)
  16. {
  17. this.IsLocked = true;
  18. R.Player.ActionController.ChangeState(PlayerAction.StateEnum.Idle);
  19. float player2Ground = Physics2D.Raycast(R.Player.Transform.position, Vector2.down, 100f, LayerManager.GroundMask).distance;
  20. this.Async = LevelManager.LoadScene(data.ToLevelId);
  21. this.Async.allowSceneActivation = false;
  22. while (!this.AllowSceneActivation)
  23. {
  24. yield return null;
  25. }
  26. if (needProgressBar)
  27. {
  28. yield return R.Ui.BlackScene.FadeBlack(0.3f, false);
  29. }
  30. this.Async.allowSceneActivation = true;
  31. yield return this.Async;
  32. base.StartCoroutine(this.Preload());
  33. this.IsLocked = false;
  34. if (data.ToId != -1)
  35. {
  36. this.Exit(this.FindGateData(data.ToId), player2Ground, data.OpenType);
  37. }
  38. else
  39. {
  40. SwitchLevelGateData data2 = new SwitchLevelGateData
  41. {
  42. SelfPosition = data.TargetPosition
  43. };
  44. this.Exit(data2, 0f, SceneGate.OpenType.None);
  45. }
  46. yield break;
  47. }
  48. public Coroutine Enter(SwitchLevelGateData data, bool needProgressBar = false)
  49. {
  50. EventManager.PostEvent<GameObject, PassGateEventArgs>("PassGate", base.gameObject, new PassGateEventArgs(PassGateEventArgs.PassGateStatus.Enter, data, LevelManager.SceneName));
  51. Log.Info(string.Format("从 Gate {1} 离开 {0}", LevelManager.SceneName, data.MyId));
  52. return base.StartCoroutine(this.EnterCoroutine(data, needProgressBar));
  53. }
  54. public Coroutine Exit(SwitchLevelGateData data, float groundDis, SceneGate.OpenType enterGateOpenType)
  55. {
  56. EventManager.PostEvent<GameObject, PassGateEventArgs>("PassGate", base.gameObject, new PassGateEventArgs(PassGateEventArgs.PassGateStatus.Exit, data, LevelManager.SceneName));
  57. Log.Info(string.Format("从 Gate {1} 进入 {0}", LevelManager.SceneName, data.MyId));
  58. return base.StartCoroutine(this.ExitCoroutine(data, groundDis, enterGateOpenType));
  59. }
  60. private IEnumerator EnterCoroutine(SwitchLevelGateData data, bool needProgressBar)
  61. {
  62. R.SceneData.CanAIRun = false;
  63. InputSetting.Stop(false);
  64. this.IsLocked = true;
  65. if (!needProgressBar)
  66. {
  67. yield return R.Ui.BlackScene.FadeBlack(0.3f, false);
  68. }
  69. this.IsLocked = false;
  70. yield return base.StartCoroutine(this.LoadLevelCoroutine(data, needProgressBar));
  71. yield break;
  72. }
  73. private IEnumerator ExitCoroutine(SwitchLevelGateData data, float player2Ground, SceneGate.OpenType enterGateOpenType)
  74. {
  75. this.IsLocked = true;
  76. Vector3 pos = R.Player.Transform.position;
  77. pos.x = data.SelfPosition.x;
  78. if (data.InAir)
  79. {
  80. pos.y = data.SelfPosition.y;
  81. }
  82. else
  83. {
  84. float distance = Physics2D.Raycast(data.SelfPosition, Vector2.down, 100f, LayerManager.GroundMask).distance;
  85. float num = data.SelfPosition.y - distance;
  86. pos.y = player2Ground + num;
  87. if (data.OpenType != SceneGate.OpenType.PressKey)
  88. {
  89. if (enterGateOpenType != SceneGate.OpenType.Left)
  90. {
  91. if (enterGateOpenType == SceneGate.OpenType.Right)
  92. {
  93. pos.x -= data.TriggerSize.x + 1f;
  94. }
  95. }
  96. else
  97. {
  98. pos.x += data.TriggerSize.x + 1f;
  99. }
  100. }
  101. }
  102. R.Player.Transform.position = pos;
  103. if (R.GameData.WindyVisiable && R.Windy != null)
  104. {
  105. if (data.OpenType == SceneGate.OpenType.Right)
  106. {
  107. R.Windy.transform.position = pos + new Vector3(-2f, 1f, 0f);
  108. }
  109. else if (data.OpenType == SceneGate.OpenType.Left)
  110. {
  111. R.Windy.transform.position = pos + new Vector3(2f, 1f, 0f);
  112. }
  113. else
  114. {
  115. R.Windy.transform.position = pos;
  116. }
  117. }
  118. if (!InputSetting.IsWorking())
  119. {
  120. InputSetting.Resume(false);
  121. }
  122. R.Camera.Controller.CameraResetPostionAfterSwitchScene();
  123. yield return new WaitForFixedUpdate();
  124. if (enterGateOpenType != SceneGate.OpenType.Left)
  125. {
  126. if (enterGateOpenType == SceneGate.OpenType.Right)
  127. {
  128. R.Player.ActionController.TurnRound(-1);
  129. }
  130. }
  131. else
  132. {
  133. R.Player.ActionController.TurnRound(1);
  134. }
  135. yield return R.Ui.BlackScene.FadeTransparent(0.3f, false);
  136. this.IsLocked = false;
  137. yield break;
  138. }
  139. public SwitchLevelGateData FindGateData(int id)
  140. {
  141. return this.FindGate(id).data;
  142. }
  143. public SceneGate FindGate(int id)
  144. {
  145. for (int i = 0; i < this.GatesInCurrentScene.Count; i++)
  146. {
  147. if (this.GatesInCurrentScene[i].data.MyId == id)
  148. {
  149. return this.GatesInCurrentScene[i];
  150. }
  151. }
  152. throw new NullReferenceException(string.Concat(new object[]
  153. {
  154. "No Gate in Scene ",
  155. LevelManager.SceneName,
  156. " id ",
  157. id
  158. }));
  159. }
  160. private IEnumerator Preload()
  161. {
  162. yield break;
  163. }
  164. public List<SceneGate> GatesInCurrentScene = new List<SceneGate>();
  165. public bool AllowSceneActivation = true;
  166. private AsyncOperation Async;
  167. public bool IsLocked;
  168. }