using UnityEngine;
using System.Collections;

public class Door : MonoBehaviour {

	public float timeOpenTime = 1.0f;
	public float timeClosedTime = 1.0f;
	public float randomPercentage = 0.05f;
	private bool doorOpen = false;
	private Animator doorAnimator; 
	private BoxCollider2D boxCollider;
	
	// Use this for initialization
	void Start () {
		doorAnimator = gameObject.GetComponent<Animator>();
		boxCollider = gameObject.GetComponent<BoxCollider2D>();
		
		StartCoroutine(DoorCoroutine());
		
	}
	
	IEnumerator DoorCoroutine()
	{
		while(true)
		{
			doorOpen = false;
			boxCollider.enabled = doorOpen;
			doorAnimator.SetBool("Engaged", doorOpen);
			yield return new WaitForSeconds(timeOpenTime + Random.Range(0, timeOpenTime * randomPercentage));
			doorOpen = true;
			boxCollider.enabled = doorOpen;
			doorAnimator.SetBool("Engaged", doorOpen);
			yield return new WaitForSeconds(timeClosedTime + Random.Range(0, timeClosedTime * randomPercentage));
		}
	}
}
