private void HandleFly()
    {
        //  Show/hide cursor
        if (Input.GetMouseButton(1))
        {
            Cursor.lockState = CursorLockMode.Locked;
        }
        else
        {
            Cursor.visible = true; Cursor.lockState = CursorLockMode.None;
        }

        // Rotation
        if (Input.GetMouseButton(1))
        {
            Vector2 mouseMovement = Vector3.zero;

            if (UIMaster.GetState() != UIState.FOCUSED)
            {
                mouseMovement = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y") * (invertY ? 1 : -1));

                //  Show/hide cursor
                if (Input.GetMouseButton(1))
                {
                    Cursor.lockState = CursorLockMode.Locked;
                }
                else
                {
                    Cursor.visible = true; Cursor.lockState = CursorLockMode.None;
                }
            }

            float mouseSensitivityFactor = mouseSensitivityCurve.Evaluate(mouseMovement.magnitude);
            m_TargetCameraState.yaw   += mouseMovement.x * mouseSensitivityFactor;
            m_TargetCameraState.pitch += mouseMovement.y * mouseSensitivityFactor;
        }

        // Translation
        Vector3 translation = GetInputTranslationDirection() * Time.deltaTime;

        // Speed up movement when shift key held
        if (Input.GetButton("Sprint"))
        {
            translation *= 10.0f;
        }

        // Modify movement by a boost factor (defined in Inspector and modified in play mode through the mouse scroll wheel)
        boost       += Input.mouseScrollDelta.y * 0.2f;
        translation *= Mathf.Pow(2.0f, boost);

        m_TargetCameraState.Translate(translation);

        // Framerate-independent interpolation
        float positionLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / positionLerpTime) * Time.deltaTime);
        float rotationLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / rotationLerpTime) * Time.deltaTime);

        m_InterpolatingCameraState.LerpTowards(m_TargetCameraState, positionLerpPct, rotationLerpPct);

        m_InterpolatingCameraState.UpdateTransform(transform);
    }
    private void HandleThirdPerson()
    {
        float positionLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / positionLerpTime) * Time.deltaTime);   //  Force a low position lerp
        float rotationLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / rotationLerpTime) * Time.deltaTime);

        //  Rotation
        Vector2 mouseMovement = Vector3.zero;

        if (UIMaster.GetState() != UIState.FOCUSED)
        {
            mouseMovement = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y") * (invertY ? 1 : -1));

            Cursor.visible   = false;
            Cursor.lockState = CursorLockMode.Locked;
        }

        float mouseSensitivityFactor = mouseSensitivityCurve.Evaluate(mouseMovement.magnitude);

        m_TargetCameraState.yaw   += mouseMovement.x * mouseSensitivityFactor;
        m_TargetCameraState.pitch += mouseMovement.y * mouseSensitivityFactor;

        //  Position
        Vector3 targetPos = thirdPersonLookTarget.position + (thirdPersonLookTarget.rotation * thirdPersonOffset);

        m_TargetCameraState.x = targetPos.x;
        m_TargetCameraState.y = targetPos.y;
        m_TargetCameraState.z = targetPos.z;

        // Framerate-independent interpolation
        m_InterpolatingCameraState.LerpTowards(m_TargetCameraState, positionLerpPct, rotationLerpPct);

        //  Pitch limits
        if (m_TargetCameraState.pitch < thirdPersonPitchLimits.x)
        {
            m_TargetCameraState.pitch = thirdPersonPitchLimits.x;
        }
        if (m_TargetCameraState.pitch > thirdPersonPitchLimits.y)
        {
            m_TargetCameraState.pitch = thirdPersonPitchLimits.y;
        }

        if (m_InterpolatingCameraState.pitch < thirdPersonPitchLimits.x)
        {
            m_InterpolatingCameraState.pitch = thirdPersonPitchLimits.x;
        }
        if (m_InterpolatingCameraState.pitch > thirdPersonPitchLimits.y)
        {
            m_InterpolatingCameraState.pitch = thirdPersonPitchLimits.y;
        }

        //  Update camera and targets
        m_InterpolatingCameraState.UpdateTransform(transform);
        thirdPersonLookTarget.rotation       = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);
        thirdPersonControlledTarget.rotation = Quaternion.Euler(thirdPersonControlledTarget.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, thirdPersonControlledTarget.rotation.eulerAngles.z);
    }
    private void Update()
    {
        //  If the UI isn't open and we are playing or spectating, allow camera control
        if (UIMaster.GetState() != UIState.FOCUSED && (GameMaster.GetState() == GameState.PLAYING || GameMaster.GetState() == GameState.SPECTATING))
        {
            if (Input.GetButtonDown("Switch View"))
            {
                SetView(view == CameraView.FIRST_PERSON ? CameraView.THIRD_PERSON : CameraView.FIRST_PERSON); GameMaster.SetState(GameState.PLAYING);
            }

            if (Input.GetButtonDown("Cinematic Camera"))
            {
                SetView(CameraView.CINEMATIC); GameMaster.SetState(GameState.SPECTATING);
            }
            if (Input.GetButtonDown("Fly Camera"))
            {
                SetView(CameraView.FLY); GameMaster.SetState(GameState.SPECTATING);
            }

            if (Input.GetButtonDown("Camera Zoom"))
            {
                Camera.main.fieldOfView = 20;
            }
            if (Input.GetButtonUp("Camera Zoom"))
            {
                Camera.main.fieldOfView = FOV;
            }
        }

        switch (view)
        {
        case CameraView.FIRST_PERSON:
            HandleFirstPerson();
            break;

        case CameraView.THIRD_PERSON:
            HandleThirdPerson();
            break;

        case CameraView.CINEMATIC:
            HandleCinematic();
            break;

        case CameraView.FLY:
            HandleFly();
            break;
        }
    }
    public override void Handle()
    {
        if (Input.GetButtonDown("Drop Item"))
        {
            if (Input.GetKey(KeyCode.LeftControl))
            {
                GameMaster.GetPlayer().DropItem(hotbarSelectedIndex);
            }
            else
            {
                GameMaster.GetPlayer().DropItem(hotbarSelectedIndex, 1);
            }
        }

        if (UIMaster.GetState() == UIState.NONE) // Only handle the hotbar when UI isn't open
        {
            //  Scrolling through hotbar
            if (Input.mouseScrollDelta.y != 0)
            {
                hotbarSelectedIndex -= Mathf.RoundToInt(Input.mouseScrollDelta.y);
                hotbarSelectedIndex  = Util.WrapInt(hotbarSelectedIndex, 0, hotbarSize - 1);

                updateHotbar = true;
            }

            //  Use number keys to switch selected hotbar index
            for (int i = 0; i < hotbarSize; i++)
            {
                KeyCode key = (KeyCode)System.Enum.Parse(typeof(KeyCode), "Alpha" + (i + 1), true);
                if (Input.GetKeyDown(key))
                {
                    hotbarSelectedIndex = i;
                    updateHotbar        = true;
                }
            }
        }

        if (updateHotbar)
        {
            UpdateHotbar();
            updateHotbar = false;
        }
    }
    private Vector3 GetInputTranslationDirection()
    {
        Vector3 direction = new Vector3();

        if (UIMaster.GetState() == UIState.FOCUSED)
        {
            return(direction);
        }

        if (Input.GetButton("Forward"))
        {
            direction += Vector3.forward;
        }
        if (Input.GetButton("Backward"))
        {
            direction += Vector3.back;
        }
        if (Input.GetButton("Left"))
        {
            direction += Vector3.left;
        }
        if (Input.GetButton("Right"))
        {
            direction += Vector3.right;
        }
        if (Input.GetButton("Jump"))
        {
            direction += Vector3.up;
        }
        if (Input.GetButton("Crouch"))
        {
            direction += Vector3.down;
        }

        return(direction);
    }
Exemple #6
0
    public override void Tick()
    {
        base.Tick();

        movementDirection = Vector3.zero;
        isSprinting       = false;
        isWalking         = false;

        if (UIMaster.GetState() == UIState.FOCUSED)
        {
            return;                                         //  Don't allow interaction while the UI is focused
        }
        if (GameMaster.GetState() != GameState.PLAYING)
        {
            return;                                             //  Don't allow interaction while the gamestate isn't in play mode
        }
        if (Input.GetButton("Forward"))
        {
            movementDirection += Vector3.forward;
        }
        if (Input.GetButton("Backward"))
        {
            movementDirection += Vector3.back;
        }
        if (Input.GetButton("Left"))
        {
            movementDirection += Vector3.left;
        }
        if (Input.GetButton("Right"))
        {
            movementDirection += Vector3.right;
        }

        if (movementDirection != Vector3.zero && Input.GetButton("Sprint") && HasEndurance(Constants.END_COST_SPRINT))
        {
            isSprinting = true;
        }

        if (movementDirection != Vector3.zero && Input.GetButton("Walk"))
        {
            isWalking = true;
        }

        if (isGrounded == true)
        {
            if (Input.GetButton("Jump") && CostModifyEndurance(Constants.END_COST_JUMP))
            {
                velocity.y = jumpStrength;

                isJumping = true;
            }
        }

        if (Input.GetButton("Attack"))
        {
            Attack();
        }

        RaycastHit hit;
        Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        // Offset interact range based on camera distance. Offset is relative to character facing, so invert it
        float range = interactRange;

        if (cameraMotor.view == CameraMotor.CameraView.THIRD_PERSON)
        {
            range += -cameraMotor.thirdPersonOffset.z;
        }
        else if (cameraMotor.view == CameraMotor.CameraView.FIRST_PERSON)
        {
            range += -cameraMotor.fpOffset.z;
        }

        if (Physics.Raycast(ray, out hit, range, interactableMask) == true)
        {
            Interactable component = hit.collider.gameObject.GetComponent <Interactable>();

            if (component != null)
            {
                UIMaster.GetHook("look_display").Enable();
                UIMaster.SendText(UITextType.SUBTEXT, component.GetDisplayText());

                if (Input.GetButton("Interact"))
                {
                    component.Interact(this);
                }
            }
            else
            {
                UIMaster.GetHook("look_display").Disable();
            }
        }

        if (Input.GetKeyDown(KeyCode.T))
        {
            Swordfish.Terrain terrain = GameObject.Find("Terrain").GetComponent <Swordfish.Terrain>();

            terrain.ModifyTerrain((int)transform.position.x, (int)transform.position.z, 1);
            terrain.ModifyTerrain((int)transform.position.x, (int)transform.position.z + 1, 1);
            terrain.ModifyTerrain((int)transform.position.x + 1, (int)transform.position.z + 1, 1);
            terrain.ModifyTerrain((int)transform.position.x + 1, (int)transform.position.z, 1);
            terrain.ModifyTerrain((int)transform.position.x - 1, (int)transform.position.z + 1, 1);
            terrain.ModifyTerrain((int)transform.position.x - 1, (int)transform.position.z, 1);
            terrain.ModifyTerrain((int)transform.position.x, (int)transform.position.z - 1, 1);
            terrain.ModifyTerrain((int)transform.position.x + 1, (int)transform.position.z - 1, 1);

            transform.position += new Vector3(0, 2, 0);
        }
        else if (Input.GetKeyDown(KeyCode.Y))
        {
            Swordfish.Terrain terrain = GameObject.Find("Terrain").GetComponent <Swordfish.Terrain>();

            terrain.ModifyTerrain((int)transform.position.x, (int)transform.position.z, -1);
            terrain.ModifyTerrain((int)transform.position.x, (int)transform.position.z + 1, -1);
            terrain.ModifyTerrain((int)transform.position.x + 1, (int)transform.position.z + 1, -1);
            terrain.ModifyTerrain((int)transform.position.x + 1, (int)transform.position.z, -1);
        }
    }