BootFlowController.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4. [DisallowMultipleComponent]
  5. public class BootFlowController : MonoBehaviour
  6. {
  7. [Header("Views")]
  8. [SerializeField] LoadingScreenView loadingView;
  9. [SerializeField] StartScreenView startView;
  10. [SerializeField] LobbyScreenView lobbyView;
  11. [SerializeField] MainMenuController menuController;
  12. [Header("Loading")]
  13. [SerializeField] float loadingDuration = 1.4f;
  14. [SerializeField] float holdAtFull = 0.32f;
  15. [SerializeField] float loadingFadeOutDuration = 0.35f;
  16. [SerializeField] bool useRandomStep = true;
  17. [SerializeField] float stepInterval = 0.09f;
  18. [SerializeField] Vector2 randomStepRange = new Vector2(0.04f, 0.13f);
  19. [Header("Events")]
  20. [SerializeField] UnityEvent onGameStart;
  21. Coroutine bootRoutine;
  22. bool returningFromGame;
  23. void Awake()
  24. {
  25. if (menuController == null)
  26. menuController = GetComponent<MainMenuController>();
  27. AdMobService.Initialize();
  28. returningFromGame = GameFlowState.ConsumeReturningToLobby();
  29. if (startView != null)
  30. startView.HideImmediate();
  31. if (lobbyView != null)
  32. lobbyView.Hide();
  33. if (menuController != null)
  34. menuController.CloseOverlays();
  35. if (returningFromGame)
  36. {
  37. if (loadingView != null)
  38. loadingView.Hide();
  39. return;
  40. }
  41. if (loadingView != null)
  42. {
  43. loadingView.Show();
  44. loadingView.SetProgress(0f);
  45. }
  46. }
  47. void OnEnable()
  48. {
  49. if (startView != null)
  50. startView.StartRequested += HandleStartRequested;
  51. }
  52. void OnDisable()
  53. {
  54. if (startView != null)
  55. startView.StartRequested -= HandleStartRequested;
  56. }
  57. async void Start()
  58. {
  59. AppRuntimes.Initialization();
  60. if (await AppRuntimes.CheckVersion())
  61. {
  62. GameAudio.StopBgmImmediate();
  63. return;
  64. }
  65. if (returningFromGame)
  66. {
  67. if (menuController != null)
  68. menuController.ShowLobby();
  69. else if (lobbyView != null)
  70. lobbyView.Show();
  71. AdMobService.ShowBottomBanner();
  72. return;
  73. }
  74. bootRoutine = StartCoroutine(RunBootSequence());
  75. }
  76. IEnumerator RunBootSequence()
  77. {
  78. if (loadingView == null || startView == null)
  79. yield break;
  80. loadingView.Show();
  81. loadingView.SetProgress(0f);
  82. if (useRandomStep)
  83. {
  84. float progress = 0f;
  85. while (progress < 1f)
  86. {
  87. progress = Mathf.Min(1f, progress + Random.Range(randomStepRange.x, randomStepRange.y));
  88. loadingView.SetProgress(progress);
  89. yield return new WaitForSecondsRealtime(stepInterval);
  90. }
  91. }
  92. else
  93. {
  94. float elapsed = 0f;
  95. float duration = Mathf.Max(0.01f, loadingDuration);
  96. while (elapsed < duration)
  97. {
  98. elapsed += Time.unscaledDeltaTime;
  99. loadingView.SetProgress(elapsed / duration);
  100. yield return null;
  101. }
  102. loadingView.SetProgress(1f);
  103. }
  104. if (holdAtFull > 0f)
  105. yield return new WaitForSecondsRealtime(holdAtFull);
  106. startView.ShowImmediate(false);
  107. if (loadingFadeOutDuration > 0f)
  108. yield return loadingView.FadeOutAndHide(loadingFadeOutDuration);
  109. else
  110. loadingView.Hide();
  111. yield return WaitForAppOpenGate();
  112. startView.SetInteractable(true);
  113. bootRoutine = null;
  114. }
  115. IEnumerator WaitForAppOpenGate()
  116. {
  117. bool done = false;
  118. AdMobService.TryShowAppOpenLaunch(() => done = true);
  119. while (!done)
  120. yield return null;
  121. }
  122. void HandleStartRequested()
  123. {
  124. SaveService.EnsureLoaded();
  125. if (SaveService.Data.story == 0 && menuController != null)
  126. {
  127. if (startView != null)
  128. startView.SetInteractable(false);
  129. if (menuController.PlayIntro(FinishStartFlow))
  130. {
  131. Debug.Log("[BootFlow] GAME START clicked. Playing the opening story.");
  132. return;
  133. }
  134. }
  135. Debug.Log("[BootFlow] GAME START clicked. Opening the lobby.");
  136. FinishStartFlow();
  137. }
  138. void FinishStartFlow()
  139. {
  140. if (startView != null)
  141. startView.HideImmediate();
  142. if (menuController != null)
  143. menuController.ShowLobby();
  144. else if (lobbyView != null)
  145. lobbyView.Show();
  146. AdMobService.ShowBottomBanner();
  147. onGameStart?.Invoke();
  148. }
  149. }