#ifndef SPIRITBOMPARTICLESYSTEM
#define SPIRITBOMPARTICLESYSTEM
#include "particlesystem.h"
#include "util_vector.h"
#include "velocityparticle.h"
#include "cl_util.h"
#include "const.h"
#include "pm_shared.h"
#include "pmtrace.h"
#include "pm_defs.h"
#include "event_api.h"

#define NUMBER_OF_SBCHARGERS 200
template <class TParticle> class TSpiritBombParticleSystem:public TParticleSystem<TParticle>
{
public:	
	TSpiritBombParticleSystem(int ent, int type, vec3_t origin, HSPRITE sprite):TParticleSystem<TParticle>(ent,type)
	{	m_fRemove = false;
		m_hsprSprite = sprite;
		m_vecSpiritBombPos = origin;
		for (int i=0;i<NUMBER_OF_SBCHARGERS;i++)
		{	TParticle p(m_hsprSprite);
			SetupParticle(p);
			particles.push_back(p);
		}
	}

	/**
	 * Updates the physics for all particles in the system
	 * @param timeDiff time difference since last draw
	 */	 

	virtual void think(float time)
	{
		vector<TParticle>::iterator b = particles.begin();
 		while (b != particles.end())
 		{
  			if( (b->position - m_vecSpiritBombPos).Length()<10)
			{	SetupParticle(*b);
			}
			b->doThink(time);
   			b++;
   		}
 	}

private:
	void SetupParticle(TParticle &p)
	{	vec3_t vecDir;
		vecDir.x = gEngfuncs.pfnRandomFloat( -1, 1 );
		vecDir.y = gEngfuncs.pfnRandomFloat( -1, 1 );
		vecDir.z = -1;
		VectorNormalize(vecDir);
		pmtrace_t tr;
		gEngfuncs.pEventAPI->EV_PlayerTrace( m_vecSpiritBombPos, m_vecSpiritBombPos+vecDir*8192, PM_NORMAL, -1, &tr );
		p.position = tr.endpos;
		p.startPosition = tr.endpos;
		p.velocity = (m_vecSpiritBombPos - tr.endpos).Normalize() * gEngfuncs.pfnRandomFloat(100, 400);
		p.m_flStartTime = gEngfuncs.GetClientTime();
	}

	vec3_t m_vecSpiritBombPos;
	HSPRITE m_hsprSprite;
};
#endif