using UnityEngine;
using System.Collections;

public class spawnBullets : MonoBehaviour {

    private int numOfBullets;
    public GameObject bullet;
    public float[] spawnTime;
    private Transform spawnTrans;
    public bool isRandom;
    public float spawnTimeRand; //Time in seconds for Random InvokeRepeating
    public float diffScale; //Modifier for dificulty
    private float timer;
    private float spawnTimer;
    public float xRand1;
    public float xRand2;
    public float zRand1;
    public float zRand2;

	// Use this for initialization
	void Start () {
        spawnTrans = GetComponent<Transform>();
        numOfBullets = spawnTime.Length;
        if (isRandom)
        {

        }
        else {
            for (int i = 0; i < numOfBullets; i++)
            {
                if (spawnTime[i] != 0)
                {
                    Invoke("Spawn", spawnTime[i]);
                }
            }
        }
        timer = Time.time;
        spawnTimer = Time.time;
	}
	
	// Update is called once per frame
	void Update () {
        if (isRandom)
        {
            if (Time.time - timer >= 1)
            {
                //action
                spawnTimeRand = spawnTimeRand * diffScale;
                timer = Time.time;
            }
            if (Time.time - spawnTimer >= spawnTimeRand)
            {
                Spawn();
                spawnTimer = Time.time;
            }
        }
	}

    void Spawn()
    {
        if (isRandom)
        {
            Instantiate(bullet, new Vector3(spawnTrans.position.x - Random.Range(xRand1, xRand2), spawnTrans.position.y, spawnTrans.position.z - Random.Range(zRand1, zRand2)), spawnTrans.rotation);
        } else
        {
            Instantiate(bullet, new Vector3(spawnTrans.position.x, spawnTrans.position.y, spawnTrans.position.z), spawnTrans.rotation);
        }
    }
}
