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

public class PlayerController : Character
{
    public VariableJoystick joystick;
    public CharacterController characterController;
    public Canvas inputCanvas;
    public bool isJoystick;
    public float movementSpeed;
    public float rotationSpeed;

    [SerializeField] private Transform firePoint;
    [SerializeField] private GameObject weaponPrefab;
    [SerializeField] private GameObject target;

    private bool canShoot = false;

    public void Start()
    {
        EnableJoystickInput();
        characterController = GetComponent<CharacterController>();
    }
    private void Update()
    {
        Move();
        if (canShoot)
        {
            //Shoot();
        }
        canShoot = false;
    }
    public void EnableJoystickInput()
    {
        isJoystick = true;
        inputCanvas.gameObject.SetActive(true);

    }
    public void Move()
    {
        if (joystick)
        {
            Vector3 moveDirection = new Vector3(joystick.Direction.x, 0.0f, joystick.Direction.y);
            characterController.SimpleMove(moveDirection * movementSpeed);
            if (moveDirection.sqrMagnitude <= 0)
            {
                animator.SetBool("IsIdle", true);
                animator.SetBool("IsRun", false);
                canShoot = true;
                return;
            }
            animator.SetBool("IsIdle", false);
            animator.SetBool("IsRun", true);
            Vector3 targetDirection = Vector3.RotateTowards(characterController.transform.forward, moveDirection, rotationSpeed * Time.deltaTime, 0.0f);
            characterController.transform.rotation = Quaternion.LookRotation(targetDirection);
        }
    }
    public void Shoot()
    {
        GameObject weapon = Instantiate(weaponPrefab, firePoint.position, Quaternion.identity);
        weapon.transform.position = Vector3.MoveTowards(firePoint.position, target.transform.position, 10);

    }
}
