Skip to content

Commit b9fb3ce

Browse files
SmallJokernerzhul
authored andcommitted
ParticleSpawner::step cleanup and rotation fix (#6486)
* Particles: Move spawner code to a separate fucntion
1 parent ee0bfbe commit b9fb3ce

File tree

2 files changed

+75
-114
lines changed

2 files changed

+75
-114
lines changed

src/particles.cpp

+68-111
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,58 @@ ParticleSpawner::ParticleSpawner(IGameDef *gamedef, LocalPlayer *player,
281281
}
282282
}
283283

284+
void ParticleSpawner::spawnParticle(ClientEnvironment *env, float radius,
285+
bool is_attached, const v3f &attached_pos, float attached_yaw)
286+
{
287+
v3f ppos = m_player->getPosition() / BS;
288+
v3f pos = random_v3f(m_minpos, m_maxpos);
289+
290+
// Need to apply this first or the following check
291+
// will be wrong for attached spawners
292+
if (is_attached) {
293+
pos.rotateXZBy(attached_yaw);
294+
pos += attached_pos;
295+
}
296+
297+
if (pos.getDistanceFrom(ppos) > radius)
298+
return;
299+
300+
v3f vel = random_v3f(m_minvel, m_maxvel);
301+
v3f acc = random_v3f(m_minacc, m_maxacc);
302+
303+
if (is_attached) {
304+
// Apply attachment yaw
305+
vel.rotateXZBy(attached_yaw);
306+
acc.rotateXZBy(attached_yaw);
307+
}
308+
309+
float exptime = rand() / (float)RAND_MAX
310+
* (m_maxexptime - m_minexptime)
311+
+ m_minexptime;
312+
float size = rand() / (float)RAND_MAX
313+
* (m_maxsize - m_minsize)
314+
+ m_minsize;
315+
316+
m_particlemanager->addParticle(new Particle(
317+
m_gamedef,
318+
m_player,
319+
env,
320+
pos,
321+
vel,
322+
acc,
323+
exptime,
324+
size,
325+
m_collisiondetection,
326+
m_collision_removal,
327+
m_vertical,
328+
m_texture,
329+
v2f(0.0, 0.0),
330+
v2f(1.0, 1.0),
331+
m_animation,
332+
m_glow
333+
));
334+
}
335+
284336
void ParticleSpawner::step(float dtime, ClientEnvironment* env)
285337
{
286338
m_time += dtime;
@@ -302,128 +354,33 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env)
302354
}
303355
}
304356

305-
if (m_spawntime != 0) // Spawner exists for a predefined timespan
306-
{
307-
for(std::vector<float>::iterator i = m_spawntimes.begin();
308-
i != m_spawntimes.end();)
309-
{
310-
if ((*i) <= m_time && m_amount > 0)
311-
{
357+
if (m_spawntime != 0) {
358+
// Spawner exists for a predefined timespan
359+
for (std::vector<float>::iterator i = m_spawntimes.begin();
360+
i != m_spawntimes.end();) {
361+
if ((*i) <= m_time && m_amount > 0) {
312362
m_amount--;
313363

314364
// Pretend to, but don't actually spawn a particle if it is
315365
// attached to an unloaded object or distant from player.
316-
if (!unloaded) {
317-
v3f ppos = m_player->getPosition() / BS;
318-
v3f pos = random_v3f(m_minpos, m_maxpos);
319-
320-
// Need to apply this first or the following check
321-
// will be wrong for attached spawners
322-
if (is_attached)
323-
pos += attached_pos;
324-
325-
if (pos.getDistanceFrom(ppos) <= radius) {
326-
v3f vel = random_v3f(m_minvel, m_maxvel);
327-
v3f acc = random_v3f(m_minacc, m_maxacc);
328-
329-
if (is_attached) {
330-
// Apply attachment yaw and position
331-
pos.rotateXZBy(attached_yaw);
332-
vel.rotateXZBy(attached_yaw);
333-
acc.rotateXZBy(attached_yaw);
334-
}
335-
336-
float exptime = rand()/(float)RAND_MAX
337-
*(m_maxexptime-m_minexptime)
338-
+m_minexptime;
339-
float size = rand()/(float)RAND_MAX
340-
*(m_maxsize-m_minsize)
341-
+m_minsize;
342-
343-
Particle* toadd = new Particle(
344-
m_gamedef,
345-
m_player,
346-
env,
347-
pos,
348-
vel,
349-
acc,
350-
exptime,
351-
size,
352-
m_collisiondetection,
353-
m_collision_removal,
354-
m_vertical,
355-
m_texture,
356-
v2f(0.0, 0.0),
357-
v2f(1.0, 1.0),
358-
m_animation,
359-
m_glow);
360-
m_particlemanager->addParticle(toadd);
361-
}
362-
}
366+
if (!unloaded)
367+
spawnParticle(env, radius, is_attached, attached_pos, attached_yaw);
368+
363369
i = m_spawntimes.erase(i);
364-
}
365-
else
366-
{
370+
} else {
367371
++i;
368372
}
369373
}
370-
}
371-
else // Spawner exists for an infinity timespan, spawn on a per-second base
372-
{
374+
} else {
375+
// Spawner exists for an infinity timespan, spawn on a per-second base
376+
373377
// Skip this step if attached to an unloaded object
374378
if (unloaded)
375379
return;
376-
for (int i = 0; i <= m_amount; i++)
377-
{
378-
if (rand()/(float)RAND_MAX < dtime)
379-
{
380-
// Do not spawn particle if distant from player
381-
v3f ppos = m_player->getPosition() / BS;
382-
v3f pos = random_v3f(m_minpos, m_maxpos);
383-
384-
// Need to apply this first or the following check
385-
// will be wrong for attached spawners
386-
if (is_attached)
387-
pos += attached_pos;
388-
389-
if (pos.getDistanceFrom(ppos) <= radius) {
390-
v3f vel = random_v3f(m_minvel, m_maxvel);
391-
v3f acc = random_v3f(m_minacc, m_maxacc);
392-
393-
if (is_attached) {
394-
// Apply attachment yaw and position
395-
pos.rotateXZBy(attached_yaw);
396-
vel.rotateXZBy(attached_yaw);
397-
acc.rotateXZBy(attached_yaw);
398-
}
399-
400-
float exptime = rand()/(float)RAND_MAX
401-
*(m_maxexptime-m_minexptime)
402-
+m_minexptime;
403-
float size = rand()/(float)RAND_MAX
404-
*(m_maxsize-m_minsize)
405-
+m_minsize;
406-
407-
Particle* toadd = new Particle(
408-
m_gamedef,
409-
m_player,
410-
env,
411-
pos,
412-
vel,
413-
acc,
414-
exptime,
415-
size,
416-
m_collisiondetection,
417-
m_collision_removal,
418-
m_vertical,
419-
m_texture,
420-
v2f(0.0, 0.0),
421-
v2f(1.0, 1.0),
422-
m_animation,
423-
m_glow);
424-
m_particlemanager->addParticle(toadd);
425-
}
426-
}
380+
381+
for (int i = 0; i <= m_amount; i++) {
382+
if (rand() / (float)RAND_MAX < dtime)
383+
spawnParticle(env, radius, is_attached, attached_pos, attached_yaw);
427384
}
428385
}
429386
}

src/particles.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class Particle : public scene::ISceneNode
114114

115115
class ParticleSpawner
116116
{
117-
public:
117+
public:
118118
ParticleSpawner(IGameDef* gamedef,
119119
LocalPlayer *player,
120120
u16 amount,
@@ -140,8 +140,12 @@ class ParticleSpawner
140140
bool get_expired ()
141141
{ return (m_amount <= 0) && m_spawntime != 0; }
142142

143-
private:
144-
ParticleManager* m_particlemanager;
143+
private:
144+
void spawnParticle(ClientEnvironment *env, float radius,
145+
bool is_attached, const v3f &attached_pos,
146+
float attached_yaw);
147+
148+
ParticleManager *m_particlemanager;
145149
float m_time;
146150
IGameDef *m_gamedef;
147151
LocalPlayer *m_player;

0 commit comments

Comments
 (0)