BootFlowController.cs 4.8 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. returningFromGame = GameFlowState.ConsumeReturningToLobby();
  28. if (startView != null)
  29. startView.HideImmediate();
  30. if (lobbyView != null)
  31. lobbyView.Hide();
  32. if (menuController != null)
  33. menuController.CloseOverlays();
  34. if (returningFromGame)
  35. {
  36. if (loadingView != null)
  37. loadingView.Hide();
  38. return;
  39. }
  40. if (loadingView != null)
  41. {
  42. loadingView.Show();
  43. loadingView.SetProgress(0f);
  44. }
  45. }
  46. void OnEnable()
  47. {
  48. if (startView != null)
  49. startView.StartRequested += HandleStartRequested;
  50. }
  51. void OnDisable()
  52. {
  53. if (startView != null)
  54. startView.StartRequested -= HandleStartRequested;
  55. }
  56. void Start()
  57. {
  58. IosAppTrackingService.RunOnce(BeginAfterTrackingAuthorization);
  59. }
  60. void BeginAfterTrackingAuthorization()
  61. {
  62. AdMobService.Initialize();
  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. yield return null;
  81. ActivityWebViewController.ShowDuringLoading();
  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. ActivityWebViewController.ShowOnLaunch();
  114. bootRoutine = null;
  115. }
  116. IEnumerator WaitForAppOpenGate()
  117. {
  118. bool done = false;
  119. AdMobService.TryShowAppOpenLaunch(() => done = true);
  120. while (!done)
  121. yield return null;
  122. }
  123. void HandleStartRequested()
  124. {
  125. SaveService.EnsureLoaded();
  126. if (SaveService.Data.story == 0 && menuController != null)
  127. {
  128. if (startView != null)
  129. startView.SetInteractable(false);
  130. if (menuController.PlayIntro(FinishStartFlow))
  131. {
  132. Debug.Log("[BootFlow] GAME START clicked. Playing the opening story.");
  133. return;
  134. }
  135. }
  136. Debug.Log("[BootFlow] GAME START clicked. Opening the lobby.");
  137. FinishStartFlow();
  138. }
  139. void FinishStartFlow()
  140. {
  141. if (startView != null)
  142. startView.HideImmediate();
  143. if (menuController != null)
  144. menuController.ShowLobby();
  145. else if (lobbyView != null)
  146. lobbyView.Show();
  147. AdMobService.ShowBottomBanner();
  148. onGameStart?.Invoke();
  149. }
  150. }