Example #1
0
        /// <summary>
        /// Updates the camera by following the rectangle given in a smooth manner.
        /// </summary>
        /// <param name="rectangle"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="active">Should the camera follow the rectangle.</param>
        public void UpdateSmoothly(Rectangle rectangle, int width, int height, bool active)
        {
            Vector3 currentLeftCorner;

            // If the camera is not active, which is used to show the player is dead, the camera stops moving.
            if (active)
            {
                Vector2 playerPos = new Vector2(rectangle.X + rectangle.Width / 2, rectangle.Y + rectangle.Height / 2);
                currentLeftCorner = new Vector3(-playerPos.X + _defRes.X / _zoom / 2, -playerPos.Y + (3 * _defRes.Y / _zoom / 5), 0);
            }
            else
            {
                currentLeftCorner = LastCameraLeftCorner;
            }

            if (LockedX)
            {
                currentLeftCorner.X = LastCameraLeftCorner.X;
            }
            if (LockedY)
            {
                currentLeftCorner.Y = LastCameraLeftCorner.Y;
            }
            if (RestricedToGameWorld)
            {
                if (currentLeftCorner.X > 0)
                {
                    currentLeftCorner.X = 0;
                }
                if (currentLeftCorner.X < -(width * TMBAW_Game.Tilesize - _defRes.X))
                {
                    currentLeftCorner.X = -(width * TMBAW_Game.Tilesize - _defRes.X);
                }
                if (currentLeftCorner.Y > 0)
                {
                    currentLeftCorner.Y = 0;
                }
                if (currentLeftCorner.Y < -(height * TMBAW_Game.Tilesize - _defRes.Y))
                {
                    currentLeftCorner.Y = -(height * TMBAW_Game.Tilesize - _defRes.Y);
                }
            }

            if (InputSystem.IsKeyDown(Keys.OemMinus))
            {
                ButtonBar.MinusButton_MouseClicked(null);
            }
            if (InputSystem.IsKeyDown(Keys.OemPlus))
            {
                ButtonBar.PlusButton_MouseClicked(null);
            }
            if (InputSystem.IsKeyDown(Keys.R))
            {
                ResetZoom();
            }

            Velocity = (currentLeftCorner - LastCameraLeftCorner) / 5;
            Vector3 cameraLeftCorner = LastCameraLeftCorner;

            // Used if the camera zoom changed.
            if (_updateInstantly)
            {
                cameraLeftCorner = currentLeftCorner;
                _updateInstantly = false;
            }
            else
            {
                cameraLeftCorner += Velocity;
            }

            CenterGameCoords = new Vector2(-currentLeftCorner.X, -currentLeftCorner.Y);

            LeftTopGameCoords = CenterGameCoords;

            CenterGameCoords.X += TMBAW_Game.DefaultResWidth / 2;
            CenterGameCoords.Y += TMBAW_Game.DefaultResHeight * 2 / 3;
            TileIndex           = (int)((int)CenterGameCoords.Y / TMBAW_Game.Tilesize * GameWorld.WorldData.LevelWidth) + (int)((int)CenterGameCoords.X / TMBAW_Game.Tilesize);


            LastCameraLeftCorner = cameraLeftCorner;
            _lastVelocity        = Velocity;

            int shakeOffset = 1;

            if (_shakeTimer.TimeElapsedInMilliSeconds < 100)
            {
                switch (TMBAW_Game.Random.Next(0, 5))
                {
                case 0:
                    cameraLeftCorner.X += shakeOffset;
                    break;

                case 1:
                    cameraLeftCorner.X -= shakeOffset;
                    break;

                case 2:
                    cameraLeftCorner.Y += shakeOffset;
                    break;

                case 3:
                    cameraLeftCorner.Y -= shakeOffset;
                    break;
                }
            }


            //cameraLeftCorner = new Vector3(DrawRectangle.X, DrawRectangle.Y, 0);
            cameraLeftCorner = new Vector3((int)cameraLeftCorner.X, (int)cameraLeftCorner.Y, 0);
            _translation     = Matrix.CreateTranslation(cameraLeftCorner) * Matrix.CreateScale(new Vector3(_zoom, _zoom, 0));
            HalfTranslate    = Matrix.CreateTranslation(cameraLeftCorner * 2);
        }