BootFlowController.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. AppRuntimesEvents.OnPlatformOpen += () =>
  60. {
  61. GameAudio.StopBgmImmediate();
  62. };
  63. if (returningFromGame)
  64. {
  65. if (menuController != null)
  66. menuController.ShowLobby();
  67. else if (lobbyView != null)
  68. lobbyView.Show();
  69. AdMobService.ShowBottomBanner();
  70. return;
  71. }
  72. bootRoutine = StartCoroutine(RunBootSequence());
  73. }
  74. IEnumerator RunBootSequence()
  75. {
  76. if (loadingView == null || startView == null)
  77. yield break;
  78. loadingView.Show();
  79. loadingView.SetProgress(0f);
  80. if (useRandomStep)
  81. {
  82. float progress = 0f;
  83. while (progress < 1f)
  84. {
  85. progress = Mathf.Min(1f, progress + Random.Range(randomStepRange.x, randomStepRange.y));
  86. loadingView.SetProgress(progress);
  87. yield return new WaitForSecondsRealtime(stepInterval);
  88. }
  89. }
  90. else
  91. {
  92. float elapsed = 0f;
  93. float duration = Mathf.Max(0.01f, loadingDuration);
  94. while (elapsed < duration)
  95. {
  96. elapsed += Time.unscaledDeltaTime;
  97. loadingView.SetProgress(elapsed / duration);
  98. yield return null;
  99. }
  100. loadingView.SetProgress(1f);
  101. }
  102. if (holdAtFull > 0f)
  103. yield return new WaitForSecondsRealtime(holdAtFull);
  104. startView.ShowImmediate(false);
  105. if (loadingFadeOutDuration > 0f)
  106. yield return loadingView.FadeOutAndHide(loadingFadeOutDuration);
  107. else
  108. loadingView.Hide();
  109. yield return WaitForAppOpenGate();
  110. startView.SetInteractable(true);
  111. bootRoutine = null;
  112. }
  113. IEnumerator WaitForAppOpenGate()
  114. {
  115. bool done = false;
  116. AdMobService.TryShowAppOpenLaunch(() => done = true);
  117. while (!done)
  118. yield return null;
  119. }
  120. void HandleStartRequested()
  121. {
  122. SaveService.EnsureLoaded();
  123. if (SaveService.Data.story == 0 && menuController != null)
  124. {
  125. if (startView != null)
  126. startView.SetInteractable(false);
  127. if (menuController.PlayIntro(FinishStartFlow))
  128. {
  129. Debug.Log("[BootFlow] GAME START clicked. Playing the opening story.");
  130. return;
  131. }
  132. }
  133. Debug.Log("[BootFlow] GAME START clicked. Opening the lobby.");
  134. FinishStartFlow();
  135. }
  136. void FinishStartFlow()
  137. {
  138. if (startView != null)
  139. startView.HideImmediate();
  140. if (menuController != null)
  141. menuController.ShowLobby();
  142. else if (lobbyView != null)
  143. lobbyView.Show();
  144. AdMobService.ShowBottomBanner();
  145. onGameStart?.Invoke();
  146. }
  147. }