using UnityEngine;
using System.Collections;

public class Button : MonoBehaviour {
	public GameObject player;
	public GameObject pushBlock;
	public bool destroyObjects = false;
	public GameObject destructableBlocks;
	public bool isEnabled = true;
	bool isAffectedByBlock = false;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	void OnTriggerEnter2D( Collider2D body ){
		if (body.gameObject.GetInstanceID() == player.GetInstanceID() || 
		    body.gameObject.GetInstanceID() == pushBlock.GetInstanceID()){
			if (destroyObjects){
				if (body.gameObject.GetInstanceID() == player.GetInstanceID() && isAffectedByBlock)
					return;
					
				if(body.gameObject.GetInstanceID() == pushBlock.GetInstanceID())
					isAffectedByBlock = true;
				//Destory objects, leaving this here in case it needs to do something else later
				foreach (Transform trans in destructableBlocks.transform){
					trans.renderer.enabled = false;
					trans.collider2D.enabled = false;
				}
			}
		}
	}
	
	void OnTriggerExit2D( Collider2D body ){
		if (body.gameObject.GetInstanceID() == player.GetInstanceID() || 
		    body.gameObject.GetInstanceID() == pushBlock.GetInstanceID()){
			if (destroyObjects){
				if (body.gameObject.GetInstanceID() == player.GetInstanceID() && isAffectedByBlock)
					return;
					
				if(body.gameObject.GetInstanceID() == pushBlock.GetInstanceID())
					isAffectedByBlock = false;
					
					
				//Destory objects, leaving this here in case it needs to do something else later
				foreach (Transform trans in destructableBlocks.transform){
					trans.renderer.enabled = true;
					trans.collider2D.enabled = true;
				}
			}
		}
	}
}
