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

public class HarpoonShots : MonoBehaviour
{
    public GameObject harpoons;
    public GameObject harpoonsOrigin;
    public float nextFire;
    public float fireRate;
    private float maxBullets;
    private float currentBullets;
    // Use this for initialization
    void Start()
    {
        StartCoroutine(Autofire());
        currentBullets = 0;
        maxBullets = 5;
    }

    void Shoot()
    {
        if (Input.GetKey(KeyCode.Mouse0) && Time.time > nextFire)
        {
            {
                var objectPos = Camera.main.WorldToScreenPoint(harpoonsOrigin.transform.position);
                var dir = Input.mousePosition - objectPos;
                nextFire = Time.time + fireRate;
                Debug.Log("Shoot");
                Instantiate(harpoons, this.transform.position, Quaternion.Euler(new Vector3(0, 0, Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg + 90)));
            }
        }
    }


    IEnumerator Autofire()
    {
        if (Time.time > nextFire && maxBullets > currentBullets)
        {
            var objectPos = Camera.main.WorldToScreenPoint(harpoonsOrigin.transform.position);
            var dir = Input.mousePosition - objectPos;
            nextFire = Time.time + fireRate; 
            Instantiate(harpoons, this.transform.position, Quaternion.Euler(new Vector3(0, 0, Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg + 90)));
            yield return new WaitForSeconds(2);
        }
    }

    void Update()
    {
       // Autofire();
        Shoot();
    }
}