using UnityEngine;
using System.Collections;

public class FlowScript : MonoBehaviour {
	
	public ParticleSystem ps1,ps2;
	public TileScript ts1,ts2;
	public bool flowing = false;

	// Use this for initialization
	void Start () {
		ParticleSystem[] findit;
		findit = gameObject.GetComponentsInChildren<ParticleSystem>();
		foreach (ParticleSystem stashit in findit) {
			if (ps1 == null) {
				ps1 = stashit;
			} else {
				ps2 = stashit;
			}
		}
		GameObject[] getit;
		TileScript TS;
		getit = GameObject.FindGameObjectsWithTag("Tile");
		foreach (GameObject storeit in getit) {
			TS = storeit.GetComponent<TileScript>();
			if (TS.transform.position.x == transform.position.x) {
				if ((TS.transform.position.z + 0.5F) == transform.position.z) {
					ts1 = TS;
				} else if ((TS.transform.position.z - 0.5F) == transform.position.z) {
					ts2 = TS;
				}
			} else if (TS.transform.position.z == transform.position.z) {
				if ((TS.transform.position.x + 0.5F) == transform.position.x) {
					ts1 = TS;
				} else if ((TS.transform.position.x - 0.5F) == transform.position.x) {
					ts2 = TS;
				}
			}
		}
	}
	
	// Update is called once per frame
	void Update () {
		if (flowing) {
			if ((!ts1.powered) || (!ts2.powered)) {
				ps1.Stop ();
				ps2.Stop ();
				flowing = false;
			}
		} else {
			if ((ts1.powered) && (ts2.powered)) {
				ps1.Play ();
				ps2.Play ();
				flowing = true;
			}
		}
	}
}
