using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class DarkLightScript : MonoBehaviour {

	// Use this for initialization
	void Start () {


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

	}



	void OnTriggerEnter2D(Collider2D col){



		//Activate Dark
		if(col.gameObject.layer == LayerMask.NameToLayer("Dark") || col.gameObject.layer == LayerMask.NameToLayer("Dark Ground")){


			if(col.renderer){
				col.renderer.enabled = true;
			}else{

				Renderer[] renderers = col.transform.GetComponentsInChildren<Renderer>();
				
				foreach(Renderer r in renderers){
					
					r.renderer.enabled = true;
					
				}

			}


			if(col.tag == "hurt stuff"){

			}else{
				col.collider2D.isTrigger = false;
			}

		}

		//turn off the renderer or the childrens renderers
		if(col.renderer && (col.gameObject.layer == LayerMask.NameToLayer("Light") || col.gameObject.layer == LayerMask.NameToLayer("LightWall"))){
			col.renderer.enabled = false;
			col.collider2D.isTrigger = true;
		}



	}

	void OnTriggerExit2D(Collider2D col){


		//Deactivate Dark
		if(col.gameObject.layer == LayerMask.NameToLayer("Dark") || col.gameObject.layer == LayerMask.NameToLayer("Dark Ground")){



			//Enable renderer or childrens renderers
			if(col.renderer){
				col.renderer.enabled = false;
			}else{
				
				if(col.transform.childCount > 0){
					
					Renderer[] renderers = col.transform.GetComponentsInChildren<Renderer>();
					
					foreach(Renderer r in renderers){
						
						r.renderer.enabled = false;
						
					}
					
				}
				
			}


			if(col.tag == "hurt stuff"){
				
			}else{
				col.collider2D.isTrigger = true;
			}


			
		}

		if(col.renderer && (col.gameObject.layer == LayerMask.NameToLayer("Light") || col.gameObject.layer == LayerMask.NameToLayer("LightWall"))){


			if(col.tag == "hurt stuff"){
				
			}else{
				col.collider2D.isTrigger = false;
			}

			col.renderer.enabled = true;
			col.collider2D.isTrigger = false;
		}

	}


}
