private void Awake()
    {
        if (instance != null)
        {
            Destroy(gameObject);
        }
        else
        {
            instance      = this;
            defaultClip   = GetComponent <AudioSource>().clip;
            defaultVolume = GetComponent <AudioSource>().volume;

            DontDestroyOnLoad(gameObject);
        }
    }
Пример #2
0
        /// <summary>
        /// Initialize the game
        /// </summary>
        public GameMaster()
        {
            LogManager.InitLogFile();
            AssetManager.LoadAssets();
            ThemeMusic     = AssetManager.LoadMusic("ThemeMusic");
            IsMainMenuOpen = true;
            _window        = new RenderWindow(new VideoMode(Screenx, Screeny), AssetManager.GetMessage("GameTitle"), Styles.Close);
            _window.SetActive(true);
            _window.SetFramerateLimit(60);
            var mainMenu = new MainMenu(ref _window, AssetManager.LoadSound("ShiftThroughMenu"), AssetManager.LoadSound("ShiftThroughMenu"));

            _window.Closed      += WindowClosed;
            _window.LostFocus   += WindowLostFocus;
            _window.GainedFocus += WindowGainedFocus;
            IsGamePaused         = false;
            HasMainBeenDestroyed = false;
            var gameClock         = new Clock();
            var showSplash        = true;
            var splash            = new SplashScreen(ref _window);
            var splashDisplayTime = 0.0f;

            while (_window.IsOpen)
            {
                _window.DispatchEvents();
                _window.SetMouseCursorVisible(!_window.HasFocus());
                Delta = gameClock.Restart();
                if (showSplash)
                {
                    splash.Draw();
                    splashDisplayTime += Delta.AsSeconds();
                    if (splashDisplayTime >= SplashScreen.MaxDisplayTime)
                    {
                        showSplash = false;
                        splash.Stop();
                        ThemeMusic.Play();
                        IsThemePlaying = true;
                    }
                }
                else
                {
                    if (IsMainMenuOpen)
                    {
                        if (ThemeMusic != null && ThemeMusic.Status != SoundStatus.Playing)
                        {
                            ThemeMusic.Play();
                            IsThemePlaying = true;
                        }
                        mainMenu.Draw();
                    }
                    if (IsThemePlaying)
                    {
                        if (ThemeMusic != null && ThemeMusic.Status != SoundStatus.Playing)
                        {
                            ThemeMusic.Play();
                        }
                    }
                    else
                    {
                        if (ThemeMusic != null && ThemeMusic.Status == SoundStatus.Playing)
                        {
                            ThemeMusic.Stop();
                        }
                    }

                    if (IsGamePaused == false)
                    {
                        GameWorld?.Draw();
                    }
                    else if (IsGamePaused && IsPauseMenuCreated)
                    {
                        Pause();
                        _pauseMenu.Draw();
                    }
                    else if (IsGamePaused && IsPauseMenuCreated == false)
                    {
                        CreatePauseMenu();
                        IsPauseMenuCreated = true;
                    }
                    if (IsMainMenuOpen == false && HasMainBeenDestroyed == false)
                    {
                        mainMenu.DestroyMenu();
                        HasMainBeenDestroyed = true;
                    }
                }
                _window.Display();
            }
        }