using UnityEngine;
using System.Collections;

public class LaserController : MonoBehaviour {


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