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

public class Attack : MonoBehaviour
{
    [SerializeField]
    Bullet bulletPrefab;

    PlayerController player;

    float attackCooldown = 0.5f;
    bool canAttack = true;
    bool IsAttacking { get; set; }

    Coroutine attackCo;

    private void Awake()
    {
        player = GetComponent<PlayerController>();
    }

    public void ActivateAttack()
    {
        if (canAttack)
        {
            if(attackCo == null)
            {
                attackCo = StartCoroutine(Attacking());
            }
            
        }
    }


    IEnumerator Attacking()
    {
        IsAttacking = true;
        while (player.controlInputs.RightTriggerPressed == true)
        {
            if(player.controlInputs.RightTriggerPressed == true)
            {
                SpawnBullet();
            }
            yield return new WaitForSeconds(attackCooldown);
        }

        IsAttacking = false;
        attackCo = null;
    }

    void SpawnBullet()
    {
        Bullet bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
        bullet.InitBullet(player, player.controlInputs.RightJoystickInputVector);
    }
}
