Esempio n. 1
0
        /// <summary>
        /// Allows the game to run logic.
        /// </summary>
        protected override void Update(GameTime gameTime)
        {
            lastKeyboardState = currentKeyboardState;
            lastGamePadState  = currentGamePadState;
            lastMousState     = currentMouseState;

#if WINDOWS_PHONE
            currentKeyboardState = new KeyboardState();
#else
            currentKeyboardState = Keyboard.GetState();
#endif
            currentGamePadState = GamePad.GetState(PlayerIndex.One);
            currentMouseState   = Mouse.GetState();


            // Exit when the Escape key or Back button is pressed
            if (currentKeyboardState.IsKeyDown(Keys.Escape) ||
                currentGamePadState.Buttons.Back == ButtonState.Pressed)
            {
                Exit();
            }

            bool touchTopLeft = currentMouseState.LeftButton == ButtonState.Pressed &&
                                lastMousState.LeftButton != ButtonState.Pressed &&
                                currentMouseState.X < GraphicsDevice.Viewport.Width / 10 &&
                                currentMouseState.Y < GraphicsDevice.Viewport.Height / 10;


            // Pressing the A button or key toggles the spring behavior on and off
            if (lastKeyboardState.IsKeyUp(Keys.A) &&
                (currentKeyboardState.IsKeyDown(Keys.A)) ||
                (lastGamePadState.Buttons.A == ButtonState.Released &&
                 currentGamePadState.Buttons.A == ButtonState.Pressed) ||
                touchTopLeft)
            {
                cameraSpringEnabled = !cameraSpringEnabled;
            }

            // Pressing the B button or key toggles terrain wireframe on and off
            if (lastKeyboardState.IsKeyUp(Keys.B) &&
                (currentKeyboardState.IsKeyDown(Keys.B)) ||
                (lastGamePadState.Buttons.B == ButtonState.Released &&
                 currentGamePadState.Buttons.B == ButtonState.Pressed))
            {
                wireframeEnabled = !wireframeEnabled;
            }

            // Reset the ship on R key or right thumb stick clicked
            if (currentKeyboardState.IsKeyDown(Keys.R) ||
                currentGamePadState.Buttons.RightStick == ButtonState.Pressed)
            {
                ship.Reset();
                camera.Reset();
            }

            // Update the ship
            ship.Update(gameTime);

            // Update the camera to chase the new target
            UpdateCameraChaseTarget();

            // The chase camera's update behavior is the springs, but we can
            // use the Reset method to have a locked, spring-less camera
            if (cameraSpringEnabled)
            {
                camera.Update(gameTime);
            }
            else
            {
                camera.Reset();
            }

            terrain.Update(gameTime);

            base.Update(gameTime);
        }