/// <summary>
/// Villager spawner.
/// this script controls the spawning of villagers, at points around the map
/// </summary>

using UnityEngine;
using System.Collections;

public class villagerSpawner : MonoBehaviour {

	public Transform[] villageSpawnsLeft;
	public Transform[] villageSpawnsRight;
	public GameObject[] villagers;
	public float spawnInt;
	private float currentTime = 0;
	private float nextSpawn = 0;
	private bool spawnLeft;

	// Use this for initialization
	void Start () {
		nextSpawn = spawnInt;
		if (Random.Range(0,2) == 1) {
			spawnLeft = true;
		}
	}
	
	// Update is called once per frame
	void Update () {
		currentTime += Time.deltaTime;

		if (currentTime >= nextSpawn) {
			nextSpawn += spawnInt;

			if (spawnLeft) {
				Instantiate (villagers[Random.Range(0, villagers.Length)], villageSpawnsLeft [Random.Range (0, villageSpawnsLeft.Length)].position, new Quaternion (0, 0, 0, 0));
			} else {
				Instantiate (villagers[Random.Range(0, villagers.Length)], villageSpawnsRight [Random.Range (0, villageSpawnsRight.Length)].position, new Quaternion (0, 0, 0, 0));
			}

			spawnLeft = !spawnLeft;
		}
	}
}
