Ejemplo n.º 1
0
        public void UpdateCamera(Device dev, TimeSpan diff)
        {
            if (Input.InputManager.Input.HasFocus == false)
            {
                return;
            }

            bool  changed     = false;
            var   inp         = Input.InputManager.Input;
            float sensitivity = Game.GameManager.GameWindow.PropertyPanel.CameraSensitivity;

            if (inp[Keys.W])
            {
                mPosition += (float)diff.TotalSeconds * mFront * 50;
                mTarget   += (float)diff.TotalSeconds * mFront * 50;
                changed    = true;
            }
            if (inp[Keys.S])
            {
                mPosition -= (float)diff.TotalSeconds * mFront * 50;
                mTarget   -= (float)diff.TotalSeconds * mFront * 50;
                changed    = true;
            }
            if (inp[Keys.D])
            {
                var change = (float)diff.TotalSeconds * mRight * 50;
                mPosition += change;
                mTarget   += change;
                changed    = true;
            }
            if (inp[Keys.A])
            {
                var change = (float)diff.TotalSeconds * mRight * 50;
                mPosition -= change;
                mTarget   -= change;
                changed    = true;
            }
            if (inp[Keys.Q])
            {
                var change = (float)diff.TotalSeconds * Vector3.UnitZ * 50;
                mPosition += change;
                mTarget   += change;
                changed    = true;
            }
            if (inp[Keys.E])
            {
                var change = (float)diff.TotalSeconds * Vector3.UnitZ * 50;
                mPosition -= change;
                mTarget   -= change;
                changed    = true;
            }

            var state = Input.InputManager.Input.Mouse.State;

            if (state.IsPressed((int)SlimDX.DirectInput.MouseObject.Button2) && Game.GameManager.SelectionManager.IsModelMovement == false)
            {
                if (state.X != 0 || state.Y != 0)
                {
                    if (state.X != 0)
                    {
                        int    fac = (state.X < 0) ? -1 : 1;
                        Matrix rot = Matrix.RotationZ(state.X * 0.005f * sensitivity);
                        mFront = Vector3.TransformCoordinate(mFront, rot);
                        mFront.Normalize();
                        mTarget = mPosition + mFront;
                        mRight  = Vector3.TransformCoordinate(mRight, rot);
                        mRight.Normalize();
                        mUp = Vector3.TransformCoordinate(mUp, rot);
                        mUp.Normalize();
                        changed = true;
                    }
                    if (state.Y != 0)
                    {
                        int    fac = (state.Y < 0) ? -1 : 1;
                        Matrix rot = Matrix.RotationAxis(mRight, state.Y * 0.005f * sensitivity);
                        mFront = Vector3.TransformCoordinate(mFront, rot);
                        mFront.Normalize();
                        mTarget = mPosition + mFront;
                        mUp     = Vector3.TransformCoordinate(mUp, rot);
                        mUp.Normalize();
                        changed = true;
                    }
                }
            }


            if (changed)
            {
                dev.SetTransform(TransformState.View, Matrix.LookAtLH(mPosition, mTarget, mUp));
                ShaderCollection.CameraChanged(this);
                Game.GameManager.WorldManager.Update(this);
                ViewFrustum.BuildViewFrustum(dev.GetTransform(TransformState.View), dev.GetTransform(TransformState.Projection));
                Game.GameManager.InformPropertyChanged(Game.GameProperties.CameraPosition);
            }
        }