using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class PlayerLook : MonoBehaviour
{
    public float recoil;
    public float recoilX;

    public float mouseSensitivity = 250f;

    public Camera cam;
    public Transform playerBody;

    float xRotation = 0f;

    private void Awake()
    {
        mouseSensitivity = PlayerPrefs.GetFloat("mouseSensitivity");
    }

    private void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.Escape) && Cursor.lockState == CursorLockMode.Locked)
        {
            Cursor.lockState = CursorLockMode.None;
        }
        else if(Input.GetKeyDown(KeyCode.Escape))
        {
            Cursor.lockState = CursorLockMode.Locked;
        }

        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation - recoil, -90f, 90f);
        
        mouseX -= recoilX;
        
        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        playerBody.Rotate(Vector3.up * mouseX);

        if(Input.GetMouseButton(1))
        {
            cam.fieldOfView = 30f;
        }
        else
        {
            cam.fieldOfView = 90f;
        }
    }

}