using UnityEngine;
using System.Collections;

public class WizzardDeathParticles : MonoBehaviour {

	ParticleSystem ps;
	ParticleSystem.Particle[] particles;
	bool bInited;
	float deathTimer;

	void Awake () {
		bInited = false;
		ps = GetComponent<ParticleSystem>();
		particles = new ParticleSystem.Particle[ps.maxParticles];
		deathTimer = 6f;
	}

	void Update () {
		deathTimer -= Time.deltaTime;
		if (deathTimer < 0)
		{
			Destroy (gameObject);
		}
		int pCount = ps.GetParticles(particles);
		Vector3 tp = Vector3.zero;
		if (pCount > 0)
		{
			if (!bInited)
			{
				for(int i=0;i<pCount;i++)
				{
					particles[i].velocity += Quaternion.Euler (0,90,0) * (particles[i].position - transform.position) * 25f;
				}
				bInited = true;
			}
			else
			{
				//apply gravity effects
				for(int i=0;i<pCount;i++)
				{
					tp = (transform.position - particles[i].position);
					tp.y = 0f;
					Vector3 v = particles[i].velocity;
					v += tp.normalized * Time.deltaTime * 20f;
					v.y *= 0.9f;
					v.x *= 0.96f;
					v.z *= 0.96f;
					particles[i].velocity = v;
				}

			}
			ps.SetParticles(particles,pCount);
		}
	}
}
