Exemplo n.º 1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            float elapsedTime = (float)gameTime.ElapsedGameTime.Milliseconds / 1000.0f;

            Input.GetInstance().Update();
            Fader.GetInstance().Update(elapsedTime);

            KeyboardState keyboardState   = Keyboard.GetState();
            GamePadState  gamePadOneState = GamePad.GetState(PlayerIndex.One);

            // Menu music playback handling:
            // Play music in mainmenu, credits and info
            // pause for start or gameplay
            switch (this.currentGameState)
            {
            case GameState.Mainmenu:
            case GameState.Info:
            case GameState.Credits:
                this.mainMenu.PlayMusic();
                break;

            case GameState.Start:
                this.mainMenu.PauseMusic();
                break;
            }

            // Allows the game to exit
            if (Input.GetInstance().IsPressed(Buttons.Back) ||
                (Input.GetInstance().IsPressed(Keys.Escape)))
            {
                //if (this.currentGameState != GameState.Start)
                {
                    // If not in the MainMenu go there else goto Quit.
                    if (this.currentGameState != GameState.Mainmenu && this.currentGameState != GameState.Quit)
                    {
                        this.currentGameState = GameState.Mainmenu;
                        if (this.mapper != null)
                        {
                            Fader.GetInstance().Add(Fader.Fade.In, 1.0f);
                            this.mapper.Reset();
                            this.mapper = null;
                        }
                    }
                    else
                    {
                        this.currentGameState = GameState.Quit;
                    }
                }
            }

            switch (this.currentGameState)
            {
            case GameState.Intro:
                this.gameIntro.Update(gameTime);
                if (this.gameIntro.Finished)
                {
                    Fader.GetInstance().Add(Fader.Fade.In, 1.0f);
                    this.gameIntro        = null;               // Shown it, not needed anymore
                    this.currentGameState = GameState.Mainmenu; // and goto main menu.
                }
                break;

            case GameState.Mainmenu:
                this.currentGameState = this.mainMenu.Update(gameTime, this.currentGameState);
                break;

            case GameState.Start:

                // Update StageChanger.
                if (this.mapper == null)
                {
                    this.mapper = new Mapper(this, XMLHandler.LoadXML <MapperData>(@"stages.xml"));
                }
                else
                {
                    this.mapper.Update(gameTime);


                    if (this.mapper.GameCompleted)
                    {       // All game stages completed. Game finished. Last videos and cutscenes played. Goto Credits.
                        this.mapper.Reset();
                        this.currentGameState = GameState.Credits;
                    }

                    if (this.mapper.Quitted)
                    {       // Quitted from the gameplay or Map. goto mainmenu.
                        this.mapper.Reset();
                        this.currentGameState = GameState.Mainmenu;
                    }
                }

                break;

            case GameState.Info:
                // Info or instructions goes here:
                if (this.info == null)
                {
                    this.info = new Credits(this, "info");
                }
                if (this.info.Update(gameTime))
                {
                    this.currentGameState = GameState.Mainmenu;
                    this.info             = null;
                }
                break;

            case GameState.Credits:
                // Credits goes here:
                if (credits == null)
                {
                    credits = new Credits(this, "credits");
                }
                if (this.credits.Update(gameTime))
                {
                    this.currentGameState = GameState.Mainmenu;
                    credits = null;
                }
                break;

            case GameState.Quit:
                // Exiting the game.
                if (!this.quitFadeOutSet)
                {
                    this.quitFadeOutSet = true;
                    Fader.GetInstance().Add(Fader.Fade.Out, 1.0f);
                }
                if (Fader.GetInstance().IsFadeFinished())
                {
                    this.Exit();
                }
                break;
            }

            base.Update(gameTime);
        }
Exemplo n.º 2
0
        public void Update(GameTime gameTime)
        {
            KeyboardState keyboardState   = Keyboard.GetState();
            GamePadState  gamePadOneState = GamePad.GetState(PlayerIndex.One);
            float         elapsedTime     = (float)gameTime.ElapsedGameTime.Milliseconds / 1000.0f;

            // All game levels completed and cutscene videos played. Show total game time and exit the game stage changer.
            if (this.stagesCompleted)
            {
                // Add Fade In and show the finishing aspects.
                if (!this.stagesCompletedFadeInStarted)
                {
                    this.stagesCompletedFadeInStarted = true;
                    Fader.GetInstance().Add(Fader.Fade.In, 1);
                }

                // Finish the whole game and return to credits/main menu after
                // space is pressed or timer triggered.
                if (this.gameCompletionTimer.GetIsTriggered(elapsedTime) ||
                    Input.GetInstance().IsPressed(Keys.Space))
                {
                    this.gameCompleted = true;
                }
                return;
            }


            // If both map and cutsceneVideoFile are null load the currentStage to get things running
            if (this.map == null && this.cutsceneVideoFile == null)
            {
                if (this.currentStage < this.mapperData.stages.Length)
                { // Uncompleted stages left, load next:
                    LoadStage(false);
                }
                else
                { // All stages completed, set bool.
                    this.stagesCompleted = true;
                }
            }
            else
            {
                // MAP RELATED
                if (this.map != null)
                {
                    // Allows the game to exit
                    if (!map.Player.Alive && (keyboardState.IsKeyDown(Keys.Q) || (gamePadOneState.IsButtonDown(Buttons.Back) && gamePadOneState.IsButtonUp(Buttons.Back))))
                    {
                        this.quitted = true;
                    }

                    // Reset the current stage when showing the death screen and player presses Y-button/key
                    if (!map.Player.Alive && (keyboardState.IsKeyDown(Keys.Y) || gamePadOneState.IsButtonDown(Buttons.Y)))
                    {
                        LoadStage(false);
                    }

                    // Pausing and resuming the gameplay
                    if (map.Player.Alive)
                    {
                        if (Input.GetInstance().IsPressed(Keys.P) || Input.GetInstance().IsPressed(Buttons.Start))
                        {
                            if (map.Paused)
                            {
                                map.Resume();
                            }
                            else
                            {
                                map.Pause();
                            }
                        }
                    }

                    // If the map is completed / end condition(s) reached load next stage.
                    if (this.map.Completed)
                    {
                        if (this.totalGameTimeUpdated == false)
                        {
                            this.totalGameTime       += this.map.CompletionTime;
                            this.totalGameTimeUpdated = true;
                            this.stageEndFadeOut      = new Timer(stageChangerTimer.TriggerTime - 1.0f, false);
                        }
                        this.map.Player.Enabled = false;

                        if (this.stageEndFadeOut.GetIsTriggered(elapsedTime))
                        {
                            Fader.GetInstance().Add(Fader.Fade.Out, 1);
                        }

                        if (stageChangerTimer.GetIsTriggered(elapsedTime))
                        {
                            stageChangerTimer.Reset();
                            this.LoadStage(true);
                        }
                    }


                    // Map update - the game logics do their thingies!
                    if (this.map != null)
                    {
                        this.map.Update(gameTime);
                    }
                } // MAP RELATED ENDS

                // CUTSCENE VIDEO PLAYBACK RELATED STARTS
                if (this.cutsceneVideoFile != null)
                {
                    if (this.videoPlayer.State == MediaState.Stopped)
                    {
                        Console.WriteLine("Video playback stopped, load next stage.");
                        this.cutsceneVideoFile = null;
                        this.LoadStage(true);
                    }
                    if (this.videoPlayer.State == MediaState.Playing)
                    {
                        if (Input.GetInstance().IsPressed(Keys.Space) ||
                            Input.GetInstance().IsPressed(Buttons.A))
                        {
                            if (this.cutsceneSkipAttempt)
                            {
                                // Skipping the current video and moving to next stage.
                                this.cutsceneSkipAttempt = false;
                                this.videoPlayer.Stop(); // Stoping the video will move us forward in the stage changer.
                                this.cutsceneSkipAttemptTimer.Reset();
                            }
                            else
                            {
                                // First skip attempt shows only the text for skipping.
                                this.cutsceneSkipAttempt = true;
                                this.cutsceneSkipAttemptTimer.Reset();
                            }
                        }
                        if (this.cutsceneSkipAttemptTimer.GetIsTriggered(elapsedTime) && this.cutsceneSkipAttempt)
                        { // Hide the skipping text if the skip-key is not pressed during the time period.
                            this.cutsceneSkipAttempt = false;
                            this.cutsceneSkipAttemptTimer.Reset();
                        }
                    }
                }
                // CUTSCENE VIDEO PLAYBACK RELATED ENDS
            }



            // Reset the current stage
            if (keyboardState.IsKeyDown(Keys.R) || gamePadOneState.IsButtonDown(Buttons.BigButton))
            {
                LoadStage(false);
            }
        }