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

public class ItemSpawner : MonoBehaviour {

    public Vector3 size;

    public float spawnInterval;

    public GameObject[] itemsToSpawn;

    public int maxItem;

    int currentItemCount = 0;

    float delay;

    AudioSource audioSource;

    // Use this for initialization
    void Start () {
        delay = spawnInterval;
        audioSource = GetComponent<AudioSource>();
    }
	
	// Update is called once per frame
	void Update () {
        delay -= Time.deltaTime;
        if (delay <= 0)
        {
            if (currentItemCount < maxItem)
            {
                int itemId = Random.Range(0, itemsToSpawn.Length);

                GameObject itemGo = GameObject.Instantiate(itemsToSpawn[itemId],
                    new Vector3(Random.Range(-size.x, size.x),
                    Random.Range(-size.y, size.y),
                    Random.Range(-size.z, size.z)) / 2
                    + transform.position, Quaternion.identity);
                itemGo.GetComponent<Item>().spawner = this;

                currentItemCount++;
            }

            delay += spawnInterval;
        }
	}

    public void PickedUp()
    {
        currentItemCount--;
        audioSource.Play();
    }

    void OnDrawGizmosSelected()
    {
        Gizmos.color = new Color(0, 0, 1, 0.5f);
        Gizmos.DrawWireCube(transform.position, size);
    }
}
