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

public class movementControls : MonoBehaviour
{
    public float moveSpeed = 5f;
    public float mouseSensitivity = 100f;

    private Transform playerBody;
    private float xRotation = 0f;
    private float yRotation = 0f;

    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        playerBody = transform;

        Quaternion targetRotation = Quaternion.Euler(0, 0, 0);

        playerBody.rotation = Quaternion.RotateTowards(playerBody.rotation, Quaternion.Euler(0, 0, 0), 0f);
    }

    // Update is called once per frame
    void Update()
    {
        // Movement input
        float moveX = Input.GetAxis("Horizontal");  //[W][S]
        float moveZ = Input.GetAxis("Vertical");    //[A][D]    
        //playerBody.position = playerBody.position + new Vector3(moveX * moveSpeed * Time.deltaTime, 0, moveZ * moveSpeed * Time.deltaTime);

                // Get the user input for horizontal and vertical movement
                //float horizontalInput = Input.GetAxis("Horizontal");
                //float verticalInput = Input.GetAxis("Vertical");

                // Get the camera's forward and right vectors, we'll use them to move along X and Z axes
                Vector3 forward = transform.forward;
                Vector3 right = transform.right;

                // Set the y component of forward and right vectors to 0 to ignore vertical movement
                forward.y = 0f;
                right.y = 0f;

                // Normalize vectors to ensure consistent speed regardless of camera rotation
                forward.Normalize();
                right.Normalize();

                // Calculate the movement direction based on user input and camera rotation
                Vector3 moveDirection = forward * moveZ + right * moveX;

                // Apply the movement direction to the camera's position
                transform.position += moveDirection * moveSpeed * Time.deltaTime;


        //newX = Mathf.Clamp(newX, -500, 500);
        //newZ = Mathf.Clamp(newZ, -500, 500);




        // Mouse input
        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;    //[Up][Down]
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;    //[Left][Right]

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);
        yRotation += mouseX;
        transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0f);
    }
}
