using UnityEngine;
using System.Collections;

public class FadeAndDestroy : MonoBehaviour {

	#region Private Variables
	private float _startTime = -1f;
	private SpriteRenderer _renderer;
	private Color _targetColor = new Color (1f, 1f, 1f, 0f);

	[SerializeField]
	private TriggerAnimation _otherTrigger;

	[SerializeField]
	private float _duration = 3f;
	#endregion


	#region Events
	void Awake () {

		_renderer = GetComponent <SpriteRenderer> ();

	}

	void Update () {

		if (_startTime > 0f) {

			float t = (Time.time - _startTime) / _duration;
			_renderer.color = Color.Lerp (_renderer.color, _targetColor, t);

			if (t >= 1f)
				Destroy (gameObject);

		}
		
	}
	#endregion


	#region Methods
	public void Kill () {

		_startTime = Time.time;
		if (_otherTrigger != null)
			_otherTrigger.Trigger ();
		
	}
	#endregion

}
