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(); 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(); } }