ManaPlus
Public Member Functions | Protected Member Functions | Protected Attributes | Private Attributes | Friends
Particle Class Reference

#include <particle.h>

Inheritance diagram for Particle:
Actor ImageParticle TextParticle AnimationParticle RotationalParticle

Public Member Functions

 Particle ()
 
 ~Particle ()
 
void clear ()
 
bool update ()
 
void draw (Graphics *const graphics, const int offsetX, const int offsetY) const
 
int getPixelY () const
 
int getSortPixelY () const
 
ParticleaddEffect (const std::string &particleEffectFile, const int pixelX, const int pixelY, const int rotation)
 
void addEmitter (ParticleEmitter *const emitter)
 
void moveTo (const Vector &pos)
 
void moveTo (const float x, const float y)
 
void moveBy (const Vector &change)
 
void setLifetime (const int lifetime)
 
void setFadeOut (const int fadeOut)
 
void setFadeIn (const int fadeIn)
 
void setVelocity (const float x, const float y, const float z)
 
void setGravity (const float gravity)
 
void setRandomness (const int r)
 
void setBounce (const float bouncieness)
 
void setFollow (const bool follow)
 
bool doesFollow () const
 
void setDestination (Particle *const target, const float accel, const float moment)
 
void setDieDistance (const float dist)
 
void adjustEmitterSize (const int w, const int h)
 
void setAllowSizeAdjust (const bool adjust)
 
bool isAlive () const
 
void prepareToDie ()
 
bool isExtinct () const
 
void kill ()
 
void disableAutoDelete ()
 
int getNumberOfLayers () const
 
float getAlpha () const
 
void setAlpha (const float alpha)
 
virtual void setDeathEffect (const std::string &effectFile, const signed char conditions)
 
void setActor (const BeingId actor)
 
- Public Member Functions inherited from Actor
virtual ~Actor ()
 
virtual int getWidth () const
 
virtual int getHeight () const
 
const VectorgetPixelPositionF () const
 
virtual void setPixelPositionF (const Vector &pos)
 
int getPixelX () const
 
virtual int getTileX () const
 
virtual int getTileY () const
 
virtual void setMap (Map *const map)
 
const MapgetMap () const
 

Protected Member Functions

void updateSelf ()
 
- Protected Member Functions inherited from Actor
 Actor ()
 

Protected Attributes

float mAlpha
 
int mLifetimeLeft
 
int mLifetimePast
 
int mFadeOut
 
int mFadeIn
 
Vector mVelocity
 
AliveStatusT mAlive
 
ParticleTypeT mType
 
SimpleAnimationmAnimation
 
ImagemImage
 
BeingId mActor
 
- Protected Attributes inherited from Actor
MapmMap
 
Vector mPos
 
int mYDiff
 

Private Attributes

Emitters mChildEmitters
 
Particles mChildParticles
 
Particles mChildMoveParticles
 
std::string mDeathEffect
 
float mGravity
 
float mBounce
 
float mAcceleration
 
float mInvDieDistance
 
float mMomentum
 
ParticlemTarget
 
int mRandomness
 
signed char mDeathEffectConditions
 
bool mAutoDelete
 
bool mAllowSizeAdjust
 
bool mFollow
 

Friends

class ParticleEngine
 

Additional Inherited Members

- Data Fields inherited from Actor
int mPixelX
 
int mPixelY
 

Detailed Description

A particle spawned by a ParticleEmitter.

Definition at line 44 of file particle.h.

Constructor & Destructor Documentation

◆ Particle()

Particle::Particle ( )

Definition at line 58 of file particle.cpp.

58  :
59  Actor(),
60  mAlpha(1.0F),
61  mLifetimeLeft(-1),
62  mLifetimePast(0),
63  mFadeOut(0),
64  mFadeIn(0),
65  mVelocity(),
68  mAnimation(nullptr),
69  mImage(nullptr),
74  mDeathEffect(),
75  mGravity(0.0F),
76  mBounce(0.0F),
77  mAcceleration(0.0F),
78  mInvDieDistance(-1.0F),
79  mMomentum(1.0F),
80  mTarget(nullptr),
81  mRandomness(0),
83  mAutoDelete(true),
84  mAllowSizeAdjust(false),
85  mFollow(false)
86 {
88 }

References ParticleEngine::particleCount.

◆ ~Particle()

Particle::~Particle ( )

Destructor.

Definition at line 90 of file particle.cpp.

91 {
92  if (mActor != BeingId_zero &&
93  (actorManager != nullptr))
94  {
95  ActorSprite *const actor = actorManager->findActor(mActor);
96  if (actor != nullptr)
97  actor->controlParticleDeleted(this);
98  }
99  // Delete child emitters and child particles
100  clear();
102  if (mImage != nullptr)
103  {
104  if (mType == ParticleType::Image)
105  {
106  const std::string &restrict name = mImage->mIdPath;
110  {
111  int &cnt = (*it).second;
112  if (cnt > 0)
113  cnt --;
114  }
115  mImage->decRef();
116  }
117  mImage = nullptr;
118  }
119 
121 }

References actorManager, BeingId_zero, clear(), ActorSprite::controlParticleDeleted(), Image::decRef(), delete2, ActorManager::findActor(), ParticleType::Image, ImageParticle::imageParticleCountByName, mActor, mAnimation, Resource::mIdPath, mImage, mType, ParticleEngine::particleCount, and restrict.

Member Function Documentation

◆ addEffect()

Particle * Particle::addEffect ( const std::string &  particleEffectFile,
const int  pixelX,
const int  pixelY,
const int  rotation 
)

Creates a child particle that hosts some emitters described in the particleEffectFile.

Definition at line 410 of file particle.cpp.

413 {
414  Particle *newParticle = nullptr;
415 
416  const size_t pos = particleEffectFile.find('|');
417  const std::string dyePalettes = (pos != std::string::npos)
418  ? particleEffectFile.substr(pos + 1) : "";
419  XML::Document *doc = Loader::getXml(particleEffectFile.substr(0, pos),
422  if (doc == nullptr)
423  return nullptr;
424  XmlNodeConstPtrConst rootNode = doc->rootNode();
425 
426  if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "effect"))
427  {
428  logger->log("Error loading particle: %s", particleEffectFile.c_str());
429  doc->decRef();
430  return nullptr;
431  }
432 
433  // Parse particles
434  for_each_xml_child_node(effectChildNode, rootNode)
435  {
436  // We're only interested in particles
437  if (!xmlNameEqual(effectChildNode, "particle"))
438  continue;
439 
440  // Determine the exact particle type
441  XmlNodePtr node;
442 
443  // Animation
444  if ((node = XML::findFirstChildByName(effectChildNode, "animation")) !=
445  nullptr)
446  {
447  newParticle = new AnimationParticle(node, dyePalettes);
448  newParticle->setMap(mMap);
449  }
450  // Rotational
451  else if ((node = XML::findFirstChildByName(
452  effectChildNode, "rotation")) != nullptr)
453  {
454  newParticle = new RotationalParticle(node, dyePalettes);
455  newParticle->setMap(mMap);
456  }
457  // Image
458  else if ((node = XML::findFirstChildByName(effectChildNode,
459  "image")) != nullptr)
460  {
461  std::string imageSrc;
462  if (XmlHaveChildContent(node))
463  imageSrc = XmlChildContent(node);
464  if (!imageSrc.empty() && !dyePalettes.empty())
465  Dye::instantiate(imageSrc, dyePalettes);
466  Image *const img = Loader::getImage(imageSrc);
467 
468  newParticle = new ImageParticle(img);
469  newParticle->setMap(mMap);
470  }
471  // Other
472  else
473  {
474  newParticle = new Particle;
475  newParticle->setMap(mMap);
476  }
477 
478  // Read and set the basic properties of the particle
479  const float offsetX = XML::getFloatProperty(
480  effectChildNode, "position-x", 0);
481  const float offsetY = XML::getFloatProperty(
482  effectChildNode, "position-y", 0);
483  const float offsetZ = XML::getFloatProperty(
484  effectChildNode, "position-z", 0);
485  const Vector position(mPos.x + static_cast<float>(pixelX) + offsetX,
486  mPos.y + static_cast<float>(pixelY) + offsetY,
487  mPos.z + offsetZ);
488  newParticle->moveTo(position);
489 
490  const int lifetime = XML::getProperty(effectChildNode, "lifetime", -1);
491  newParticle->setLifetime(lifetime);
492  const bool resizeable = "false" != XML::getProperty(effectChildNode,
493  "size-adjustable", "false");
494 
495  newParticle->setAllowSizeAdjust(resizeable);
496 
497  // Look for additional emitters for this particle
498  for_each_xml_child_node(emitterNode, effectChildNode)
499  {
500  if (xmlNameEqual(emitterNode, "emitter"))
501  {
502  ParticleEmitter *restrict const newEmitter =
503  new ParticleEmitter(
504  emitterNode,
505  newParticle,
506  mMap,
507  rotation,
508  dyePalettes);
509  newParticle->addEmitter(newEmitter);
510  }
511  else if (xmlNameEqual(emitterNode, "deatheffect"))
512  {
513  std::string deathEffect;
514  if ((node != nullptr) && XmlHaveChildContent(node))
515  deathEffect = XmlChildContent(emitterNode);
516 
517  char deathEffectConditions = 0x00;
518  if (XML::getBoolProperty(emitterNode, "on-floor", true))
519  {
520  deathEffectConditions += CAST_S8(
522  }
523  if (XML::getBoolProperty(emitterNode, "on-sky", true))
524  {
525  deathEffectConditions += CAST_S8(
527  }
528  if (XML::getBoolProperty(emitterNode, "on-other", false))
529  {
530  deathEffectConditions += CAST_S8(
532  }
533  if (XML::getBoolProperty(emitterNode, "on-impact", true))
534  {
535  deathEffectConditions += CAST_S8(
537  }
538  if (XML::getBoolProperty(emitterNode, "on-timeout", true))
539  {
540  deathEffectConditions += CAST_S8(
542  }
543  newParticle->setDeathEffect(
544  deathEffect, deathEffectConditions);
545  }
546  }
547 
548  mChildParticles.push_back(newParticle);
549  }
550 
551  doc->decRef();
552  return newParticle;
553 }

References addEmitter(), CAST_S8, AliveStatus::DEAD_FLOOR, AliveStatus::DEAD_IMPACT, AliveStatus::DEAD_OTHER, AliveStatus::DEAD_SKY, AliveStatus::DEAD_TIMEOUT, Resource::decRef(), XML::findFirstChildByName(), for_each_xml_child_node, XML::getBoolProperty(), XML::getFloatProperty(), Loader::getImage(), XML::getProperty(), Loader::getXml(), Dye::instantiate(), Logger::log(), logger, Ea::GameRecv::mMap, moveTo(), anonymous_namespace{mrand.cpp}::mPos, restrict, XML::Document::rootNode(), setAllowSizeAdjust(), setDeathEffect(), setLifetime(), Actor::setMap(), SkipError_false, and UseVirtFs_true.

◆ addEmitter()

void Particle::addEmitter ( ParticleEmitter *const  emitter)
inline

Adds an emitter to the particle.

Definition at line 100 of file particle.h.

101  { mChildEmitters.push_back(emitter); }

References mChildEmitters.

Referenced by addEffect(), ParticleEngine::addEffect(), and ParticleEmitter::createParticles().

◆ adjustEmitterSize()

void Particle::adjustEmitterSize ( const int  w,
const int  h 
)

Changes the size of the emitters so that the effect fills a rectangle of this size

Definition at line 555 of file particle.cpp.

556 {
557  if (mAllowSizeAdjust)
558  {
560  (*e)->adjustSize(w, h);
561  }
562 }

References FOR_EACH.

Referenced by Map::initializeParticleEffects().

◆ clear()

void Particle::clear ( )

Deletes all child particles and emitters.

Definition at line 581 of file particle.cpp.

582 {
584  mChildEmitters.clear();
585 
587  mChildParticles.clear();
588 
589  mChildMoveParticles.clear();
590 }

References delete_all(), mChildEmitters, mChildMoveParticles, and mChildParticles.

Referenced by ~Particle().

◆ disableAutoDelete()

void Particle::disableAutoDelete ( )
inline

After calling this function the particle will only request deletion when kill() is called

Definition at line 226 of file particle.h.

227  { mAutoDelete = false; }

References mAutoDelete.

Referenced by ActorSprite::controlCustomParticle(), and ParticleVector::setLocally().

◆ doesFollow()

bool Particle::doesFollow ( ) const
inline

Gets the flag if the particle is supposed to be moved by its parent

Definition at line 175 of file particle.h.

176  { return mFollow; }

References mFollow.

◆ draw()

void Particle::draw ( Graphics *const  graphics,
const int  offsetX,
const int  offsetY 
) const
virtual

Draws the particle image.

Implements Actor.

Reimplemented in TextParticle.

Definition at line 123 of file particle.cpp.

126 {
127 }

◆ getAlpha()

float Particle::getAlpha ( ) const
inlinevirtual

Returns the current alpha value used to draw the actor.

Implements Actor.

Definition at line 233 of file particle.h.

234  { return 1.0F; }

◆ getNumberOfLayers()

int Particle::getNumberOfLayers ( ) const
inlinevirtual

We consider particles (at least for now) to be one layer-sprites

Reimplemented from Actor.

Definition at line 230 of file particle.h.

231  { return 1; }

◆ getPixelY()

int Particle::getPixelY ( ) const
inlinevirtual

Necessary for sorting with the other sprites.

Reimplemented from Actor.

Reimplemented in TextParticle.

Definition at line 79 of file particle.h.

80  { return CAST_S32(mPos.y) - 16; }

References CAST_S32, Actor::mPos, and Vector::y.

◆ getSortPixelY()

int Particle::getSortPixelY ( ) const
inlinevirtual

Necessary for sorting with the other sprites for sorting only.

Reimplemented from Actor.

Reimplemented in TextParticle.

Definition at line 85 of file particle.h.

86  { return CAST_S32(mPos.y) - 16; }

References CAST_S32, Actor::mPos, and Vector::y.

◆ isAlive()

bool Particle::isAlive ( ) const
inline

Definition at line 205 of file particle.h.

206  { return mAlive == AliveStatus::ALIVE; }

References AliveStatus::ALIVE, and mAlive.

Referenced by isExtinct().

◆ isExtinct()

bool Particle::isExtinct ( ) const
inline

Determines whether the particle and its children are all dead

Definition at line 213 of file particle.h.

214  { return !isAlive() && mChildParticles.empty(); }

References isAlive(), and mChildParticles.

Referenced by ParticleList::moveTo(), and ParticleVector::moveTo().

◆ kill()

void Particle::kill ( )
inline

◆ moveBy()

void Particle::moveBy ( const Vector change)

Changes the particle position relative

Definition at line 396 of file particle.cpp.

397 {
398  mPos += change;
400  {
401  (*p)->moveBy(change);
402  }
403 }

References FOR_EACH, and anonymous_namespace{mrand.cpp}::mPos.

Referenced by moveTo(), and updateSelf().

◆ moveTo() [1/2]

void Particle::moveTo ( const float  x,
const float  y 
)

Sets the position in 2 dimensional space in pixels relative to map.

Definition at line 405 of file particle.cpp.

406 {
407  moveTo(Vector(x, y, mPos.z));
408 }

References anonymous_namespace{mrand.cpp}::mPos, x, and y.

◆ moveTo() [2/2]

void Particle::moveTo ( const Vector pos)
inline

Sets the position in 3 dimensional space in pixels relative to map.

Definition at line 106 of file particle.h.

107  { moveBy(pos - mPos); }

References moveBy(), and Actor::mPos.

Referenced by addEffect(), ParticleEngine::addEffect(), ParticleEngine::addTextRiseFadeOutEffect(), ParticleEngine::addTextSplashEffect(), ParticleEmitter::createParticles(), ParticleList::moveTo(), and ParticleVector::moveTo().

◆ prepareToDie()

void Particle::prepareToDie ( )

Definition at line 564 of file particle.cpp.

565 {
567  {
568  Particle *restrict const particle = *p;
569  if (particle == nullptr)
570  continue;
571  particle->prepareToDie();
572  if (particle->isAlive() &&
573  particle->mLifetimeLeft == -1 &&
574  particle->mAutoDelete)
575  {
576  particle->kill();
577  }
578  }
579 }

References FOR_EACH, mChildParticles, and restrict.

Referenced by ParticleList::removeLocally().

◆ setActor()

void Particle::setActor ( const BeingId  actor)
inline

Definition at line 243 of file particle.h.

244  { mActor = actor; }

References mActor.

Referenced by ActorSprite::controlAutoParticle().

◆ setAllowSizeAdjust()

void Particle::setAllowSizeAdjust ( const bool  adjust)
inline

Definition at line 202 of file particle.h.

203  { mAllowSizeAdjust = adjust; }

References mAllowSizeAdjust.

Referenced by addEffect(), and ParticleEngine::addEffect().

◆ setAlpha()

void Particle::setAlpha ( const float  alpha)
inlinevirtual

Sets the alpha value used to draw the actor.

Implements Actor.

Definition at line 236 of file particle.h.

237  { }

Referenced by ParticleEmitter::createParticles().

◆ setBounce()

void Particle::setBounce ( const float  bouncieness)
inline

Sets the ammount of velocity particles retain after hitting the ground.

Definition at line 163 of file particle.h.

164  { mBounce = bouncieness; }

References mBounce.

Referenced by ParticleEngine::addTextSplashEffect(), and ParticleEmitter::createParticles().

◆ setDeathEffect()

virtual void Particle::setDeathEffect ( const std::string &  effectFile,
const signed char  conditions 
)
inlinevirtual

Definition at line 239 of file particle.h.

241  { mDeathEffect = effectFile; mDeathEffectConditions = conditions; }

References mDeathEffect, and mDeathEffectConditions.

Referenced by addEffect(), ParticleEngine::addEffect(), and ParticleEmitter::createParticles().

◆ setDestination()

void Particle::setDestination ( Particle *const  target,
const float  accel,
const float  moment 
)
inline

Makes the particle move toward another particle with a given acceleration and momentum

Definition at line 182 of file particle.h.

186  { mTarget = target; mAcceleration = accel; mMomentum = moment; }

References mAcceleration, mMomentum, and mTarget.

Referenced by ParticleEmitter::createParticles().

◆ setDieDistance()

void Particle::setDieDistance ( const float  dist)
inline

Sets the distance in pixel the particle can come near the target particle before it is destroyed. Does only make sense after a target particle has been set using setDestination.

Definition at line 193 of file particle.h.

194  { mInvDieDistance = 1.0F / dist; }

References mInvDieDistance.

Referenced by ParticleEmitter::createParticles().

◆ setFadeIn()

void Particle::setFadeIn ( const int  fadeIn)
inline

Sets the remaining particle lifetime where the particle starts to fade out.

Definition at line 136 of file particle.h.

137  { mFadeIn = fadeIn; }

References mFadeIn.

Referenced by ParticleEngine::addTextRiseFadeOutEffect(), and ParticleEmitter::createParticles().

◆ setFadeOut()

void Particle::setFadeOut ( const int  fadeOut)
inline

Sets the age of the pixel in game ticks where the particle has faded in completely.

Definition at line 129 of file particle.h.

130  { mFadeOut = fadeOut; }

References mFadeOut.

Referenced by ParticleEngine::addTextRiseFadeOutEffect(), ParticleEngine::addTextSplashEffect(), and ParticleEmitter::createParticles().

◆ setFollow()

void Particle::setFollow ( const bool  follow)
inline

Sets the flag if the particle is supposed to be moved by its parent

Definition at line 169 of file particle.h.

170  { mFollow = follow; }

References Actions::follow(), and mFollow.

Referenced by ParticleEmitter::createParticles().

◆ setGravity()

void Particle::setGravity ( const float  gravity)
inline

Sets the downward acceleration.

Definition at line 150 of file particle.h.

151  { mGravity = gravity; }

References mGravity.

Referenced by ParticleEngine::addTextRiseFadeOutEffect(), ParticleEngine::addTextSplashEffect(), and ParticleEmitter::createParticles().

◆ setLifetime()

void Particle::setLifetime ( const int  lifetime)
inline

Sets the time in game ticks until the particle is destroyed.

Definition at line 122 of file particle.h.

123  { mLifetimeLeft = lifetime; mLifetimePast = 0; }

References mLifetimeLeft, and mLifetimePast.

Referenced by addEffect(), ParticleEngine::addEffect(), ParticleEngine::addTextRiseFadeOutEffect(), ParticleEngine::addTextSplashEffect(), and ParticleEmitter::createParticles().

◆ setRandomness()

void Particle::setRandomness ( const int  r)
inline

Sets the ammount of random vector changes

Definition at line 156 of file particle.h.

157  { mRandomness = r; }

References mRandomness.

Referenced by ParticleEmitter::createParticles().

◆ setVelocity()

void Particle::setVelocity ( const float  x,
const float  y,
const float  z 
)
inline

Sets the current velocity in 3 dimensional space.

Definition at line 142 of file particle.h.

145  { mVelocity.x = x; mVelocity.y = y; mVelocity.z = z; }

References mVelocity, x, Vector::x, y, Vector::y, and Vector::z.

Referenced by ParticleEngine::addTextRiseFadeOutEffect(), ParticleEngine::addTextSplashEffect(), and ParticleEmitter::createParticles().

◆ update()

bool Particle::update ( )

Updates particle position, returns false when the particle should be deleted.

Definition at line 253 of file particle.cpp.

254 {
256  {
257  if (A_UNLIKELY(mLifetimeLeft == 0))
258  {
260  if (mChildParticles.empty())
261  {
262  if (mAutoDelete)
263  return false;
264  return true;
265  }
266  }
267  else
268  {
269  if (mAnimation != nullptr)
270  {
272  {
273  // particle engine is updated every 10ms
274  mAnimation->update(10);
275  }
276  else // ParticleType::Rotational
277  {
278  // TODO: cache velocities to avoid spamming atan2()
279  const int size = mAnimation->getLength();
280  if (size == 0)
281  return false;
282 
283  float rad = static_cast<float>(atan2(mVelocity.x,
284  mVelocity.y));
285  if (rad < 0)
286  rad = PI2 + rad;
287 
288  const float range = static_cast<float>(PI / size);
289 
290  // Determines which frame the particle should play
291  if (A_UNLIKELY(rad < range || rad > PI2 - range))
292  {
293  mAnimation->setFrame(0);
294  }
295  else
296  {
297  const float range2 = 2 * range;
298  // +++ need move condition outside of for
299  for (int c = 1; c < size; c++)
300  {
301  const float cRange = static_cast<float>(c) *
302  range2;
303  if (cRange - range < rad &&
304  rad < cRange + range)
305  {
306  mAnimation->setFrame(c);
307  break;
308  }
309  }
310  }
311  }
313  }
314  const Vector oldPos = mPos;
315 
316  updateSelf();
317 
318  const Vector change = mPos - oldPos;
319  if (mChildParticles.empty())
320  {
321  if (mAlive != AliveStatus::ALIVE &&
322  mAutoDelete)
323  {
324  return false;
325  }
326  return true;
327  }
328  for (ParticleIterator p = mChildMoveParticles.begin(),
329  fp2 = mChildMoveParticles.end(); p != fp2; )
330  {
331  // move particle with its parent if desired
332  (*p)->moveBy(change);
333  ++p;
334  }
335  }
336 
337  // Update child particles
338  for (ParticleIterator p = mChildParticles.begin(),
339  fp2 = mChildParticles.end(); p != fp2; )
340  {
341  Particle *restrict const particle = *p;
342  // update particle
343  if (A_LIKELY(particle->update()))
344  {
345  ++p;
346  }
347  else
348  {
349  mChildMoveParticles.remove(*p);
350  delete particle;
351  p = mChildParticles.erase(p);
352  }
353  }
355  mChildParticles.empty() &&
356  mAutoDelete))
357  {
358  return false;
359  }
360  }
361  else
362  {
363  if (mChildParticles.empty())
364  {
365  if (mAutoDelete)
366  return false;
367  return true;
368  }
369  // Update child particles
370  for (ParticleIterator p = mChildParticles.begin(),
371  fp2 = mChildParticles.end(); p != fp2; )
372  {
373  Particle *restrict const particle = *p;
374  // update particle
375  if (A_LIKELY(particle->update()))
376  {
377  ++p;
378  }
379  else
380  {
381  mChildMoveParticles.remove(*p);
382  delete particle;
383  p = mChildParticles.erase(p);
384  }
385  }
386  if (A_UNLIKELY(mChildParticles.empty() &&
387  mAutoDelete))
388  {
389  return false;
390  }
391  }
392 
393  return true;
394 }

References A_LIKELY, A_UNLIKELY, AliveStatus::ALIVE, ParticleType::Animation, AliveStatus::DEAD_TIMEOUT, SimpleAnimation::getCurrentImage(), SimpleAnimation::getLength(), mAlive, mAnimation, mAutoDelete, mChildMoveParticles, mChildParticles, mImage, mLifetimeLeft, Actor::mPos, mType, mVelocity, PI, PI2, restrict, SimpleAnimation::setFrame(), EmoteDB::size(), SimpleAnimation::update(), updateSelf(), Vector::x, and Vector::y.

◆ updateSelf()

void Particle::updateSelf ( )
protected

Definition at line 129 of file particle.cpp.

130 {
131  // calculate particle movement
132  if (A_LIKELY(mMomentum != 1.0F))
133  mVelocity *= mMomentum;
134 
135  if ((mTarget != nullptr) && mAcceleration != 0.0F)
136  {
137  Vector dist = mPos - mTarget->mPos;
138  dist.x *= SIN45;
139  float invHypotenuse;
140 
142  {
144  invHypotenuse = fastInvSqrt(
145  dist.x * dist.x + dist.y * dist.y + dist.z * dist.z);
146  break;
148  if (dist.x == 0.0F)
149  {
150  invHypotenuse = 0;
151  break;
152  }
153 
154  invHypotenuse = 2.0F / (static_cast<float>(fabs(dist.x))
155  + static_cast<float>(fabs(dist.y))
156  + static_cast<float>(fabs(dist.z)));
157  break;
159  default:
160  invHypotenuse = 1.0F / static_cast<float>(sqrt(
161  dist.x * dist.x + dist.y * dist.y + dist.z * dist.z));
162  break;
163  }
164 
165  if (invHypotenuse != 0.0F)
166  {
167  if (mInvDieDistance > 0.0F && invHypotenuse > mInvDieDistance)
169  const float accFactor = invHypotenuse * mAcceleration;
170  mVelocity -= dist * accFactor;
171  }
172  }
173 
174  if (A_LIKELY(mRandomness >= 10)) // reduce useless calculations
175  {
176  const int rand2 = mRandomness * 2;
177  mVelocity.x += static_cast<float>(mrand() % rand2 - mRandomness)
178  / 1000.0F;
179  mVelocity.y += static_cast<float>(mrand() % rand2 - mRandomness)
180  / 1000.0F;
181  mVelocity.z += static_cast<float>(mrand() % rand2 - mRandomness)
182  / 1000.0F;
183  }
184 
185  mVelocity.z -= mGravity;
186 
187  // Update position
188  mPos.x += mVelocity.x;
189  mPos.y += mVelocity.y * SIN45;
190  mPos.z += mVelocity.z * SIN45;
191 
192  // Update other stuff
193  if (A_LIKELY(mLifetimeLeft > 0))
194  mLifetimeLeft--;
195 
196  mLifetimePast++;
197 
198  if (mPos.z < 0.0F)
199  {
200  if (mBounce > 0.0F)
201  {
202  mPos.z *= -mBounce;
203  mVelocity *= mBounce;
204  mVelocity.z = -mVelocity.z;
205  }
206  else
207  {
209  }
210  }
212  {
214  }
215 
216  // Update child emitters
217  if ((ParticleEngine::emitterSkip != 0) &&
219  {
221  {
222  STD_VECTOR<Particle*> newParticles;
223  (*e)->createParticles(mLifetimePast, newParticles);
224  FOR_EACH (STD_VECTOR<Particle*>::const_iterator,
225  it,
226  newParticles)
227  {
228  Particle *const p = *it;
229  p->moveBy(mPos);
230  mChildParticles.push_back(p);
231  if (p->mFollow)
232  mChildMoveParticles.push_back(p);
233  }
234  }
235  }
236 
237  // create death effect when the particle died
240  {
242  > 0x00 && !mDeathEffect.empty())
243  {
244  Particle *restrict const deathEffect = particleEngine->addEffect(
245  mDeathEffect, 0, 0, 0);
246  if (deathEffect != nullptr)
247  deathEffect->moveBy(mPos);
248  }
250  }
251 }

References A_LIKELY, A_UNLIKELY, ParticleEngine::addEffect(), AliveStatus::ALIVE, ParticlePhysics::Best, CAST_U32, AliveStatus::DEAD_FLOOR, AliveStatus::DEAD_IMPACT, AliveStatus::DEAD_LONG_AGO, AliveStatus::DEAD_SKY, ParticleEngine::emitterSkip, ParticlePhysics::Fast, fastInvSqrt(), ParticleEngine::fastPhysics, FOR_EACH, mAcceleration, mAlive, mBounce, mChildEmitters, mChildMoveParticles, mChildParticles, mDeathEffect, mDeathEffectConditions, mFollow, mGravity, mInvDieDistance, mLifetimeLeft, mLifetimePast, mMomentum, moveBy(), Actor::mPos, mrand(), mRandomness, mTarget, mVelocity, ParticlePhysics::Normal, ParticleEngine::PARTICLE_SKY, particleEngine, restrict, SIN45, Vector::x, Vector::y, and Vector::z.

Referenced by update().

Friends And Related Function Documentation

◆ ParticleEngine

friend class ParticleEngine
friend

Definition at line 47 of file particle.h.

Field Documentation

◆ mAcceleration

float Particle::mAcceleration
private

Definition at line 300 of file particle.h.

Referenced by setDestination(), and updateSelf().

◆ mActor

BeingId Particle::mActor
protected

Definition at line 278 of file particle.h.

Referenced by setActor(), and ~Particle().

◆ mAlive

AliveStatusT Particle::mAlive
protected

Definition at line 268 of file particle.h.

Referenced by isAlive(), kill(), update(), and updateSelf().

◆ mAllowSizeAdjust

bool Particle::mAllowSizeAdjust
private

Definition at line 323 of file particle.h.

Referenced by setAllowSizeAdjust().

◆ mAlpha

float Particle::mAlpha
protected

Definition at line 250 of file particle.h.

Referenced by ImageParticle::setAlpha().

◆ mAnimation

SimpleAnimation* Particle::mAnimation
protected

The image used for this particle.

Definition at line 273 of file particle.h.

Referenced by AnimationParticle::AnimationParticle(), RotationalParticle::RotationalParticle(), update(), and ~Particle().

◆ mAutoDelete

bool Particle::mAutoDelete
private

Definition at line 320 of file particle.h.

Referenced by disableAutoDelete(), kill(), and update().

◆ mBounce

float Particle::mBounce
private

Definition at line 297 of file particle.h.

Referenced by setBounce(), and updateSelf().

◆ mChildEmitters

Emitters Particle::mChildEmitters
private

Definition at line 282 of file particle.h.

Referenced by addEmitter(), clear(), and updateSelf().

◆ mChildMoveParticles

Particles Particle::mChildMoveParticles
private

Definition at line 287 of file particle.h.

Referenced by clear(), update(), and updateSelf().

◆ mChildParticles

Particles Particle::mChildParticles
private

Definition at line 285 of file particle.h.

Referenced by clear(), isExtinct(), prepareToDie(), update(), and updateSelf().

◆ mDeathEffect

std::string Particle::mDeathEffect
private

Definition at line 290 of file particle.h.

Referenced by setDeathEffect(), and updateSelf().

◆ mDeathEffectConditions

signed char Particle::mDeathEffectConditions
private

Definition at line 317 of file particle.h.

Referenced by setDeathEffect(), and updateSelf().

◆ mFadeIn

int Particle::mFadeIn
protected

Definition at line 262 of file particle.h.

Referenced by setFadeIn().

◆ mFadeOut

int Particle::mFadeOut
protected

Definition at line 259 of file particle.h.

Referenced by setFadeOut().

◆ mFollow

bool Particle::mFollow
private

Definition at line 326 of file particle.h.

Referenced by doesFollow(), setFollow(), and updateSelf().

◆ mGravity

float Particle::mGravity
private

Definition at line 294 of file particle.h.

Referenced by setGravity(), and updateSelf().

◆ mImage

Image* Particle::mImage
protected

Definition at line 276 of file particle.h.

Referenced by ImageParticle::ImageParticle(), update(), and ~Particle().

◆ mInvDieDistance

float Particle::mInvDieDistance
private

Definition at line 304 of file particle.h.

Referenced by setDieDistance(), and updateSelf().

◆ mLifetimeLeft

int Particle::mLifetimeLeft
protected

Definition at line 253 of file particle.h.

Referenced by setLifetime(), update(), and updateSelf().

◆ mLifetimePast

int Particle::mLifetimePast
protected

Definition at line 256 of file particle.h.

Referenced by setLifetime(), and updateSelf().

◆ mMomentum

float Particle::mMomentum
private

Definition at line 307 of file particle.h.

Referenced by setDestination(), and updateSelf().

◆ mRandomness

int Particle::mRandomness
private

Definition at line 313 of file particle.h.

Referenced by setRandomness(), and updateSelf().

◆ mTarget

Particle* Particle::mTarget
private

Definition at line 310 of file particle.h.

Referenced by setDestination(), and updateSelf().

◆ mType

ParticleTypeT Particle::mType
protected

◆ mVelocity

Vector Particle::mVelocity
protected

Definition at line 265 of file particle.h.

Referenced by setVelocity(), update(), and updateSelf().


The documentation for this class was generated from the following files:
SimpleAnimation::getLength
int getLength() const
Definition: simpleanimation.cpp:132
Particle::mActor
BeingId mActor
Definition: particle.h:278
ParticlePhysics::Best
@ Best
Definition: particlephysics.h:28
EmitterConstIterator
Emitters::const_iterator EmitterConstIterator
Definition: particleengine.h:44
AliveStatus::DEAD_SKY
@ DEAD_SKY
Definition: alivestatus.h:33
Particle::setLifetime
void setLifetime(const int lifetime)
Definition: particle.h:122
XML::getFloatProperty
float getFloatProperty(const xmlNodePtr node, const char *const name, float def)
Definition: libxml.cpp:210
Vector::x
float x
Definition: vector.h:208
Particle
Definition: particle.h:44
ParticleType::Animation
@ Animation
Definition: particletype.h:29
Actor::mMap
Map * mMap
Definition: actor.h:138
Actor::mPos
Vector mPos
Definition: actor.h:139
Particle::mAllowSizeAdjust
bool mAllowSizeAdjust
Definition: particle.h:323
ParticleEngine::addEffect
Particle * addEffect(const std::string &particleEffectFile, const int pixelX, const int pixelY, const int rotation)
Definition: particleengine.cpp:129
Particle::mInvDieDistance
float mInvDieDistance
Definition: particle.h:304
particleEngine
ParticleEngine * particleEngine
Definition: particleengine.cpp:41
AnimationParticle
Definition: animationparticle.h:32
ParticleType::Normal
@ Normal
Definition: particletype.h:28
Particle::mType
ParticleTypeT mType
Definition: particle.h:270
actorManager
ActorManager * actorManager
Definition: actormanager.cpp:80
Particle::mChildMoveParticles
Particles mChildMoveParticles
Definition: particle.h:287
Particle::addEmitter
void addEmitter(ParticleEmitter *const emitter)
Definition: particle.h:100
SkipError_false
const bool SkipError_false
Definition: skiperror.h:29
Particle::mChildParticles
Particles mChildParticles
Definition: particle.h:285
Particle::setDeathEffect
virtual void setDeathEffect(const std::string &effectFile, const signed char conditions)
Definition: particle.h:239
ParticleEngine::fastPhysics
static ParticlePhysicsT fastPhysics
Definition: particleengine.h:50
Image
Definition: image.h:67
Particle::mFadeIn
int mFadeIn
Definition: particle.h:262
Particle::mMomentum
float mMomentum
Definition: particle.h:307
CAST_U32
#define CAST_U32
Definition: cast.h:30
ParticleEngine::particleCount
static int particleCount
Definition: particleengine.h:51
SIN45
static const float SIN45
Definition: particle.cpp:52
XML::findFirstChildByName
xmlNodePtr findFirstChildByName(const xmlNode *const parent, const char *const name)
Definition: libxml.cpp:288
ActorManager::findActor
ActorSprite * findActor(const BeingId id) const
Definition: actormanager.cpp:438
XML::Document
Definition: libxml.h:52
Particle::mTarget
Particle * mTarget
Definition: particle.h:310
Particle::mBounce
float mBounce
Definition: particle.h:297
Particle::clear
void clear()
Definition: particle.cpp:581
delete2
#define delete2(var)
Definition: delete2.h:24
AliveStatus::DEAD_IMPACT
@ DEAD_IMPACT
Definition: alivestatus.h:34
ParticleConstIterator
Particles::const_iterator ParticleConstIterator
Definition: particleengine.h:41
ParticleIterator
Particles::iterator ParticleIterator
Definition: particleengine.h:40
XML::getProperty
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:173
AliveStatus::DEAD_OTHER
@ DEAD_OTHER
Definition: alivestatus.h:35
Vector::z
float z
Definition: vector.h:208
Particle::moveBy
void moveBy(const Vector &change)
Definition: particle.cpp:396
Actor::Actor
Actor()
Definition: actor.cpp:28
logger
Logger * logger
Definition: logger.cpp:88
Particle::mFollow
bool mFollow
Definition: particle.h:326
A_LIKELY
#define A_LIKELY(x)
Definition: likely.h:28
XML::getBoolProperty
bool getBoolProperty(const xmlNodePtr node, const char *const name, const bool def)
Definition: libxml.cpp:268
Particle::mRandomness
int mRandomness
Definition: particle.h:313
SimpleAnimation::getCurrentImage
Image * getCurrentImage() const
Definition: simpleanimation.cpp:140
ParticlePhysics::Fast
@ Fast
Definition: particlephysics.h:30
PI2
static const float PI2
Definition: particle.cpp:54
Particle::mDeathEffect
std::string mDeathEffect
Definition: particle.h:290
CAST_S8
#define CAST_S8
Definition: cast.h:25
ImageParticle::imageParticleCountByName
static StringIntMap imageParticleCountByName
Definition: imageparticle.h:58
Loader::getImage
Image * getImage(const std::string &idPath)
Definition: imageloader.cpp:85
A_UNLIKELY
#define A_UNLIKELY(x)
Definition: likely.h:29
ParticleEmitter
Definition: particleemitter.h:43
ParticleEngine::emitterSkip
static int emitterSkip
Definition: particleengine.h:53
ActorSprite
Definition: actorsprite.h:54
SimpleAnimation::update
bool update(const int timePassed)
Definition: simpleanimation.cpp:110
PI
static const double PI
Definition: particle.cpp:53
Particle::mAlive
AliveStatusT mAlive
Definition: particle.h:268
ParticleType::Image
@ Image
Definition: particletype.h:30
x
x
Definition: graphics_calcImageRect.hpp:72
UseVirtFs_true
const bool UseVirtFs_true
Definition: usevirtfs.h:29
Particle::mLifetimeLeft
int mLifetimeLeft
Definition: particle.h:253
mrand
int mrand()
Definition: mrand.cpp:40
Particle::mAutoDelete
bool mAutoDelete
Definition: particle.h:320
AliveStatus::ALIVE
@ ALIVE
Definition: alivestatus.h:30
Particle::mFadeOut
int mFadeOut
Definition: particle.h:259
StringIntMapIter
StringIntMap::iterator StringIntMapIter
Definition: stringmap.h:28
Particle::mGravity
float mGravity
Definition: particle.h:294
Particle::updateSelf
void updateSelf()
Definition: particle.cpp:129
Particle::mAlpha
float mAlpha
Definition: particle.h:250
ParticlePhysics::Normal
@ Normal
Definition: particlephysics.h:29
Loader::getXml
XML::Document * getXml(const std::string &idPath, const UseVirtFs useResman, const SkipError skipError)
Definition: xmlloader.cpp:55
delete_all
void delete_all(Container &c)
Definition: dtor.h:55
EmoteDB::size
int size()
Definition: emotedb.cpp:305
SimpleAnimation::setFrame
void setFrame(int frame)
Definition: simpleanimation.cpp:96
Actor::setMap
virtual void setMap(Map *const map)
Definition: actor.cpp:47
Actions::follow
bool follow(InputEvent &event)
Definition: commands.cpp:49
Particle::setAllowSizeAdjust
void setAllowSizeAdjust(const bool adjust)
Definition: particle.h:202
ParticleEngine::PARTICLE_SKY
static const float PARTICLE_SKY
Definition: particleengine.h:49
Resource::decRef
virtual void decRef()
Definition: resource.cpp:49
restrict
#define restrict
Definition: localconsts.h:164
Vector::y
float y
Definition: vector.h:208
Dye::instantiate
static void instantiate(std::string &target, const std::string &palettes)
Definition: dye.cpp:96
Particle::mLifetimePast
int mLifetimePast
Definition: particle.h:256
Particle::mImage
Image * mImage
Definition: particle.h:276
XML::Document::rootNode
xmlNodePtr rootNode()
Definition: libxml.cpp:168
CAST_S32
#define CAST_S32
Definition: cast.h:29
Particle::mChildEmitters
Emitters mChildEmitters
Definition: particle.h:282
Particle::mAnimation
SimpleAnimation * mAnimation
Definition: particle.h:273
Particle::mVelocity
Vector mVelocity
Definition: particle.h:265
BeingId_zero
const BeingId BeingId_zero
Definition: beingid.h:29
for_each_xml_child_node
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:160
RotationalParticle
Definition: rotationalparticle.h:32
Vector
Definition: vector.h:38
Particle::mDeathEffectConditions
signed char mDeathEffectConditions
Definition: particle.h:317
AliveStatus::DEAD_TIMEOUT
@ DEAD_TIMEOUT
Definition: alivestatus.h:31
fastInvSqrt
float fastInvSqrt(float x)
Definition: mathutils.h:209
y
y
Definition: graphics_calcImageRect.hpp:72
ImageParticle
Definition: imageparticle.h:35
Particle::moveTo
void moveTo(const Vector &pos)
Definition: particle.h:106
Particle::isAlive
bool isAlive() const
Definition: particle.h:205
Particle::mAcceleration
float mAcceleration
Definition: particle.h:300
Particle::Particle
Particle()
Definition: particle.cpp:58
Logger::log
void log(const char *const log_text,...)
Definition: logger.cpp:268
FOR_EACH
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
Resource::mIdPath
std::string mIdPath
Definition: resource.h:83
AliveStatus::DEAD_LONG_AGO
@ DEAD_LONG_AGO
Definition: alivestatus.h:36
Image::decRef
void decRef()
Definition: image.cpp:522
AliveStatus::DEAD_FLOOR
@ DEAD_FLOOR
Definition: alivestatus.h:32
ActorSprite::controlParticleDeleted
void controlParticleDeleted(const Particle *const particle)
Definition: actorsprite.cpp:166