using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PlanetScript : MonoBehaviour {

	public int numExplosions;
	public List<GameObject> listExplosions;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	IEnumerator spawnExplosions(){
		for (int i = 0; i < numExplosions; i++) {
			GameObject newExplosion = ExplosionInstantiatorScript.instance.InstantiateExplosion (0, this.transform.position, 1.5f, 999);
			listExplosions.Add(newExplosion);
			yield return new WaitForSeconds (Random.Range(0.2f,0.4f)); 
		}
	}

	public virtual void DestroyMe()
	{
	}

	public virtual void PrepareToDestroyYourself(){
		foreach (GameObject e in listExplosions) {
			Destroy (e);
		}
		listExplosions.Clear ();
		ExplosionInstantiatorScript.instance.InstantiateExplosion (1, this.transform.position, 0, 0);
		OnDestroyMe ();
		Destroy (this.gameObject);
	}

	public virtual void OnDestroyMe()
	{
	}

	public virtual void FinallyDestroy()
	{
	}

	IEnumerator DestructionRoutine(){
		StartCoroutine ("spawnExplosions");
		yield return new WaitForSeconds (0.4f*numExplosions);
		PrepareToDestroyYourself ();
	}
}
