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

public class GenieDeathParticleController : MonoBehaviour {

	public ParticleSystem rockmanPrefab;
	public float pewSpeed;
	public float delay;
	public static Color baseColor;
	float timer;
	int spawnCount;

	List<ParticleSystem> rockmanPews;

	// Use this for initialization
	void Start () {
		timer = 0.1f;
		rockmanPews = new List<ParticleSystem>();
		spawnCount = 0;
	}
	
	// Update is called once per frame
	void Update () {
		timer += Time.deltaTime;
		if ((timer > delay)&& (spawnCount < 3))
		{
			timer -= delay;
			//spawn prefabs
			for (int i=0;i<8;++i)
			{
				ParticleSystem ps = Instantiate<ParticleSystem>(rockmanPrefab);
				ps.transform.SetParent(transform);
				ps.transform.localPosition = Vector3.zero;
				ps.startColor = baseColor;
				rockmanPews.Add (ps);
			}
			spawnCount++;
		}
		for (int i=0;i<rockmanPews.Count;i++)
		{
			Vector3 v = (Quaternion.Euler(0,0,(45*spawnCount + 360f*(i%8)/8f)) * Vector3.up) * pewSpeed * Time.deltaTime;
			rockmanPews[i].transform.position = rockmanPews[i].transform.position + v;
			if (timer > .5f)
				rockmanPews[i].enableEmission = false;
		}
		if (timer > 3f)
			Destroy (gameObject);
	}
}
