using UnityEngine;
using System.Collections;

public class ObjectScript : MonoBehaviour {

	public Sprite orig;
	public int isError = 0;
	public Sprite error;
	public Sprite hide;
	public int isHide = 0;
	public float isGoal = 0;
	public int isFamily = 0;
	AudioSource audsrc;
	public int isPlaying = 0;

	// Use this for initialization
	void Start () {
		audsrc = GetComponent<AudioSource>();
	}
	
	// Update is called once per frame
	void Update () {
		if (isHide == 1) {
			if (isPlaying == 0)
			{
				audsrc.Stop();
				audsrc.Play();
				isPlaying = 1;
			}
			if (isFamily == 0)
			{
				GetComponent<SpriteRenderer> ().sprite = hide;
			}
			if (isGoal == 1)
			{
				GameObject[] keys =  GameObject.FindGameObjectsWithTag("key");
				foreach (GameObject key in keys)
				{
					key.GetComponent<SpriteRenderer>().enabled = true;
				}
				GameObject.Find("Player").transform.rotation = new Quaternion (0f, -180f, 0f, 0f);
				PlayerMoveScript playScript = GameObject.Find("Player").GetComponent<PlayerMoveScript>();
				playScript.goal = 1;
				CameraScript camScript = GameObject.Find("Main Camera").GetComponent<CameraScript>();
				camScript.goal = 1;
			}
			foreach (Transform child in gameObject.transform) {
				if (child.tag == "key")
				{
					child.GetComponent<SpriteRenderer>().enabled = false;
				}
			}

			if (isFamily == 1)
			{
				PlayerMoveScript playScript = GameObject.Find("Player").GetComponent<PlayerMoveScript>();
				playScript.start = -2;
			}

		} if (isError == 1) {

			if (Input.GetKeyDown (KeyCode.Space))
			{
				Application.LoadLevel(Application.loadedLevel);
			}

			GetComponent<SpriteRenderer> ().sprite = error;
			foreach (Transform child in gameObject.transform) {
				if (child.tag == "key")
				{
					child.GetComponent<SpriteRenderer>().enabled = false;
				}
			}
		} 
		if (isError == 0 && isHide == 0 && isFamily == 0)
		{
			GetComponent<SpriteRenderer>().sprite = orig;
		}
		if (isError == 0 && isHide == 0) {
			isPlaying = 0;
		}
	}
}
