예제 #1
0
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit when pressing End
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
                Keyboard.GetState().IsKeyDown(Keys.End))
            {
                this.Exit();
            }

            // Fullscreen
            if (Keyboard.GetState().IsKeyDown(Keys.F11))
            {
                graphics.ToggleFullScreen();
            }


            // If game over
            if (game.GameOver)
            {
                // The saving score will be the current score
                saveFile.PlayerScore = game.Score;
                // If game over and in play page (This will run only once after a game over)
                if (game.GameOver && menu.PageSelection == 1)
                {
                    // Save score into highscore list
                    saveFile.SaveHighScore();
                    // Change page to game over screen
                    menu.PageSelection = 6;
                    // Update the score text in game over page
                    menu.Pages[6].Text[1].UpdateLine("Score: " + saveFile.PlayerScore);
                }
            }


            // A small bool to make the full variable a bit smaller
            bool isKeyUp = input.IsKeyUp;

            // Update the variables in control scheme (input)
            input.Update();
            // If player is not in play screen/page, make menu navigation possible
            if (isKeyUp && menu.PageSelection != 1)
            {
                menu.Navigation(input.Up, input.Down, input.Left, input.Right);
            }
            // If mouse pointer does not move, then do not check if mouse is intersecting with menu buttons
            if (input.MousePointer != input.PreviousMousePosition)
            {
                menu.UpdateMouse(input.MousePointer);
            }



            // A region of all the pages and what each button do
            #region All Menus

            // A safety function that checks if player is outside the play screen and then pauses the game
            if (menu.PageSelection != 1)
            {
                game.GameActive = false;
            }


            // Main Menu
            // Single activation on select key and mouse button
            if (isKeyUp && input.Select || isKeyUp && input.SelectMouse)
            {
                // Check if player select first button in main menu (play)
                if (menu.State(0, 0))
                {
                    // Change page to Play screen
                    menu.PageSelection = 1;
                    // Start game
                    initializeGame = true;
                    // Make single activation key reset
                    isKeyUp = false;
                }

                // Highscore
                if (menu.State(0, 1))
                {
                    menu.PageSelection = 2;
                    isKeyUp            = false;
                }

                // Options
                if (menu.State(0, 2))
                {
                    menu.PageSelection = 3;
                    isKeyUp            = false;
                }

                // How to play
                if (menu.State(0, 3))
                {
                    menu.PageSelection = 4;
                    isKeyUp            = false;
                }

                // Credits
                if (menu.State(0, 4))
                {
                    menu.PageSelection = 5;
                    isKeyUp            = false;
                }

                // Exit
                if (menu.State(0, 5))
                {
                    // Exit the game
                    this.Exit();
                }
            }

            // Play
            // If pause (Back) button is activated, or if mouse presses on pause button in the left top corner
            if (isKeyUp && input.Back || isKeyUp && input.SelectMouse && input.MousePointer.Intersects(pauseButton))
            {
                // Only if game is active (you cannot pause when the player see a countdown)
                if (game.GameActive)
                {
                    // Move to pause menu
                    menu.PageSelection = 7;
                    // Pause game
                    game.PauseGame();
                    // Reset single activation key
                    isKeyUp = false;
                }
            }

            // HighScore
            if (isKeyUp && input.Select || isKeyUp && input.SelectMouse)
            {
                // Back
                if (menu.State(2, 0))
                {
                    menu.PageSelection = 0;
                    isKeyUp            = false;
                }
            }
            // If Left shift and R is pressed while in Highscore page, clean/reset the score board
            if (menu.PageSelection == 2 && Keyboard.GetState().IsKeyDown(Keys.LeftShift) && Keyboard.GetState().IsKeyDown(Keys.R))
            {
                saveFile.ResetHighScore();
            }

            // Options (Select with input.select)
            if (isKeyUp && input.Select || isKeyUp && input.SelectMouse)
            {
                // Controls
                if (menu.State(3, 0))
                {
                    // If button gets pressed (with selection key/mouse button), change button state, loop it so that it doesn't get stuck
                    menu.Pages[3].Buttons[0].SelectRight(true);
                    // Reset single activation key
                    isKeyUp = false;
                }

                // Speed
                if (menu.State(3, 1))
                {
                    // If button gets pressed (with selection key/mouse button), add 0.1f speed
                    options.Speed += 0.1f;
                    // If it gets over 2f, change it to 0.2f
                    if (options.Speed > 2.01f)
                    {
                        options.Speed = 0.2f;
                    }
                    // Reset single activation key
                    isKeyUp = false;
                }

                // Fullscreen
                if (menu.State(3, 2))
                {
                    // Toggle fullscreen
                    graphics.ToggleFullScreen();
                    // Reset single activation key
                    isKeyUp = false;
                }

                // Back
                if (menu.State(3, 3))
                {
                    menu.PageSelection = 0;
                    isKeyUp            = false;
                }
            }

            // Options Change State with arrows
            if (isKeyUp)
            {
                // Control Settings
                // Checks what state the button is in (Switching button state with left/right keys is in menu.navigation method)
                if (menu.State(3, 0, 0))
                {
                    // If button state = 0, make options hold false
                    options.ControlsHold = false;
                }
                if (menu.State(3, 0, 1))
                {
                    // If button state = 1, make options hold true
                    options.ControlsHold = true;
                }

                // Speed Settings
                // Modified button
                // Checks if button is selected
                if (menu.State(3, 1))
                {
                    // If left is pressed and is more than 0.2f
                    if (input.Left && options.Speed > 0.2f)
                    {
                        // Subtract speed with 0.1f
                        options.Speed -= 0.1f;
                    }

                    // If right is pressed and is less than 2f
                    if (input.Right && options.Speed < 2f)
                    {
                        // Add speed with 0.1f
                        options.Speed += 0.1f;
                    }
                }
            }

            // HowToPlay
            if (isKeyUp && input.Select || isKeyUp && input.SelectMouse)
            {
                // Back
                if (menu.State(4, 0))
                {
                    menu.PageSelection = 0;
                    isKeyUp            = false;
                }
            }

            // Credits
            if (isKeyUp && input.Select || isKeyUp && input.SelectMouse)
            {
                // Back
                if (menu.State(5, 0))
                {
                    menu.PageSelection = 0;
                    isKeyUp            = false;
                }
            }

            // GameOver
            if (isKeyUp && input.Select || isKeyUp && input.SelectMouse)
            {
                // Highscore
                if (menu.State(6, 0))
                {
                    // Move to highscore page
                    menu.PageSelection = 2;
                    // Reset game after game over
                    game.ResetGame();
                    // Reset single activation key
                    isKeyUp = false;
                }

                // Back
                if (menu.State(6, 1))
                {
                    // Move to main menu
                    menu.PageSelection = 0;
                    // Reset game after game over
                    game.ResetGame();
                    // Reset single activation key
                    isKeyUp = false;
                }
            }

            // Pause
            if (isKeyUp && input.Select || isKeyUp && input.SelectMouse)
            {
                // Resume
                if (menu.State(7, 0))
                {
                    // Move back to play screen
                    menu.PageSelection = 1;
                    // Start game again
                    initializeGame = true;
                    // Reset single activation key
                    isKeyUp = false;
                }

                // Reset
                if (menu.State(7, 1))
                {
                    // Move back to play screen
                    menu.PageSelection = 1;
                    // Reset game
                    game.ResetGame();
                    // Start game again
                    initializeGame = true;
                    // Reset single activation key
                    isKeyUp = false;
                }

                // Back to Menu
                if (menu.State(7, 2))
                {
                    // Move back to main menu
                    menu.PageSelection = 0;
                    // Reset single activation key
                    isKeyUp = false;
                }

                // Exit
                if (menu.State(7, 3))
                {
                    this.Exit();
                }
            }

            #endregion



            // Update game
            game.Update(gameTime);
            // If initializeGame, then start game, when it have started, make initializeGame false (so that it wont continue to start)
            if (initializeGame)
            {
                game.StartGame(gameTime);
                if (game.GameActive)
                {
                    initializeGame = false;
                }
            }

            // Update camera (It needs to be last thing of everything in InGame.cs)
            game.UpdateCamera();

            base.Update(gameTime);
        }