using UnityEngine;
using System.Collections;

public class ObstacleLogic : MonoBehaviour {
	public float timerMin = 8f;
	public float timerMax = 15f;
	public float actualTimer = 2f;

	Enemy enemy;

	void Awake()
	{
		enemy = gameObject.GetComponent<Enemy>();
	}

	void Update()
	{
		tickClock();
		checkTick();

		TearDown();
	}

	void tickClock()
	{
		actualTimer -= Time.deltaTime;
	}

	void checkTick()
	{
		if(actualTimer <= 0)
		{
			toggleState();
			actualTimer = Random.Range(timerMin,timerMax);
		}
	}

	void toggleState()
	{
		if(renderer.enabled)
		{
			renderer.enabled = false;
			collider2D.enabled = false;
		}
		else
		{
			renderer.enabled = true;
			collider2D.enabled = true;
		}
	}

	public void TearDown()
	{
		if(enemy.Health <= 0)
			Destroy(gameObject);
	}



}
