BootFlowController.cs 4.5 KB

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