WindowsPuzzleVisualizer.Game1.Update C# (CSharp) Method

Update() protected method

Allows the game to run logic such as updating the world, checking for collisions, gathering input, and playing audio.
protected Update ( GameTime gameTime ) : void
gameTime Microsoft.Xna.Framework.GameTime Provides a snapshot of timing values.
return void
        protected override void Update(GameTime gameTime)
        {
            KeyboardState keyboard = Keyboard.GetState();

            if (keyboard.IsKeyDown(Keys.Escape))
                Exit();

            if (keyboard.IsKeyDown(Keys.F11) && prevKeyboardState.IsKeyUp(Keys.F11))
                IsFullScreen = !IsFullScreen;

            //if (keyboard.IsKeyDown(Keys.A) && prevKeyboardState.IsKeyUp(Keys.A))
            //    isAnimating = !isAnimating;

            float movement = (float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.02f;

            if (keyboard.IsKeyDown(Keys.PageUp))
            {
                cameraPosition = new Vector3(cameraPosition.X, cameraPosition.Y, cameraPosition.Z - movement);
            }

            if (keyboard.IsKeyDown(Keys.PageDown))
            {
                cameraPosition = new Vector3(cameraPosition.X, cameraPosition.Y, cameraPosition.Z + movement);
            }

            float rotation = (float)gameTime.ElapsedGameTime.TotalMilliseconds *
                    MathHelper.ToRadians(0.1f);

            if (keyboard.IsKeyDown(Keys.Left))
            {
                rotationMatrix = rotationMatrix * Matrix.CreateRotationY(rotation);
            }
            if (keyboard.IsKeyDown(Keys.Right))
            {
                rotationMatrix = rotationMatrix * Matrix.CreateRotationY(-rotation);
            }
            if (keyboard.IsKeyDown(Keys.Up))
            {
                rotationMatrix = rotationMatrix * Matrix.CreateRotationX(rotation);
            }
            if (keyboard.IsKeyDown(Keys.Down))
            {
                rotationMatrix = rotationMatrix * Matrix.CreateRotationX(-rotation);
            }

            bool commandForward = false;
            bool commandBack = false;
            bool commandResetView = false;

            TouchCollection touches = TouchPanel.GetState();

            if (touches.Count > 0)
            {
                foreach (TouchLocation touch in touches)
                {
                    if (touch.Position.X <= _buttonTop)
                    {
                        _rotationVelocity = Vector2.Zero;
                    }
                }
            }

            while (TouchPanel.IsGestureAvailable)
            {
                float factor = 0.01f;

                GestureSample gesture = TouchPanel.ReadGesture();
                //  Windows store app does not rotate view sideways, so swap X and Y
            #if NETFX_CORE
                float gestureDeltaX = gesture.Delta.Y;
                float gestureDeltaY = gesture.Delta.X;
            #else
                float gestureDeltaX = gesture.Delta.X;
                float gestureDeltaY = -gesture.Delta.Y;
            #endif
                if (gesture.GestureType == GestureType.FreeDrag)
                {
                    rotationMatrix = rotationMatrix *
                        Matrix.CreateRotationX(gestureDeltaX * factor) *
                        Matrix.CreateRotationY(gestureDeltaY * factor);
                }
                else if (gesture.GestureType == GestureType.Flick)
                {
                    float seconds = (float)gameTime.ElapsedGameTime.TotalSeconds;
                    _rotationVelocity = new Vector2(gestureDeltaX * factor, gestureDeltaY * factor);
                }
                else if (gesture.GestureType == GestureType.Tap)
                {
                    if (gesture.Position.X <= GraphicsDevice.Viewport.Width / 2)
                       {
                            commandForward = true;
                        }
                        else if (gesture.Position.X > GraphicsDevice.Viewport.Width / 2 )
                        {
                            commandBack = true;
                        }
                }
                else if (gesture.GestureType == GestureType.DoubleTap)
                {
                    commandResetView = true;
                }
            }

            foreach (var kvp in _pieceKeyMapping)
            {
                if (keyboard.IsKeyDown(kvp.Key) && prevKeyboardState.IsKeyUp(kvp.Key))
                {
                    _pieceVisibility[kvp.Value] = !_pieceVisibility[kvp.Value];
                }
            }

            if (keyboard.IsKeyDown(Keys.R) && prevKeyboardState.IsKeyUp(Keys.R))
            {
                commandResetView = true;
            }

            if (keyboard.IsKeyDown(Keys.Space) && prevKeyboardState.IsKeyUp(Keys.Space))
            {
                commandForward = true;
            }

            if (keyboard.IsKeyDown(Keys.B) && prevKeyboardState.IsKeyUp(Keys.B))
            {
                commandBack = true;
            }

            if (keyboard.IsKeyDown(Keys.U) && prevKeyboardState.IsKeyUp(Keys.U))
            {
                lock (_lockObject)
                {
                    if (_solved)
                    {
                        _puzzleState = _moves[_moveIndex].PuzzleState;
                    }
                    else
                    {
                        _puzzleState = _solverState;
                    }
                }
            }

            if (commandResetView)
            {
                rotationMatrix = Matrix.Identity;
                cameraPosition = new Vector3(0.0f, 0.0f, 30.0f);
                _rotationVelocity = Vector2.Zero;
            }

            lock (_lockObject)
            {
                if (_solved)
                {
                    if (commandForward)
                    {
                        if (isAnimating)
                        {
                            EndAnimation();
                        }
                        if (_moveIndex < _moves.Length - 1)
                        {
                            var move = _moves[_moveIndex + 1].BestSolutionPrevMove;

                            if (move.IsRemoval)
                            {
                                _moveIndex++;
                                _puzzleState = _moves[_moveIndex].PuzzleState;
                            }
                            else
                            {
                                SetupAnimation(move, gameTime.TotalGameTime, _moveIndex + 1);
                            }
                        }
                    }

                    if (commandBack)
                    {
                        if (isAnimating)
                        {
                            EndAnimation();
                        }

                        if (_moveIndex > 0)
                        {
                            int newMoveIndex = _moveIndex - 1;
                            var move = _moves[_moveIndex].BestSolutionPrevMove;
                            if (move.IsRemoval)
                            {
                                _moveIndex--;
                                _puzzleState = _moves[_moveIndex].PuzzleState;
                            }
                            else
                            {
                                var moveToReverse = move;
                                PuzzleMove reverseMove = new PuzzleMove(moveToReverse.GetEndingState().Normalize(), moveToReverse.MovingPieces, moveToReverse.Direction.Negate());
                                SetupAnimation(reverseMove, gameTime.TotalGameTime, newMoveIndex);
                            }
                        }
                    }
                }
                else
                {
                    if (commandForward)
                    {
                        _showSolverProgress = !_showSolverProgress;
                    }
                    if (commandBack)
                    {
                        _showSolverProgress = false;
                        _puzzleState = _initialState;
                    }
                }
            }

            if (_rotationVelocity != Vector2.Zero)
            {
                rotationMatrix = rotationMatrix *
                    Matrix.CreateRotationX(_rotationVelocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds) *
                    Matrix.CreateRotationY(_rotationVelocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds);

                float friction = 0.03f;
                _rotationVelocity *= 1f - (friction - (float)gameTime.ElapsedGameTime.TotalSeconds);
            }

            if (isAnimating)
            {
                if (gameTime.TotalGameTime >= _animationEnd)
                {
                    EndAnimation();
                }
                else
                {
                    TimeSpan animationElapsed = gameTime.TotalGameTime - _animationStart;
                    _animationPercent = (float)(animationElapsed.TotalMilliseconds / _animationLength.TotalMilliseconds);

                    modelPosition = _animationStartModelPosition + (_animationEndModelPosition - _animationStartModelPosition) * _animationPercent;
                }
            }
            else
            {
                lock (_lockObject)
                {
                    modelPosition = GetCenteringVector(_puzzleState);
                }
            }

            prevKeyboardState = keyboard;

            base.Update(gameTime);
        }