| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- using System.Collections;
- using UnityEngine;
- using UnityEngine.Events;
- [DisallowMultipleComponent]
- public class BootFlowController : MonoBehaviour
- {
- [Header("Views")]
- [SerializeField] LoadingScreenView loadingView;
- [SerializeField] StartScreenView startView;
- [SerializeField] LobbyScreenView lobbyView;
- [SerializeField] MainMenuController menuController;
- [Header("Loading")]
- [SerializeField] float loadingDuration = 1.4f;
- [SerializeField] float holdAtFull = 0.32f;
- [SerializeField] float loadingFadeOutDuration = 0.35f;
- [SerializeField] bool useRandomStep = true;
- [SerializeField] float stepInterval = 0.09f;
- [SerializeField] Vector2 randomStepRange = new Vector2(0.04f, 0.13f);
- [Header("Events")]
- [SerializeField] UnityEvent onGameStart;
- Coroutine bootRoutine;
- bool returningFromGame;
- void Awake()
- {
- if (menuController == null)
- menuController = GetComponent<MainMenuController>();
- AdMobService.Initialize();
- returningFromGame = GameFlowState.ConsumeReturningToLobby();
- if (startView != null)
- startView.HideImmediate();
- if (lobbyView != null)
- lobbyView.Hide();
- if (menuController != null)
- menuController.CloseOverlays();
- if (returningFromGame)
- {
- if (loadingView != null)
- loadingView.Hide();
- return;
- }
- if (loadingView != null)
- {
- loadingView.Show();
- loadingView.SetProgress(0f);
- }
- }
- void OnEnable()
- {
- if (startView != null)
- startView.StartRequested += HandleStartRequested;
- }
- void OnDisable()
- {
- if (startView != null)
- startView.StartRequested -= HandleStartRequested;
- }
- async void Start()
- {
- AppRuntimesEvents.OnPlatformOpen += () =>
- {
- GameAudio.StopBgmImmediate();
- };
-
- if (returningFromGame)
- {
- if (menuController != null)
- menuController.ShowLobby();
- else if (lobbyView != null)
- lobbyView.Show();
- AdMobService.ShowBottomBanner();
- return;
- }
- bootRoutine = StartCoroutine(RunBootSequence());
- }
- IEnumerator RunBootSequence()
- {
- if (loadingView == null || startView == null)
- yield break;
- loadingView.Show();
- loadingView.SetProgress(0f);
- if (useRandomStep)
- {
- float progress = 0f;
- while (progress < 1f)
- {
- progress = Mathf.Min(1f, progress + Random.Range(randomStepRange.x, randomStepRange.y));
- loadingView.SetProgress(progress);
- yield return new WaitForSecondsRealtime(stepInterval);
- }
- }
- else
- {
- float elapsed = 0f;
- float duration = Mathf.Max(0.01f, loadingDuration);
- while (elapsed < duration)
- {
- elapsed += Time.unscaledDeltaTime;
- loadingView.SetProgress(elapsed / duration);
- yield return null;
- }
- loadingView.SetProgress(1f);
- }
- if (holdAtFull > 0f)
- yield return new WaitForSecondsRealtime(holdAtFull);
- startView.ShowImmediate(false);
- if (loadingFadeOutDuration > 0f)
- yield return loadingView.FadeOutAndHide(loadingFadeOutDuration);
- else
- loadingView.Hide();
- yield return WaitForAppOpenGate();
- startView.SetInteractable(true);
- bootRoutine = null;
- }
- IEnumerator WaitForAppOpenGate()
- {
- bool done = false;
- AdMobService.TryShowAppOpenLaunch(() => done = true);
- while (!done)
- yield return null;
- }
- void HandleStartRequested()
- {
- SaveService.EnsureLoaded();
- if (SaveService.Data.story == 0 && menuController != null)
- {
- if (startView != null)
- startView.SetInteractable(false);
- if (menuController.PlayIntro(FinishStartFlow))
- {
- Debug.Log("[BootFlow] GAME START clicked. Playing the opening story.");
- return;
- }
- }
- Debug.Log("[BootFlow] GAME START clicked. Opening the lobby.");
- FinishStartFlow();
- }
- void FinishStartFlow()
- {
- if (startView != null)
- startView.HideImmediate();
- if (menuController != null)
- menuController.ShowLobby();
- else if (lobbyView != null)
- lobbyView.Show();
- AdMobService.ShowBottomBanner();
- onGameStart?.Invoke();
- }
- }
|