Exemplo n.º 1
0
    /// <summary>
    /// Updates the initialization process.
    /// </summary>
    private void UpdateInitState()
    {
        switch (m_initState)
        {
        case InitState.NONE:
            AdvanceInitState();
            break;

        case InitState.INIT_SHARED_SYSTEMS:
#if SHOW_STATE_LOGS
            Debug.Log("Initializing shared systems...");
#endif
            // Find or create containers for shared MonoBehaviour systems
            GameObject uiManagerObj = GameObject.Find(UI_MANAGER_OBJ_NAME);
            if (uiManagerObj == null)
            {
                uiManagerObj = new GameObject(UI_MANAGER_OBJ_NAME);
            }
            GameObject soundManagerObj = GameObject.Find(SOUND_MANAGER_OBJ_NAME);
            if (soundManagerObj == null)
            {
                soundManagerObj = new GameObject(SOUND_MANAGER_OBJ_NAME);
            }

            // Create and store references to shared systems
            m_uiManager          = uiManagerObj.AddComponentNoDupe <UIManager>();
            m_soundManager       = soundManagerObj.AddComponentNoDupe <SoundManager>();
            m_dataSystem         = new SoomlaDataSystem();
            m_notifSystem        = new NotificationSystem();
            m_playServicesSystem = new PlayServicesSystem();

            // Shared systems are not destroyed on scene switches
            DontDestroyOnLoad(uiManagerObj);
            DontDestroyOnLoad(soundManagerObj);

            // Initialize shared systems
            m_uiManager.Initialize();
            m_soundManager.Initialize();
            m_dataSystem.Initialize();
            m_playServicesSystem.Initialize(m_enableSavedGames);
            m_notifSystem.Initialize(m_enablePushNotifs);

            // Add FPSDisplay component
            m_fpsDisplay = this.gameObject.AddComponentNoDupe <FPSDisplay>();

            AdvanceInitState();
            break;

        case InitState.WAIT_INIT_SHARED_SYSTEMS:
#if SHOW_STATE_LOGS
            Debug.Log("Waiting for shared system initialization to finish...");
#endif
            // Wait for shared systems to finish initialization
            if (m_uiManager.IsInitialized &&
                m_soundManager.IsInitialized &&
                m_dataSystem.IsInitialized &&
                m_playServicesSystem.IsInitialized &&
                m_notifSystem.IsInitialized)
            {
                AdvanceInitState();
            }
            break;

        case InitState.INIT_LOCATOR:
#if SHOW_STATE_LOGS
            Debug.Log("Initializing Service Locator...");
#endif
            Locator.ProvideUIManager(m_uiManager);
            Locator.ProvideSoundManager(m_soundManager);
            Locator.ProvideDataSystem(m_dataSystem);
            Locator.ProvidePlayServicesSystem(m_playServicesSystem);
            Locator.ProvideNotifSystem(m_notifSystem);

            // If game is loaded from the Main scene (index 0)
            if (Application.loadedLevel == 0)
            {
                AdvanceInitState();
            }
            // If game is loaded from a different scene
            else
            {
                // No need to load a different scene
                // Just load the SceneMaster
                m_initState = InitState.LOAD_SCENE_MASTER;
            }
            break;

        // The following states are cycled through whenever scenes are switched
        case InitState.LOAD_SCENE:
#if SHOW_STATE_LOGS
            Debug.Log("Loading scene...");
#endif
            // Block input while next scene is loading
            m_uiManager.SetBlockInput();

            StartLoading(m_sceneInfo.GetSceneNameOf(m_nextScene));

            AdvanceInitState();
            break;

        case InitState.WAIT_LOAD_SCENE:
            // Wait for scene to finish loading in the background
#if SHOW_STATE_LOGS
            Debug.Log("Waiting for scene to load in the background...");
#endif
            if (m_async != null && m_async.progress >= READY_TO_LOAD_PROGRESS)
            {
                // Start fade out
                if (m_enableFadeAnim)
                {
                    // There is nothing to fade out if starting from Main scene
                    if (Application.loadedLevel != 0)
                    {
                        m_uiManager.StartFadeOut(true);
                    }
                }

                AdvanceInitState();
            }
            break;

        case InitState.UNLOAD_CUR_SCENE:
#if SHOW_STATE_LOGS
            Debug.Log("Unloading current scene...");
#endif
            // If starting from Main scene, there will be nothing to unload
            if (Application.loadedLevel == 0 || m_sceneMaster.Unload())
            {
                AdvanceInitState();
            }
            break;

        case InitState.WAIT_UNLOAD_CUR_SCENE:
#if SHOW_STATE_LOGS
            Debug.Log("Waiting for current scene to finish unloading...");
#endif
            // If starting from Main scene, there will be nothing to unload
            if (Application.loadedLevel == 0 || !m_sceneMaster.IsInitialized)
            {
                // If scene fading is enabled, wait for scene to fade out first
                if (!m_enableFadeAnim ||
                    (m_enableFadeAnim && m_uiManager.IsFadedOut()))
                {
                    // Clean up non-persistent sounds
                    m_soundManager.DeleteAllSoundObjects(false);

                    AdvanceInitState();
                }
            }
            break;

        case InitState.SWITCH_SCENE:
            // Load the next scene
#if SHOW_STATE_LOGS
            Debug.Log("Switching scene...");
#endif
            ActivateScene();
            // Initialization will continue in OnLevelWasLoaded
            break;

        case InitState.LOAD_SCENE_MASTER:
#if SHOW_STATE_LOGS
            Debug.Log("Loading scene master in scene " + Application.loadedLevelName);
#endif
            if (m_sceneMaster.Load())
            {
                // Provide SceneMaster to the service locator
                Locator.ProvideSceneMaster(m_sceneMaster);

                AdvanceInitState();
            }
            break;

        case InitState.WAIT_LOAD_SCENE_MASTER:
#if SHOW_STATE_LOGS
            Debug.Log("Waiting for scene master to load...");
#endif
            if (m_sceneMaster.IsInitialized)
            {
                // Start fade in
                if (m_enableFadeAnim)
                {
                    m_uiManager.StartFadeIn();
                }

                // Fire the SceneInitialized event
                if (sceneInitializedInvoker != null)
                {
                    sceneInitializedInvoker(this, new System.EventArgs());
                }

                AdvanceInitState();
            }
            break;

        case InitState.DONE:
#if SHOW_STATE_LOGS
            if (BuildInfo.IsDebugMode)
            {
                Debug.Log("Main initialization complete");
            }
#endif
            // Switch to IDLE state
            // If the SceneMaster switches the scene, this state change will be overridden
            AdvanceInitState();

            // Keep track of previous scene
            m_prevScene = m_curScene;
            // Update scene enum for the current scene
            m_curScene = m_nextScene;
            // Start the scene - pass control over to the active scene master
            m_sceneMaster.StartScene();
            break;

        case InitState.IDLE:
            break;
        }
    }