+/*
+ ParticleSpawner
+*/
+
+ParticleSpawner::ParticleSpawner(IGameDef* gamedef, scene::ISceneManager *smgr, LocalPlayer *player,
+ u16 amount, float time,
+ v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc,
+ float minexptime, float maxexptime, float minsize, float maxsize,
+ bool collisiondetection, AtlasPointer ap, u32 id)
+{
+ m_gamedef = gamedef;
+ m_smgr = smgr;
+ m_player = player;
+ m_amount = amount;
+ m_spawntime = time;
+ m_minpos = minpos;
+ m_maxpos = maxpos;
+ m_minvel = minvel;
+ m_maxvel = maxvel;
+ m_minacc = minacc;
+ m_maxacc = maxacc;
+ m_minexptime = minexptime;
+ m_maxexptime = maxexptime;
+ m_minsize = minsize;
+ m_maxsize = maxsize;
+ m_collisiondetection = collisiondetection;
+ m_ap = ap;
+ m_time = 0;
+
+ for (u16 i = 0; i<=m_amount; i++)
+ {
+ float spawntime = (float)rand()/(float)RAND_MAX*m_spawntime;
+ m_spawntimes.push_back(spawntime);
+ }
+
+ all_particlespawners.insert(std::pair<u32, ParticleSpawner*>(id, this));
+}
+
+ParticleSpawner::~ParticleSpawner() {}
+
+void ParticleSpawner::step(float dtime, ClientEnvironment &env)
+{
+ m_time += dtime;
+
+ if (m_spawntime != 0) // Spawner exists for a predefined timespan
+ {
+ for(std::vector<float>::iterator i = m_spawntimes.begin();
+ i != m_spawntimes.end();)
+ {
+ if ((*i) <= m_time && m_amount > 0)
+ {
+ m_amount--;
+
+ v3f pos = random_v3f(m_minpos, m_maxpos);
+ v3f vel = random_v3f(m_minvel, m_maxvel);
+ v3f acc = random_v3f(m_minacc, m_maxacc);
+ float exptime = rand()/(float)RAND_MAX
+ *(m_maxexptime-m_minexptime)
+ +m_minexptime;
+ float size = rand()/(float)RAND_MAX
+ *(m_maxsize-m_minsize)
+ +m_minsize;
+
+ new Particle(
+ m_gamedef,
+ m_smgr,
+ m_player,
+ env,
+ pos,
+ vel,
+ acc,
+ exptime,
+ size,
+ m_collisiondetection,
+ m_ap);
+ m_spawntimes.erase(i);
+ }
+ else
+ {
+ i++;
+ }
+ }
+ }
+ else // Spawner exists for an infinity timespan, spawn on a per-second base
+ {
+ for (int i = 0; i <= m_amount; i++)
+ {
+ if (rand()/(float)RAND_MAX < dtime)
+ {
+ v3f pos = random_v3f(m_minpos, m_maxpos);
+ v3f vel = random_v3f(m_minvel, m_maxvel);
+ v3f acc = random_v3f(m_minacc, m_maxacc);
+ float exptime = rand()/(float)RAND_MAX
+ *(m_maxexptime-m_minexptime)
+ +m_minexptime;
+ float size = rand()/(float)RAND_MAX
+ *(m_maxsize-m_minsize)
+ +m_minsize;
+
+ new Particle(
+ m_gamedef,
+ m_smgr,
+ m_player,
+ env,
+ pos,
+ vel,
+ acc,
+ exptime,
+ size,
+ m_collisiondetection,
+ m_ap);
+ }
+ }
+ }
+}
+
+void allparticlespawners_step (float dtime, ClientEnvironment &env)
+{
+ for(std::map<u32, ParticleSpawner*>::iterator i =
+ all_particlespawners.begin();
+ i != all_particlespawners.end();)
+ {
+ if (i->second->get_expired())
+ {
+ delete i->second;
+ all_particlespawners.erase(i++);
+ }
+ else
+ {
+ i->second->step(dtime, env);
+ i++;
+ }
+ }
+}
+
+void delete_particlespawner (u32 id)
+{
+ if (all_particlespawners.find(id) != all_particlespawners.end())
+ {
+ delete all_particlespawners.find(id)->second;
+ all_particlespawners.erase(id);
+ }
+}
+
+void clear_particles ()
+{
+ for(std::map<u32, ParticleSpawner*>::iterator i =
+ all_particlespawners.begin();
+ i != all_particlespawners.end();)
+ {
+ delete i->second;
+ all_particlespawners.erase(i++);
+ }
+
+ for(std::vector<Particle*>::iterator i =
+ all_particles.begin();
+ i != all_particles.end();)
+ {
+ (*i)->remove();
+ delete *i;
+ all_particles.erase(i);
+ }