/// <summary>
/// Animal spawner.
/// spawns animals in an area.
/// </summary>

using UnityEngine;
using System.Collections;

public class animalSpawner : MonoBehaviour {

	public GameObject[] animals; 
	public float spawnInt;
	private float currentTime = 0;
	private float nextSpawn = 0;

	private gameController gameController;

	private BoxCollider2D area;

	// Use this for initialization
	void Start () {
		nextSpawn = spawnInt;
		area = gameObject.GetComponent<BoxCollider2D> ();
		gameController = GameObject.Find ("GameController").GetComponent<gameController>();
	}

	// Update is called once per frame
	void Update () {


		if(currentTime <= nextSpawn)
			currentTime += Time.deltaTime;

		if (currentTime >= nextSpawn && gameController.totalAnimals <= gameController.maxAnimals) {
			nextSpawn += spawnInt;

			gameController.totalAnimals++;

			GameObject animal = Instantiate (animals[Random.Range(0, animals.Length)], new Vector3(Random.Range(area.bounds.min.x, area.bounds.max.x), 
				Random.Range(area.bounds.min.y, area.bounds.max.y)), 
				new Quaternion (0, 0, 0, 0)) as GameObject;

			animal.GetComponent<animalMove>().moveZone = area;

		}
	}
}
