using UnityEngine;
using System.Collections;
using System.Threading;

public class SpawnerScript : MonoBehaviour
{
    [SerializeField]
    private GameObject _spawnee;
    [SerializeField]
    private int numPerSpawn;
    [SerializeField]
    private int numSpawns;
    [SerializeField]
    private float timeBetweenSpawns;
    [SerializeField]
    private float initialDelay;


    public void OnTriggerEnter2D(Collider2D coll) {
        Invoke("SpawnContinuous",initialDelay);
    }


    private void SpawnThing()
    {
        Instantiate(this._spawnee, new Vector2(this.transform.position.x + Random.value, this.transform.position.y + Random.value), Quaternion.identity, this.transform);
    }

    private void SpawnContinuous() {
        if (this.numSpawns <= 0) {
            Destroy(this);
            return;
        }
        this.numSpawns--;

        SpawnThing();

        Invoke("SpawnContinuous",this.timeBetweenSpawns);
    }
}