using UnityEngine;

public class ObjectFactory : MonoBehaviour
{
	public GameObject[] objects;
	public float spawnTime;
	public Rect area;

	private GameManager gameManager;
	private float lastSpawnTime;

	private void Awake()
	{
		gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
	}

	private void Start()
	{
		lastSpawnTime = 0f;
	}

	private void Update()
	{
		if(gameManager.IsPaused())
		{
			return;
		}

		lastSpawnTime += Time.deltaTime;

		if(lastSpawnTime > spawnTime)
		{
			Spawn();
			lastSpawnTime = 0f;
		}
	}

	public void Spawn()
	{
		var index = Random.Range(0, objects.Length);
		var x = Random.Range(0, area.width) - (area.width / 2f) + this.transform.position.x;
		var z = Random.Range(0, area.height) - (area.height / 2f) + this.transform.position.z;

		Object.Instantiate(objects[index], new Vector3(x, this.transform.position.y, z), Quaternion.Euler(0, 90, 0));
	}

	private void OnDrawGizmosSelected()
	{
        Gizmos.color = new Color(1, 0, 0, 0.5F);
        Gizmos.DrawCube(transform.position, new Vector3(area.width, 1, area.height));
    }
}
