﻿using UnityEngine;
using System.Collections;

public class AutoDestroyParticle : MonoBehaviour
{
	public float timeOut = 4.0f;
	Light _light;
	float _decreaseTime;
	bool _isRemoveLight = false;
	
	void Awake () {
		_light = GetComponentInChildren<Light>();
	}

	void Start () {
		_decreaseTime = _light.range/timeOut;
		Invoke("KillObject", timeOut);
	}
	
	void Update () {
		if (!_isRemoveLight) {
			if (_light.range > 0) {
				_light.range -= _decreaseTime*Time.deltaTime;
			} else {
				_isRemoveLight = true;
				GameObject.Destroy(_light);
			}
		}
	}
	
	void KillObject () {
		ParticleSystem[] fireExplosions = GetComponentsInChildren<ParticleSystem>();
		for (int i = 0; i < fireExplosions.Length; ++i) {
			if (fireExplosions[i] != null) {
				fireExplosions[i].Stop();
				fireExplosions[i].loop = false;
			}
		}
		GameObject.Destroy(gameObject);
	}
}