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 currentKeyboardState = Keyboard.GetState();
            GamePadState  currentGamepadState  = GamePad.GetState(PlayerIndex.One);

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || currentKeyboardState.IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;// difference in elapsed game time since last call

            switch (gameState)
            {
            case GameState.Menu:
                currentKeyboardState = Keyboard.GetState();

                if (currentKeyboardState.IsKeyDown(Keys.Enter))
                {
                    if (menuOption == MenuOption.Quit)
                    {
                        this.Exit();
                    }
                    else if (menuOption == MenuOption.Start)
                    {
                        gameState = GameState.Playing;
                    }
                }
                // move to the next option
                // doing modulus by the number of options lets us wrap back around to the first option
                // this also allows us to easily add / remove options later
                else if (currentKeyboardState.IsKeyDown(Keys.Up) &&
                         lastKeyboardState.IsKeyUp(Keys.Up))
                {
                    // up takes the absolute value in case it causes menuOption to go negative
                    menuOption = (MenuOption)Math.Abs((int)(menuOption - 1) % NUM_MENU_OPTIONS);
                }
                else if (currentKeyboardState.IsKeyDown(Keys.Down) &&
                         lastKeyboardState.IsKeyUp(Keys.Down))
                {
                    menuOption = (MenuOption)((int)(menuOption + 1) % NUM_MENU_OPTIONS);
                }

                break;

            case GameState.Playing:
                //gamePadState = GamePad.GetState(bike.PlayerIndex);
                currentKeyboardState = Keyboard.GetState();

                for (int i = Actor.Actors.Count - 1; i >= 0; i--)
                {
                    Actor actor = Actor.Actors[i];
                    actor.Update(gameTime);

                    Bike bike = actor as Bike;
                    if (bike != null)
                    {
                        if (currentKeyboardState.IsKeyDown(GetKey(bike.PlayerIndex, Direction.Up)))
                        {
                            bike.ChangeDirection(Direction.Up);
                        }
                        else if (currentKeyboardState.IsKeyDown(GetKey(bike.PlayerIndex, Direction.Down)))
                        {
                            bike.ChangeDirection(Direction.Down);
                        }
                        else if (currentKeyboardState.IsKeyDown(GetKey(bike.PlayerIndex, Direction.Left)))
                        {
                            bike.ChangeDirection(Direction.Left);
                        }
                        else if (currentKeyboardState.IsKeyDown(GetKey(bike.PlayerIndex, Direction.Right)))
                        {
                            bike.ChangeDirection(Direction.Right);
                        }

                        //double speedRange = BikeStopThreshold - BikeMoveInterval;
                        //bike.MoveInterval = gamePadState.Triggers.Right * speedRange + BikeMoveInterval;
                    }
                }

                if (currentKeyboardState.IsKeyDown(Keys.P) &&
                    lastKeyboardState.IsKeyUp(Keys.P))
                {
                    gameState = GameState.Paused;
                }

                break;

            case GameState.Collision:
                currentKeyboardState = Keyboard.GetState();
                timeSinceCollision  += dt;

                if (timeSinceCollision < EXPLOSION_TIME)
                {
                    UpdateExplosions(dt);
                }

                // ENTER restarts the game
                if (currentKeyboardState.IsKeyDown(Keys.Enter))
                {
                    timeSinceCollision = 0f;
                    CreateScene();
                    gameState = GameState.Playing;
                }
                break;

            case GameState.Paused:
                currentKeyboardState = Keyboard.GetState();

                if (currentKeyboardState.IsKeyDown(Keys.P) &&
                    lastKeyboardState.IsKeyUp(Keys.P))
                {
                    gameState = GameState.Playing;
                }

                break;
            }

            currentGamepadState = GamePad.GetState(PlayerIndex.One);
            lastKeyboardState   = currentKeyboardState;

            base.Update(gameTime);
        }