using UnityEngine;
using System.Collections;

public class WizzardRuneParticles : MonoBehaviour {

	public Color runeColor;

	ParticleSystem[] pSs;
	ParticleSystem.Particle[] particles;
	float deathTimer;

	void Awake () {
		pSs = GetComponentsInChildren<ParticleSystem>();
		particles = new ParticleSystem.Particle[pSs[0].maxParticles];
		foreach (ParticleSystem ps in pSs)
			ps.startColor = runeColor;
		deathTimer = 6f;
	}
	
	// Update is called once per frame
	void Update () {
		deathTimer -= Time.deltaTime;
		if (deathTimer < 0)
		{
			Destroy (gameObject);
		}
		Vector3 tp;
		foreach (ParticleSystem ps in pSs)
		{
			int pCount = ps.GetParticles(particles);
			//apply gravity effects
			for(int i=0;i<pCount;i++)
			{
				tp = (- particles[i].position);
				float mag = tp.magnitude;
				if (mag > 0.1f)
				{
					tp = tp/mag; //inverse - not inverse square
				}
				particles[i].velocity += tp * Time.deltaTime * 2.8f;
			}
			ps.SetParticles(particles,pCount);
		}
	}
}
