using UnityEngine;
using System.Collections;

public class disasters : MonoBehaviour {



	public BoxCollider2D blueZone;
	public BoxCollider2D redZone;

	public gameController gm;

	public ParticleSystem famineParticles;
	public ParticleSystem repentParticles;

	private GameObject[] animals;


	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKeyDown(KeyCode.W)){
			gameObject.SendMessage("RunRepentance", 2);
		}
	}

	void RunFamine(float p){
		if (p == 1) {
			animals = GameObject.FindGameObjectsWithTag ("Animal");

			foreach (GameObject a in animals) {
				if (a.transform.position.x >= blueZone.bounds.min.x && a.transform.position.x <= blueZone.bounds.max.x) {
					gm.SendMessage ("KillAnimal");
					Destroy (a, 2);
				}
			}

			famineParticles.Play ();

		} else if(p == 2) {
			animals = GameObject.FindGameObjectsWithTag("Animal");

			foreach (GameObject a in animals) {
				if (a.transform.position.x >= redZone.bounds.min.x && a.transform.position.x <= redZone.bounds.max.x) {
					gm.SendMessage ("KillAnimal");
					Destroy (a, 2);

				}
			}

			famineParticles.Play ();

		}
	}

	void RunRepentance(float p){
		if (p == 1) {
			animals = GameObject.FindGameObjectsWithTag ("Animal");

			foreach (GameObject a in animals) {
				if (a.transform.position.x >= blueZone.bounds.min.x && a.transform.position.x <= blueZone.bounds.max.x) {
					a.SendMessage ("Repent",1);
				}
			}

			repentParticles.Play ();

		} else if(p == 2) {
			animals = GameObject.FindGameObjectsWithTag("Animal");

			foreach (GameObject a in animals) {
				if (a.transform.position.x >= redZone.bounds.min.x && a.transform.position.x <= redZone.bounds.max.x) {
					a.SendMessage ("Repent", 2);
				}
			}

			repentParticles.Play ();

		}
	}

	void RunHaunting(){
	//	Debug.Log ("Running the haunting");
		StartCoroutine(Haunt());
	}

	IEnumerator Haunt(){
		gameObject.SendMessage ("RecieveObj", 4);
		yield return new WaitForSeconds (1);
		gameObject.SendMessage ("RecieveObj", 4);
		yield return new WaitForSeconds (1);
		gameObject.SendMessage ("RecieveObj", 4);
		yield return new WaitForSeconds (1);
		gameObject.SendMessage ("RecieveObj", 4);
		yield return new WaitForSeconds (1);
		gameObject.SendMessage ("RecieveObj", 4);
		yield return new WaitForSeconds (1);
		/*gameObject.SendMessage ("RecieveObj", 4);
		yield return new WaitForSeconds (1);
		gameObject.SendMessage ("RecieveObj", 4);
		yield return new WaitForSeconds (1);
		gameObject.SendMessage ("RecieveObj", 4);
		yield return new WaitForSeconds (1);
		gameObject.SendMessage ("RecieveObj", 4);
		yield return new WaitForSeconds (1);
		gameObject.SendMessage ("RecieveObj", 4);*/
	}

}
