using UnityEngine;
using UnityEngine.InputSystem;

public class playerController : MonoBehaviour
{
    public float walkSpeed = 5.0f;
    public float runSpeed = 10.0f;
    public float jumpForce = 5.0f;
    public GameObject cameraObject;

    private InputActionAsset runButton;
    private bool running = false;
    private float moveSpeed;
    private Vector2 moveValue;
    private Vector2 lookValue;
    private Rigidbody playerRB;
    
    // Start is called before the first frame update
    void Start()
    {
        cameraObject = GameObject.FindGameObjectWithTag("MainCamera");
        playerRB = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {

        if (running)
        {
            moveSpeed = runSpeed;
        }
        else
        {
            moveSpeed = walkSpeed;
        }
        
        //Move player according to the value from OnMove()
        transform.Translate(new Vector3(moveValue.x, 0, moveValue.y) * (moveSpeed * Time.deltaTime));
        
        //Rotate camera and player according to the value from OnLook()
        cameraObject.transform.Rotate(new Vector3(-lookValue.y, 0f, 0f), Space.Self);
        transform.Rotate(new Vector3(0f, lookValue.x, 0f), Space.World);
    }

    void OnMove(InputValue value)
    {
        moveValue = value.Get<Vector2>();
    }

    void OnLook(InputValue value)
    {
        lookValue = value.Get<Vector2>();
    }

    void OnJump(InputValue value)
    {
        playerRB.AddForce(new Vector3(0, jumpForce, 0), ForceMode.Impulse);
    }

}
