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

public class Shooting : MonoBehaviour
{
    public List<Transform> turretBarrel;
    public GameObject round;
    public float Reload = 1;

    private bool CanShoot = true;
    private Collider2D tankColliders;
    private float currentDelay = 0;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log("Shooting");
        }
    }

    private void Awake()
    {
        tankColliders = GetComponentInParent<Collider2D>();
    }

    private void FixedUpdate()
    {
        if (CanShoot == false)
        {
            currentDelay -= Time.deltaTime;
            if (currentDelay <= 0)
            {
                CanShoot = true;
            }
        }
    }

    public void Shoot()
    {
        if (CanShoot)
        {
            CanShoot = false;
            currentDelay = Reload;

            foreach (var barrel in turretBarrel)
            {
                GameObject bullet = Instantiate(round);
                bullet.transform.position = barrel.position;
                bullet.transform.localRotation = barrel.rotation;
                bullet.GetComponent<Round>().Initialize();

                foreach (var collider in tankColliders)
                {
                    Physics2D.IgnoreCollision(round.GetComponent<Collider2D>(), collider);
                }
            }
        }
    }

}