示例#1
0
        public void Follow(Vector2 pos, Rectangle hitbox, float deltaTime)
        {
            var position = Matrix.CreateTranslation(new Vector3(-pos.X - (hitbox.Width / 2), -pos.Y - (hitbox.Height / 2), 0));

            var offset = Matrix.CreateTranslation(new Vector3(Game1.screenSize.X / 2, Game1.screenSize.Y / 2, 0));
            var scale  = Matrix.CreateScale(new Vector3(2f, 2f, 0));
            //transform = position * offset;
            Matrix newPos        = position * offset * scale;
            float  smoothTime    = 0.2f;
            float  amountToMoveY = AdvancedMath.SmoothDamp(transform.Translation.Y, newPos.Translation.Y, ref yVelocity, smoothTime, float.MaxValue, deltaTime);
            float  amountToMoveX = AdvancedMath.SmoothDamp(transform.Translation.X, newPos.Translation.X, ref xVelocity, smoothTime, float.MaxValue, deltaTime);

            transform = Matrix.CreateTranslation(new Vector3(amountToMoveX, amountToMoveY, 0));
        }
示例#2
0
        //public double ConvertToRadians(double angle) //Stulen från unity
        //{
        //    return (Math.PI / 180) * angle;
        //}

        public static Vector2 ClampMagnitude(Vector2 vector, float maxLength)//Stulen från unity
        {
            float sqrMagnitude = AdvancedMath.sqrMagnitude(vector);

            if (sqrMagnitude > maxLength * maxLength)
            {
                float mag = (float)Math.Sqrt(sqrMagnitude);
                //these intermediate variables force the intermediate result to be
                //of float precision. without this, the intermediate result can be of higher
                //precision, which changes behavior.
                float normalized_x = vector.X / mag;
                float normalized_y = vector.Y / mag;
                return(new Vector2(normalized_x * maxLength,
                                   normalized_y * maxLength));
            }
            return(vector);
        }
示例#3
0
        public static KeyboardState GetState()
        {
            previousKeyState = currentKeyState;
            currentKeyState  = Microsoft.Xna.Framework.Input.Keyboard.GetState();

            previousGamePadState = currentGamePadState;
            currentGamePadState  = Microsoft.Xna.Framework.Input.GamePad.GetState(PlayerIndex.One);

            previousMouseState = currentMouseState;
            currentMouseState  = Microsoft.Xna.Framework.Input.Mouse.GetState();

            previousscrollWheelValue = currentscrollWheelValue;
            currentscrollWheelValue  = Mouse.GetState().ScrollWheelValue;

            differenceScrollWheelValue = currentscrollWheelValue - previousscrollWheelValue;
            if (Math.Abs(differenceScrollWheelValue) > 10)
            {
            }

            clampedScrollWheelValue += differenceScrollWheelValue;

            clampedScrollWheelValue = MathHelper.Clamp(clampedScrollWheelValue, minScrollWheel, maxScrollWheel);
            //Vector2 input = new Vector2();
            directional  = new Vector2();
            directional += new Vector2(0, GamePad.GetState(PlayerIndex.One).DPad.Down == ButtonState.Pressed ? 1 : 0);
            directional += new Vector2(0, GamePad.GetState(PlayerIndex.One).DPad.Up == ButtonState.Pressed ? -1 : 0);
            directional += new Vector2(GamePad.GetState(PlayerIndex.One).DPad.Left == ButtonState.Pressed ? -1 : 0, 0);
            directional += new Vector2(GamePad.GetState(PlayerIndex.One).DPad.Right == ButtonState.Pressed ? 1 : 0, 0);

            directional += new Vector2(GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X, -GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Y);

            directional          += new Vector2(0, GetButton(Keys.Down) || GetButton(Keys.S) ? 1 : 0);
            directional          += new Vector2(0, GetButton(Keys.Up) || GetButton(Keys.W) ? -1 : 0);
            directional          += new Vector2(GetButton(Keys.Left) || GetButton(Keys.A) ? -1 : 0, 0);
            directional          += new Vector2(GetButton(Keys.Right) || GetButton(Keys.D) ? 1 : 0, 0);
            directional           = new Vector2(Math.Clamp(directional.X, -1, 1), Math.Clamp(directional.Y, -1, 1));
            normalizedDirectional = AdvancedMath.ClampMagnitude(directional, 1);
            return(currentKeyState);
        }