Beispiel #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)
        {
            KeyboardState newState = Keyboard.GetState();



            Keys[] keysDown = newState.GetPressedKeys();


            switch (gameState)
            {
            case GAMESTATE.GAMESTATE_GAME_RUNNING:
            {
                foreach (Keys k in keysDown)
                {
                    if (oldState.IsKeyUp(k))
                    {
                        if (k == keys_moveLeft)
                        {
                            gameField.MoveLeft(); TimeSpan_DAS_Delay = TimeSpan.Zero; TimeSpan_DAS_Repeat = TimeSpan.Zero;
                        }
                        if (k == keys_moveRight)
                        {
                            gameField.MoveRight(); TimeSpan_DAS_Delay = TimeSpan.Zero; TimeSpan_DAS_Repeat = TimeSpan.Zero;
                        }
                        if (k == keys_hardDrop)
                        {
                            gameField.LockBlock();
                        }
                        if (k == keys_rotateLeft)
                        {
                            gameField.RotateLeft();
                        }
                        if (k == keys_rotateRight)
                        {
                            gameField.RotateRight();
                        }
                        if (k == keys_softDrop)
                        {
                            gameField.MoveDown(); TimeSpan_DAS_Delay = TimeSpan.Zero; TimeSpan_DAS_Repeat = TimeSpan.Zero;
                        }
                        if (k == keys_switchHold)
                        {
                            gameField.SwitchActiveBlockWithHold();
                        }
                        if (k == keys_menuBack)
                        {
                            gameState = GAMESTATE.GAMESTATE_MAIN_MENU;
                        }
                    }
                    if (oldState.IsKeyDown(k))
                    {
                        TimeSpan_DAS_Delay  += gameTime.ElapsedGameTime;
                        TimeSpan_DAS_Repeat += gameTime.ElapsedGameTime;
                        if (TimeSpan_DAS_Delay.TotalMilliseconds >= DAS_Delay && TimeSpan_DAS_Repeat.TotalMilliseconds >= DAS_Speed)
                        {
                            if (k == keys_moveLeft)
                            {
                                gameField.MoveLeft(); TimeSpan_DAS_Repeat = TimeSpan.Zero;
                            }
                            if (k == keys_moveRight)
                            {
                                gameField.MoveRight(); TimeSpan_DAS_Repeat = TimeSpan.Zero;
                            }
                            if (k == keys_softDrop)
                            {
                                gameField.MoveDown(); TimeSpan_DAS_Repeat = TimeSpan.Zero; TimeSpan_GravityTick = TimeSpan.Zero;
                            }
                        }
                    }
                }
                break;
            }

            default:
            {
                foreach (Keys k in keysDown)
                {
                    if (oldState.IsKeyUp(k))
                    {
                        menuManager.HandleInput(k);
                    }
                }
                break;
            }
            }


            switch (gameState)
            {
            case GAMESTATE.GAMESTATE_GAME_RUNNING:
            {
                TimeSpan_GravityTick += gameTime.ElapsedGameTime;
                if (TimeSpan_GravityTick.TotalMilliseconds >= difficultyTickSpeedMillis[difficultyLevel])
                {
                    gameField.MoveDown();
                    if (gameField.GetGameOver())
                    {
                        gameState = GAMESTATE.GAMESTATE_GAMEOVER;
                        menuManager.SetActiveMenu("gameOver");
                    }
                    TimeSpan_GravityTick = TimeSpan.Zero;
                }
                break;
            }

            default:
                break;
            }

            oldState = Keyboard.GetState();
            base.Update(gameTime);
        }