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 45 of file particle.h.

Constructor & Destructor Documentation

◆ Particle()

Particle::Particle ( )

Definition at line 59 of file particle.cpp.

59  :
60  Actor(),
61  mAlpha(1.0F),
62  mLifetimeLeft(-1),
63  mLifetimePast(0),
64  mFadeOut(0),
65  mFadeIn(0),
66  mVelocity(),
69  mAnimation(nullptr),
70  mImage(nullptr),
75  mDeathEffect(),
76  mGravity(0.0F),
77  mBounce(0.0F),
78  mAcceleration(0.0F),
79  mInvDieDistance(-1.0F),
80  mMomentum(1.0F),
81  mTarget(nullptr),
82  mRandomness(0),
84  mAutoDelete(true),
85  mAllowSizeAdjust(false),
86  mFollow(false)
87 {
89 }
const BeingId BeingId_zero
Definition: beingid.h:30
Actor()
Definition: actor.cpp:29
static int particleCount
Particles mChildParticles
Definition: particle.h:286
float mBounce
Definition: particle.h:298
float mMomentum
Definition: particle.h:308
Image * mImage
Definition: particle.h:277
float mGravity
Definition: particle.h:295
SimpleAnimation * mAnimation
Definition: particle.h:274
int mRandomness
Definition: particle.h:314
int mFadeIn
Definition: particle.h:263
float mAcceleration
Definition: particle.h:301
Particle * mTarget
Definition: particle.h:311
bool mAutoDelete
Definition: particle.h:321
BeingId mActor
Definition: particle.h:279
signed char mDeathEffectConditions
Definition: particle.h:318
Particles mChildMoveParticles
Definition: particle.h:288
std::string mDeathEffect
Definition: particle.h:291
float mInvDieDistance
Definition: particle.h:305
int mLifetimePast
Definition: particle.h:257
float mAlpha
Definition: particle.h:251
int mLifetimeLeft
Definition: particle.h:254
AliveStatusT mAlive
Definition: particle.h:269
bool mAllowSizeAdjust
Definition: particle.h:324
bool mFollow
Definition: particle.h:327
ParticleTypeT mType
Definition: particle.h:271
int mFadeOut
Definition: particle.h:260
Vector mVelocity
Definition: particle.h:266
Emitters mChildEmitters
Definition: particle.h:283

References ParticleEngine::particleCount.

◆ ~Particle()

Particle::~Particle ( )

Destructor.

Definition at line 91 of file particle.cpp.

92 {
93  if (mActor != BeingId_zero &&
94  (actorManager != nullptr))
95  {
96  ActorSprite *const actor = actorManager->findActor(mActor);
97  if (actor != nullptr)
98  actor->controlParticleDeleted(this);
99  }
100  // Delete child emitters and child particles
101  clear();
103  if (mImage != nullptr)
104  {
105  if (mType == ParticleType::Image)
106  {
107  const std::string &restrict name = mImage->mIdPath;
111  {
112  int &cnt = (*it).second;
113  if (cnt > 0)
114  cnt --;
115  }
116  mImage->decRef();
117  }
118  mImage = nullptr;
119  }
120 
122 }
ActorManager * actorManager
ActorSprite * findActor(const BeingId id) const
void controlParticleDeleted(const Particle *const particle)
static StringIntMap imageParticleCountByName
Definition: imageparticle.h:59
void clear()
Definition: particle.cpp:582
#define delete2(var)
Definition: delete2.h:25
#define restrict
Definition: localconsts.h:165
StringIntMap::iterator StringIntMapIter
Definition: stringmap.h:29

References actorManager, BeingId_zero, clear(), ActorSprite::controlParticleDeleted(), delete2, ActorManager::findActor(), ParticleType::Image, ImageParticle::imageParticleCountByName, mActor, mAnimation, 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 411 of file particle.cpp.

414 {
415  Particle *newParticle = nullptr;
416 
417  const size_t pos = particleEffectFile.find('|');
418  const std::string dyePalettes = (pos != std::string::npos)
419  ? particleEffectFile.substr(pos + 1) : "";
420  XML::Document *doc = Loader::getXml(particleEffectFile.substr(0, pos),
423  if (doc == nullptr)
424  return nullptr;
425  XmlNodeConstPtrConst rootNode = doc->rootNode();
426 
427  if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "effect"))
428  {
429  logger->log("Error loading particle: %s", particleEffectFile.c_str());
430  doc->decRef();
431  return nullptr;
432  }
433 
434  // Parse particles
435  for_each_xml_child_node(effectChildNode, rootNode)
436  {
437  // We're only interested in particles
438  if (!xmlNameEqual(effectChildNode, "particle"))
439  continue;
440 
441  // Determine the exact particle type
442  XmlNodePtr node;
443 
444  // Animation
445  if ((node = XML::findFirstChildByName(effectChildNode, "animation")) !=
446  nullptr)
447  {
448  newParticle = new AnimationParticle(node, dyePalettes);
449  newParticle->setMap(mMap);
450  }
451  // Rotational
452  else if ((node = XML::findFirstChildByName(
453  effectChildNode, "rotation")) != nullptr)
454  {
455  newParticle = new RotationalParticle(node, dyePalettes);
456  newParticle->setMap(mMap);
457  }
458  // Image
459  else if ((node = XML::findFirstChildByName(effectChildNode,
460  "image")) != nullptr)
461  {
462  std::string imageSrc;
463  if (XmlHaveChildContent(node))
464  imageSrc = XmlChildContent(node);
465  if (!imageSrc.empty() && !dyePalettes.empty())
466  Dye::instantiate(imageSrc, dyePalettes);
467  Image *const img = Loader::getImage(imageSrc);
468 
469  newParticle = new ImageParticle(img);
470  newParticle->setMap(mMap);
471  }
472  // Other
473  else
474  {
475  newParticle = new Particle;
476  newParticle->setMap(mMap);
477  }
478 
479  // Read and set the basic properties of the particle
480  const float offsetX = XML::getFloatProperty(
481  effectChildNode, "position-x", 0);
482  const float offsetY = XML::getFloatProperty(
483  effectChildNode, "position-y", 0);
484  const float offsetZ = XML::getFloatProperty(
485  effectChildNode, "position-z", 0);
486  const Vector position(mPos.x + static_cast<float>(pixelX) + offsetX,
487  mPos.y + static_cast<float>(pixelY) + offsetY,
488  mPos.z + offsetZ);
489  newParticle->moveTo(position);
490 
491  const int lifetime = XML::getProperty(effectChildNode, "lifetime", -1);
492  newParticle->setLifetime(lifetime);
493  const bool resizeable = "false" != XML::getProperty(effectChildNode,
494  "size-adjustable", "false");
495 
496  newParticle->setAllowSizeAdjust(resizeable);
497 
498  // Look for additional emitters for this particle
499  for_each_xml_child_node(emitterNode, effectChildNode)
500  {
501  if (xmlNameEqual(emitterNode, "emitter"))
502  {
503  ParticleEmitter *restrict const newEmitter =
504  new ParticleEmitter(
505  emitterNode,
506  newParticle,
507  mMap,
508  rotation,
509  dyePalettes);
510  newParticle->addEmitter(newEmitter);
511  }
512  else if (xmlNameEqual(emitterNode, "deatheffect"))
513  {
514  std::string deathEffect;
515  if ((node != nullptr) && XmlHaveChildContent(node))
516  deathEffect = XmlChildContent(emitterNode);
517 
518  char deathEffectConditions = 0x00;
519  if (XML::getBoolProperty(emitterNode, "on-floor", true))
520  {
521  deathEffectConditions += CAST_S8(
523  }
524  if (XML::getBoolProperty(emitterNode, "on-sky", true))
525  {
526  deathEffectConditions += CAST_S8(
528  }
529  if (XML::getBoolProperty(emitterNode, "on-other", false))
530  {
531  deathEffectConditions += CAST_S8(
533  }
534  if (XML::getBoolProperty(emitterNode, "on-impact", true))
535  {
536  deathEffectConditions += CAST_S8(
538  }
539  if (XML::getBoolProperty(emitterNode, "on-timeout", true))
540  {
541  deathEffectConditions += CAST_S8(
543  }
544  newParticle->setDeathEffect(
545  deathEffect, deathEffectConditions);
546  }
547  }
548 
549  mChildParticles.push_back(newParticle);
550  }
551 
552  doc->decRef();
553  return newParticle;
554 }
#define CAST_S8
Definition: cast.h:26
virtual void setMap(Map *const map)
Definition: actor.cpp:48
Vector mPos
Definition: actor.h:140
Map * mMap
Definition: actor.h:139
static void instantiate(std::string &target, const std::string &palettes)
Definition: dye.cpp:97
void log(const char *const log_text,...)
Definition: logger.cpp:269
void setLifetime(const int lifetime)
Definition: particle.h:123
Particle()
Definition: particle.cpp:59
void setAllowSizeAdjust(const bool adjust)
Definition: particle.h:203
virtual void setDeathEffect(const std::string &effectFile, const signed char conditions)
Definition: particle.h:240
void addEmitter(ParticleEmitter *const emitter)
Definition: particle.h:101
void moveTo(const Vector &pos)
Definition: particle.h:107
virtual void decRef()
Definition: resource.cpp:50
Definition: vector.h:40
float z
Definition: vector.h:209
float y
Definition: vector.h:209
float x
Definition: vector.h:209
xmlNodePtr rootNode()
Definition: libxml.cpp:169
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:161
Logger * logger
Definition: logger.cpp:89
XML::Document * getXml(const std::string &idPath, const UseVirtFs useResman, const SkipError skipError)
Definition: xmlloader.cpp:56
Image * getImage(const std::string &idPath)
Definition: imageloader.cpp:86
float getFloatProperty(const xmlNodePtr node, const char *const name, float def)
Definition: libxml.cpp:211
bool getBoolProperty(const xmlNodePtr node, const char *const name, const bool def)
Definition: libxml.cpp:269
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:174
xmlNodePtr findFirstChildByName(const xmlNode *const parent, const char *const name)
Definition: libxml.cpp:289
const bool SkipError_false
Definition: skiperror.h:30
const bool UseVirtFs_true
Definition: usevirtfs.h:30

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 101 of file particle.h.

102  { 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 556 of file particle.cpp.

557 {
558  if (mAllowSizeAdjust)
559  {
561  (*e)->adjustSize(w, h);
562  }
563 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
Emitters::const_iterator EmitterConstIterator

References FOR_EACH.

Referenced by Map::initializeParticleEffects().

◆ clear()

void Particle::clear ( )

Deletes all child particles and emitters.

Definition at line 582 of file particle.cpp.

583 {
585  mChildEmitters.clear();
586 
588  mChildParticles.clear();
589 
590  mChildMoveParticles.clear();
591 }
void delete_all(Container &c)
Definition: dtor.h:56

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 227 of file particle.h.

228  { 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 176 of file particle.h.

177  { 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 124 of file particle.cpp.

127 {
128 }

◆ getAlpha()

float Particle::getAlpha ( ) const
inlinevirtual

Returns the current alpha value used to draw the actor.

Implements Actor.

Definition at line 234 of file particle.h.

235  { 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 231 of file particle.h.

232  { return 1; }

◆ getPixelY()

int Particle::getPixelY ( ) const
inlinevirtual

Necessary for sorting with the other sprites.

Reimplemented from Actor.

Reimplemented in TextParticle.

Definition at line 80 of file particle.h.

81  { return CAST_S32(mPos.y) - 16; }
#define CAST_S32
Definition: cast.h:30

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 86 of file particle.h.

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

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

◆ isAlive()

bool Particle::isAlive ( ) const
inline

Definition at line 206 of file particle.h.

207  { 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 214 of file particle.h.

215  { return !isAlive() && mChildParticles.empty(); }
bool isAlive() const
Definition: particle.h:206

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 397 of file particle.cpp.

398 {
399  mPos += change;
401  {
402  (*p)->moveBy(change);
403  }
404 }
Particles::const_iterator ParticleConstIterator

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 406 of file particle.cpp.

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

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 107 of file particle.h.

108  { moveBy(pos - mPos); }
void moveBy(const Vector &change)
Definition: particle.cpp:397

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 565 of file particle.cpp.

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

References FOR_EACH, mChildParticles, and restrict.

Referenced by ParticleList::removeLocally().

◆ setActor()

void Particle::setActor ( const BeingId  actor)
inline

Definition at line 244 of file particle.h.

245  { mActor = actor; }

References mActor.

Referenced by ActorSprite::controlAutoParticle().

◆ setAllowSizeAdjust()

void Particle::setAllowSizeAdjust ( const bool  adjust)
inline

Definition at line 203 of file particle.h.

204  { 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 237 of file particle.h.

238  { }

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 164 of file particle.h.

165  { 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 240 of file particle.h.

242  { 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 183 of file particle.h.

187  { 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 194 of file particle.h.

195  { 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 137 of file particle.h.

138  { 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 130 of file particle.h.

131  { 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 170 of file particle.h.

171  { mFollow = follow; }
bool follow(InputEvent &event)
Definition: commands.cpp:50

References Actions::follow(), and mFollow.

Referenced by ParticleEmitter::createParticles().

◆ setGravity()

void Particle::setGravity ( const float  gravity)
inline

Sets the downward acceleration.

Definition at line 151 of file particle.h.

152  { 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 123 of file particle.h.

124  { 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 157 of file particle.h.

158  { 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 143 of file particle.h.

146  { 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 254 of file particle.cpp.

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

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 130 of file particle.cpp.

131 {
132  // calculate particle movement
133  if (A_LIKELY(mMomentum != 1.0F))
134  mVelocity *= mMomentum;
135 
136  if ((mTarget != nullptr) && mAcceleration != 0.0F)
137  {
138  Vector dist = mPos - mTarget->mPos;
139  dist.x *= SIN45;
140  float invHypotenuse;
141 
143  {
145  invHypotenuse = fastInvSqrt(
146  dist.x * dist.x + dist.y * dist.y + dist.z * dist.z);
147  break;
149  if (dist.x == 0.0F)
150  {
151  invHypotenuse = 0;
152  break;
153  }
154 
155  invHypotenuse = 2.0F / (static_cast<float>(fabs(dist.x))
156  + static_cast<float>(fabs(dist.y))
157  + static_cast<float>(fabs(dist.z)));
158  break;
160  default:
161  invHypotenuse = 1.0F / static_cast<float>(sqrt(
162  dist.x * dist.x + dist.y * dist.y + dist.z * dist.z));
163  break;
164  }
165 
166  if (invHypotenuse != 0.0F)
167  {
168  if (mInvDieDistance > 0.0F && invHypotenuse > mInvDieDistance)
170  const float accFactor = invHypotenuse * mAcceleration;
171  mVelocity -= dist * accFactor;
172  }
173  }
174 
175  if (A_LIKELY(mRandomness >= 10)) // reduce useless calculations
176  {
177  const int rand2 = mRandomness * 2;
178  mVelocity.x += static_cast<float>(mrand() % rand2 - mRandomness)
179  / 1000.0F;
180  mVelocity.y += static_cast<float>(mrand() % rand2 - mRandomness)
181  / 1000.0F;
182  mVelocity.z += static_cast<float>(mrand() % rand2 - mRandomness)
183  / 1000.0F;
184  }
185 
186  mVelocity.z -= mGravity;
187 
188  // Update position
189  mPos.x += mVelocity.x;
190  mPos.y += mVelocity.y * SIN45;
191  mPos.z += mVelocity.z * SIN45;
192 
193  // Update other stuff
194  if (A_LIKELY(mLifetimeLeft > 0))
195  mLifetimeLeft--;
196 
197  mLifetimePast++;
198 
199  if (mPos.z < 0.0F)
200  {
201  if (mBounce > 0.0F)
202  {
203  mPos.z *= -mBounce;
204  mVelocity *= mBounce;
205  mVelocity.z = -mVelocity.z;
206  }
207  else
208  {
210  }
211  }
213  {
215  }
216 
217  // Update child emitters
218  if ((ParticleEngine::emitterSkip != 0) &&
220  {
222  {
223  STD_VECTOR<Particle*> newParticles;
224  (*e)->createParticles(mLifetimePast, newParticles);
225  FOR_EACH (STD_VECTOR<Particle*>::const_iterator,
226  it,
227  newParticles)
228  {
229  Particle *const p = *it;
230  p->moveBy(mPos);
231  mChildParticles.push_back(p);
232  if (p->mFollow)
233  mChildMoveParticles.push_back(p);
234  }
235  }
236  }
237 
238  // create death effect when the particle died
241  {
243  > 0x00 && !mDeathEffect.empty())
244  {
245  Particle *restrict const deathEffect = particleEngine->addEffect(
246  mDeathEffect, 0, 0, 0);
247  if (deathEffect != nullptr)
248  deathEffect->moveBy(mPos);
249  }
251  }
252 }
#define CAST_U32
Definition: cast.h:31
static const float PARTICLE_SKY
Particle * addEffect(const std::string &particleEffectFile, const int pixelX, const int pixelY, const int rotation)
static ParticlePhysicsT fastPhysics
static int emitterSkip
float fastInvSqrt(float x)
Definition: mathutils.h:210
int mrand()
Definition: mrand.cpp:41
static const float SIN45
Definition: particle.cpp:53
ParticleEngine * particleEngine

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 48 of file particle.h.

Field Documentation

◆ mAcceleration

float Particle::mAcceleration
private

Definition at line 301 of file particle.h.

Referenced by setDestination(), and updateSelf().

◆ mActor

BeingId Particle::mActor
protected

Definition at line 279 of file particle.h.

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

◆ mAlive

AliveStatusT Particle::mAlive
protected

Definition at line 269 of file particle.h.

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

◆ mAllowSizeAdjust

bool Particle::mAllowSizeAdjust
private

Definition at line 324 of file particle.h.

Referenced by setAllowSizeAdjust().

◆ mAlpha

float Particle::mAlpha
protected

Definition at line 251 of file particle.h.

Referenced by ImageParticle::setAlpha().

◆ mAnimation

SimpleAnimation* Particle::mAnimation
protected

The image used for this particle.

Definition at line 274 of file particle.h.

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

◆ mAutoDelete

bool Particle::mAutoDelete
private

Definition at line 321 of file particle.h.

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

◆ mBounce

float Particle::mBounce
private

Definition at line 298 of file particle.h.

Referenced by setBounce(), and updateSelf().

◆ mChildEmitters

Emitters Particle::mChildEmitters
private

Definition at line 283 of file particle.h.

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

◆ mChildMoveParticles

Particles Particle::mChildMoveParticles
private

Definition at line 288 of file particle.h.

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

◆ mChildParticles

Particles Particle::mChildParticles
private

Definition at line 286 of file particle.h.

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

◆ mDeathEffect

std::string Particle::mDeathEffect
private

Definition at line 291 of file particle.h.

Referenced by setDeathEffect(), and updateSelf().

◆ mDeathEffectConditions

signed char Particle::mDeathEffectConditions
private

Definition at line 318 of file particle.h.

Referenced by setDeathEffect(), and updateSelf().

◆ mFadeIn

int Particle::mFadeIn
protected

Definition at line 263 of file particle.h.

Referenced by setFadeIn().

◆ mFadeOut

int Particle::mFadeOut
protected

Definition at line 260 of file particle.h.

Referenced by setFadeOut().

◆ mFollow

bool Particle::mFollow
private

Definition at line 327 of file particle.h.

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

◆ mGravity

float Particle::mGravity
private

Definition at line 295 of file particle.h.

Referenced by setGravity(), and updateSelf().

◆ mImage

Image* Particle::mImage
protected

Definition at line 277 of file particle.h.

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

◆ mInvDieDistance

float Particle::mInvDieDistance
private

Definition at line 305 of file particle.h.

Referenced by setDieDistance(), and updateSelf().

◆ mLifetimeLeft

int Particle::mLifetimeLeft
protected

Definition at line 254 of file particle.h.

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

◆ mLifetimePast

int Particle::mLifetimePast
protected

Definition at line 257 of file particle.h.

Referenced by setLifetime(), and updateSelf().

◆ mMomentum

float Particle::mMomentum
private

Definition at line 308 of file particle.h.

Referenced by setDestination(), and updateSelf().

◆ mRandomness

int Particle::mRandomness
private

Definition at line 314 of file particle.h.

Referenced by setRandomness(), and updateSelf().

◆ mTarget

Particle* Particle::mTarget
private

Definition at line 311 of file particle.h.

Referenced by setDestination(), and updateSelf().

◆ mType

ParticleTypeT Particle::mType
protected

◆ mVelocity

Vector Particle::mVelocity
protected

Definition at line 266 of file particle.h.

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


The documentation for this class was generated from the following files: