예제 #1
0
    /// <summary>
    /// Updates the player's movement.
    /// </summary>
    public virtual void UpdateMovement()
    {
        // Do not apply input if we are showing a level selection display
        if (HaltUpdateMovement == true)
        {
            return;
        }
        if (Application.loadedLevel != 0)
        {
            moveForward = false;
        }
        bool moveLeft  = false;
        bool moveRight = false;
        bool moveBack  = false;

        MoveScale = 2.0f;

        // * * * * * * * * * * *
        // Keyboard input

        // Move

        // WASD
        if (Input.GetKey(KeyCode.W))
        {
            moveForward = true;
        }
        if (Input.GetKey(KeyCode.A))
        {
            moveLeft = true;
        }
        if (Input.GetKey(KeyCode.S))
        {
            moveBack = true;
        }
        if (Input.GetKey(KeyCode.D))
        {
            moveRight = true;
        }
        // Arrow keys
        if (Input.GetKey(KeyCode.UpArrow))
        {
            if (moveForward)
            {
                moveForward = false;
            }
            else
            {
                moveForward = true;
            }
        }
        //if (Input.GetKey(KeyCode.LeftArrow))  moveLeft      = true;
        if (Input.GetKey(KeyCode.DownArrow))
        {
            moveBack = true;
        }
        //if (Input.GetKey(KeyCode.RightArrow)) moveRight   = true;
        //Jump
        float x = 0;
        float y = 0;
        float z = 0;

        if (Application.loadedLevel == 0)
        {
            if (Input.GetKey(KeyCode.J))
            {
                Jump();
            }
            if (OVRDevice.GetAcceleration(ref x, ref y, ref z))
            {
                if (y > 1.5 && x < 0.3 && z < 0.3)
                {
                    Jump();
                }
            }
        }

        //Restart
        if (Input.GetKey(KeyCode.Return))
        {
            myTimer = 300.0f;
            Application.LoadLevel("Scaled");
        }



        // D-Pad
        bool dpad_move = false;

        if (OVRGamepadController.GPC_GetButton((int)OVRGamepadController.Button.Up) == true)
        {
            moveForward = true;
            dpad_move   = true;
        }
        if (OVRGamepadController.GPC_GetButton((int)OVRGamepadController.Button.Down) == true)
        {
            moveBack  = true;
            dpad_move = true;
        }

        if ((moveForward && moveLeft) || (moveForward && moveRight) ||
            (moveBack && moveLeft) || (moveBack && moveRight))
        {
            MoveScale = 0.70710678f;
        }

        // No positional movement if we are in the air
        //if (!Controller.isGrounded)
        //MoveScale = 0.0f;

        MoveScale *= OVRDevice.SimulationRate * Time.deltaTime;

        // Compute this for key movement
        float moveInfluence = Acceleration * 0.1f * MoveScale * MoveScaleMultiplier;

        // Run!
        if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
        {
            moveInfluence *= 2.0f;
        }
        else if (dpad_move)
        {
            moveInfluence *= 3.0f;
        }

        if (DirXform != null)
        {
            if (moveForward)
            {
                MoveThrottle += DirXform.TransformDirection(Vector3.forward * moveInfluence * transform.lossyScale.z);
            }
            if (moveBack)
            {
                MoveThrottle += DirXform.TransformDirection(Vector3.back * moveInfluence * transform.lossyScale.z) * BackAndSideDampen;
            }
            if (moveLeft)
            {
                MoveThrottle += DirXform.TransformDirection(Vector3.left * moveInfluence * transform.lossyScale.x) * BackAndSideDampen;
            }
            if (moveRight)
            {
                MoveThrottle += DirXform.TransformDirection(Vector3.right * moveInfluence * transform.lossyScale.x) * BackAndSideDampen;
            }
        }

        // Rotate

        // D-Pad rachet

        bool curHatLeft = false;

        if (OVRGamepadController.GPC_GetButton((int)OVRGamepadController.Button.Left) == true)
        {
            curHatLeft = true;
        }

        if (curHatLeft && !prevHatLeft)
        {
            YRotation -= RotationRatchet;
        }

        prevHatLeft = curHatLeft;

        bool curHatRight = false;

        if (OVRGamepadController.GPC_GetButton((int)OVRGamepadController.Button.Right) == true)
        {
            curHatRight = true;
        }

        if (curHatRight && !prevHatRight)
        {
            YRotation += RotationRatchet;
        }

        prevHatRight = curHatRight;

        //Use keys to ratchet rotation
        if (Input.GetKeyDown(KeyCode.Q))
        {
            YRotation -= RotationRatchet;
        }
        if (Input.GetKeyDown(KeyCode.E))
        {
            YRotation += RotationRatchet;
        }

        // * * * * * * * * * * *
        // Mouse input

        // Move

        // Rotate

        // compute for key rotation
        float rotateInfluence = OVRDevice.SimulationRate * Time.deltaTime * RotationAmount * RotationScaleMultiplier;

        float deltaRotation = 0.0f;

        if (SkipMouseRotation == false)
        {
            deltaRotation = Input.GetAxis("Mouse X") * rotateInfluence * 3.25f;
        }

        float filteredDeltaRotation = (sDeltaRotationOld * 0.0f) + (deltaRotation * 1.0f);

        YRotation        += filteredDeltaRotation;
        sDeltaRotationOld = filteredDeltaRotation;

        // * * * * * * * * * * *
        // XBox controller input

        // Compute this for xinput movement
        moveInfluence = OVRDevice.SimulationRate * Time.deltaTime * Acceleration * 0.1f * MoveScale * MoveScaleMultiplier;

        // Run!
        moveInfluence *= 1.0f +
                         OVRGamepadController.GPC_GetAxis((int)OVRGamepadController.Axis.LeftTrigger);

        // Move
        if (DirXform != null)
        {
            float leftAxisY =
                OVRGamepadController.GPC_GetAxis((int)OVRGamepadController.Axis.LeftYAxis);

            float leftAxisX =
                OVRGamepadController.GPC_GetAxis((int)OVRGamepadController.Axis.LeftXAxis);

            if (leftAxisY > 0.0f)
            {
                MoveThrottle += leftAxisY *
                                DirXform.TransformDirection(Vector3.forward * moveInfluence);
            }

            if (leftAxisY < 0.0f)
            {
                MoveThrottle += Mathf.Abs(leftAxisY) *
                                DirXform.TransformDirection(Vector3.back * moveInfluence) * BackAndSideDampen;
            }

            if (leftAxisX < 0.0f)
            {
                MoveThrottle += Mathf.Abs(leftAxisX) *
                                DirXform.TransformDirection(Vector3.left * moveInfluence) * BackAndSideDampen;
            }

            if (leftAxisX > 0.0f)
            {
                MoveThrottle += leftAxisX *
                                DirXform.TransformDirection(Vector3.right * moveInfluence) * BackAndSideDampen;
            }
        }

        float rightAxisX =
            OVRGamepadController.GPC_GetAxis((int)OVRGamepadController.Axis.RightXAxis);

        // Rotate
        YRotation += rightAxisX * rotateInfluence;

        // Update cameras direction and rotation
        SetCameras();
    }