Exemplo n.º 1
0
    // InputHandler for the various game states
    public void HandleInput(GameTime gameTime, InputHelper inputHelper)
    {
        if (gameState == GameState.Menu)
        {
            // Check if the mousebutton is pressed, and if so if it was inside one of the boundingboxes of the buttons
            if (inputHelper.MouseLeftButtonPressed())
            {
                if (Play.IsClicked(inputHelper))
                {
                    // stop the Mediaplayer (so that the game song can be played) and make the mouse invisible
                    MediaPlayer.Stop();
                    TetrisGame.showmouse = false;
                    // Reset the game
                    Reset();

                    // Only play music in the main game if the user has it set to true in the options menu
                    if (music)
                    {
                        MediaPlayer.Play(playing);
                        Console.WriteLine(MediaPlayer.State);
                    }
                    if (videoison)
                    {
                        videoplayer.Play(backgroundvideo);
                        videoplayer.IsLooped = true;
                    }
                    gameState = GameState.Playing;
                }
                // Switch to other menus depending on which button was clicked
                if (Help.IsClicked(inputHelper))
                {
                    TetrisGame.showmouse = true;
                    gameState            = GameState.Help;
                }
                if (Options.IsClicked(inputHelper))
                {
                    TetrisGame.showmouse = true;
                    gameState            = GameState.Options;
                }
                if (Back.IsClicked(inputHelper))
                {
                    // set a boolean in the main game clas to true so that it exits the game
                    TetrisGame.exitgame = true;
                }
            }
        }
        if (gameState == GameState.Playing)
        {
            // Check if the current block even exists (this will never be the case but it is a failsafe)
            if (newblock != null)
            {
                // Rotate the block left, check if its new position is valid, and if not rotate it back to it's previous position
                if (inputHelper.KeyPressed(Keys.A, false))
                {
                    newblock.RotateLeft();
                    if (!grid.IsValid(newblock))
                    {
                        newblock.RotateRight();
                    }
                }
                // Rotate the block right, check if its new position is valid, and if not rotate it back to it's previous position
                if (inputHelper.KeyPressed(Keys.D, false))
                {
                    newblock.RotateRight();
                    if (!grid.IsValid(newblock))
                    {
                        newblock.RotateLeft();
                    }
                }
                if (inputHelper.KeyPressed(Keys.W, false))
                {
                    // Moves the current block down instantly, uses an optional boolean inside the TryMoveDown method to check if the block was placed
                    // (otherwise the next block would go down a bit because the loop didn't exit yet)
                    for (int i = 0; i < TetrisGrid.GridHeight; i++)
                    {
                        TryMoveDown(true);
                        if (placed)
                        {
                            // break the loop if Trymovedown communicates that it placed a block
                            placed = false;
                            break;
                        }
                    }
                }

                // Try to move the block down (same kind of strategy as the rotate method, but a seperate method handles all 3 commands plus
                // a few additional extras)
                if (inputHelper.KeyPressed(Keys.S, true))
                {
                    TryMoveDown();
                }

                // Same as with the rotate method, try to move left and if it is not valid do the reverse operation
                if (inputHelper.KeyPressed(Keys.Left, true))
                {
                    newblock.MoveLeft();
                    if (!grid.IsValid(newblock))
                    {
                        newblock.MoveRight();
                    }
                }
                // Now the same but with moving right
                if (inputHelper.KeyPressed(Keys.Right, true))
                {
                    newblock.MoveRight();
                    if (!grid.IsValid(newblock))
                    {
                        newblock.MoveLeft();
                    }
                }
            }
        }
        if (gameState == GameState.Options)
        {
            // options menu to turn music and the video background off
            if (inputHelper.MouseLeftButtonPressed())
            {
                // Go back to the main menu if the back button is clicked
                if (Back.IsClicked(inputHelper))
                {
                    gameState = GameState.Loading;
                }
                // if the music button is clicked, change to boolean to its reverse (true to false or false to true) and stop the music
                // currently playing if it is set to false
                if (Musicbutton.IsClicked(inputHelper))
                {
                    music = !music;
                    if (!music)
                    {
                        MediaPlayer.Stop();
                    }
                }
                // Change the boolean for the videobackground if the button is clicked
                if (Videobutton.IsClicked(inputHelper))
                {
                    videoison = !videoison;
                }
            }
        }
        // Check if the player has pressed the back button in the help or gameover screen (it is the same button in the same position)
        if (gameState == GameState.Help || gameState == GameState.GameOver)
        {
            if (inputHelper.MouseLeftButtonPressed())
            {
                if (Back.IsClicked(inputHelper))
                {
                    gameState = GameState.Loading;
                }
            }
        }
    }