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 ()
 
virtual ~Particle ()
 
void clear ()
 
bool update ()
 
void draw (Graphics *const graphics, const int offsetX, const int offsetY) const A_CONST
 
int getPixelY () const
 
int getSortPixelY () const
 
ParticleaddEffect (const std::string &particleEffectFile, const int pixelX, const int pixelY, const int rotation=0)
 
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.

References ParticleEngine::particleCount.

Referenced by addEffect().

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 }
Vector mVelocity
Definition: particle.h:265
int mRandomness
Definition: particle.h:313
float mGravity
Definition: particle.h:294
std::string mDeathEffect
Definition: particle.h:290
float mInvDieDistance
Definition: particle.h:304
int mLifetimeLeft
Definition: particle.h:253
ParticleTypeT mType
Definition: particle.h:270
static int particleCount
BeingId mActor
Definition: particle.h:278
Actor()
Definition: actor.cpp:28
bool mAutoDelete
Definition: particle.h:320
float mAlpha
Definition: particle.h:250
float mMomentum
Definition: particle.h:307
float mAcceleration
Definition: particle.h:300
Image * mImage
Definition: particle.h:276
SimpleAnimation * mAnimation
Definition: particle.h:273
bool mAllowSizeAdjust
Definition: particle.h:323
float mBounce
Definition: particle.h:297
int mLifetimePast
Definition: particle.h:256
Particles mChildMoveParticles
Definition: particle.h:287
Emitters mChildEmitters
Definition: particle.h:282
int mFadeOut
Definition: particle.h:259
const BeingId BeingId_zero
Definition: beingid.h:29
Particles mChildParticles
Definition: particle.h:285
Particle * mTarget
Definition: particle.h:310
int mFadeIn
Definition: particle.h:262
signed char mDeathEffectConditions
Definition: particle.h:317
bool mFollow
Definition: particle.h:326
AliveStatusT mAlive
Definition: particle.h:268

◆ ~Particle()

Particle::~Particle ( )
virtual

Destructor.

Definition at line 90 of file particle.cpp.

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.

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 }
void controlParticleDeleted(const Particle *const particle)
void clear()
Definition: particle.cpp:581
StringIntMap::iterator StringIntMapIter
Definition: stringmap.h:28
#define delete2(var)
Definition: delete2.h:24
ParticleTypeT mType
Definition: particle.h:270
static int particleCount
BeingId mActor
Definition: particle.h:278
Image * mImage
Definition: particle.h:276
SimpleAnimation * mAnimation
Definition: particle.h:273
static StringIntMap imageParticleCountByName
Definition: imageparticle.h:58
ActorSprite * findActor(const BeingId id) const
#define restrict
Definition: localconsts.h:176
const BeingId BeingId_zero
Definition: beingid.h:29
std::string mIdPath
Definition: resource.h:83
void decRef()
Definition: image.cpp:521
ActorManager * actorManager

Member Function Documentation

◆ addEffect()

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

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

Definition at line 410 of file particle.cpp.

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, mChildParticles, Actor::mMap, moveTo(), Actor::mPos, Particle(), restrict, XML::Document::rootNode(), setAllowSizeAdjust(), setDeathEffect(), setLifetime(), Actor::setMap(), SkipError_false, UseVirtFs_true, Vector::x, Vector::y, and Vector::z.

Referenced by getSortPixelY().

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 }
static void instantiate(std::string &target, const std::string &palettes)
Definition: dye.cpp:96
const bool SkipError_false
Definition: skiperror.h:29
float getFloatProperty(const xmlNodePtr node, const char *const name, float def)
Definition: libxml.cpp:208
virtual void setDeathEffect(const std::string &effectFile, const signed char conditions)
Definition: particle.h:239
Particle()
Definition: particle.cpp:58
virtual void decRef()
Definition: resource.cpp:49
Definition: vector.h:38
void setAllowSizeAdjust(const bool adjust)
Definition: particle.h:202
Vector mPos
Definition: actor.h:139
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:171
Logger * logger
Definition: logger.cpp:95
const bool UseVirtFs_true
Definition: usevirtfs.h:29
void moveTo(const Vector &pos)
Definition: particle.h:106
xmlNodePtr findFirstChildByName(const xmlNode *const parent, const char *const name)
Definition: libxml.cpp:286
Image * getImage(const std::string &idPath)
Definition: imageloader.cpp:85
bool getBoolProperty(const xmlNodePtr node, const char *const name, const bool def)
Definition: libxml.cpp:266
#define CAST_S8
Definition: cast.h:25
virtual void setMap(Map *const map)
Definition: actor.cpp:47
XML::Document * getXml(const std::string &idPath, const UseVirtFs useResman, const SkipError skipError)
Definition: xmlloader.cpp:55
xmlNodePtr rootNode()
Definition: libxml.cpp:166
float x
Definition: vector.h:208
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:160
void addEmitter(ParticleEmitter *const emitter)
Definition: particle.h:100
float y
Definition: vector.h:208
Definition: image.h:61
#define restrict
Definition: localconsts.h:176
void setLifetime(const int lifetime)
Definition: particle.h:122
void log(const char *const log_text,...)
Definition: logger.cpp:243
Particles mChildParticles
Definition: particle.h:285
float z
Definition: vector.h:208
Map * mMap
Definition: actor.h:138

◆ addEmitter()

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

Adds an emitter to the particle.

Definition at line 100 of file particle.h.

References mChildEmitters.

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

101  { mChildEmitters.push_back(emitter); }
Emitters mChildEmitters
Definition: particle.h:282

◆ 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.

References FOR_EACH, mAllowSizeAdjust, and mChildEmitters.

Referenced by Map::initializeParticleEffects(), and setDieDistance().

556 {
557  if (mAllowSizeAdjust)
558  {
560  (*e)->adjustSize(w, h);
561  }
562 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
bool mAllowSizeAdjust
Definition: particle.h:323
Emitters::const_iterator EmitterConstIterator
Emitters mChildEmitters
Definition: particle.h:282

◆ clear()

void Particle::clear ( )

Deletes all child particles and emitters.

Definition at line 581 of file particle.cpp.

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

Referenced by ~Particle().

582 {
584  mChildEmitters.clear();
585 
587  mChildParticles.clear();
588 
589  mChildMoveParticles.clear();
590 }
void delete_all(Container &c)
Definition: dtor.h:55
Particles mChildMoveParticles
Definition: particle.h:287
Emitters mChildEmitters
Definition: particle.h:282
Particles mChildParticles
Definition: particle.h:285

◆ 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.

References mAutoDelete.

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

227  { mAutoDelete = false; }
bool mAutoDelete
Definition: particle.h:320

◆ 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.

References mFollow.

176  { return mFollow; }
bool mFollow
Definition: particle.h:326

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

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

81  { return CAST_S32(mPos.y) - 16; }
Vector mPos
Definition: actor.h:139
#define CAST_S32
Definition: cast.h:29
float y
Definition: vector.h:208

◆ 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.

References addEffect(), CAST_S32, Actor::mPos, restrict, restrict2, and Vector::y.

87  { return CAST_S32(mPos.y) - 16; }
Vector mPos
Definition: actor.h:139
#define CAST_S32
Definition: cast.h:29
float y
Definition: vector.h:208

◆ isAlive()

bool Particle::isAlive ( ) const
inline

Definition at line 205 of file particle.h.

References AliveStatus::ALIVE, mAlive, prepareToDie(), and restrict2.

Referenced by TextParticle::draw(), and isExtinct().

206  { return mAlive == AliveStatus::ALIVE; }
AliveStatusT mAlive
Definition: particle.h:268

◆ isExtinct()

bool Particle::isExtinct ( ) const
inline

Determines whether the particle and its children are all dead

Definition at line 213 of file particle.h.

References isAlive(), and mChildParticles.

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

214  { return !isAlive() && mChildParticles.empty(); }
bool isAlive() const
Definition: particle.h:205
Particles mChildParticles
Definition: particle.h:285

◆ 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.

References FOR_EACH, mChildMoveParticles, and Actor::mPos.

Referenced by moveTo(), and updateSelf().

397 {
398  mPos += change;
400  {
401  (*p)->moveBy(change);
402  }
403 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
Vector mPos
Definition: actor.h:139
Particles mChildMoveParticles
Definition: particle.h:287
Particles::const_iterator ParticleConstIterator

◆ moveTo() [1/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.

References moveBy(), Actor::mPos, restrict, restrict2, x, and y.

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

107  { moveBy(pos - mPos); }
Vector mPos
Definition: actor.h:139
void moveBy(const Vector &change)
Definition: particle.cpp:396

◆ moveTo() [2/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.

References moveTo(), Actor::mPos, x, y, and Vector::z.

406 {
407  moveTo(Vector(x, y, mPos.z));
408 }
Definition: vector.h:38
Vector mPos
Definition: actor.h:139
void moveTo(const Vector &pos)
Definition: particle.h:106
float z
Definition: vector.h:208

◆ prepareToDie()

void Particle::prepareToDie ( )

Definition at line 564 of file particle.cpp.

References FOR_EACH, mChildParticles, and restrict.

Referenced by isAlive(), and ParticleList::removeLocally().

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 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
Particles::iterator ParticleIterator
#define restrict
Definition: localconsts.h:176
Particles mChildParticles
Definition: particle.h:285

◆ setActor()

void Particle::setActor ( const BeingId  actor)
inline

Definition at line 243 of file particle.h.

References mActor, restrict2, and updateSelf().

Referenced by ActorSprite::controlAutoParticle().

244  { mActor = actor; }
BeingId mActor
Definition: particle.h:278

◆ setAllowSizeAdjust()

void Particle::setAllowSizeAdjust ( const bool  adjust)
inline

Definition at line 202 of file particle.h.

References mAllowSizeAdjust.

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

203  { mAllowSizeAdjust = adjust; }
bool mAllowSizeAdjust
Definition: particle.h:323

◆ 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.

Referenced by ParticleEmitter::createParticles().

237  { }

◆ 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.

References mBounce.

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

164  { mBounce = bouncieness; }
float mBounce
Definition: particle.h:297

◆ setDeathEffect()

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

Definition at line 239 of file particle.h.

References mDeathEffect, and mDeathEffectConditions.

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

241  { mDeathEffect = effectFile; mDeathEffectConditions = conditions; }
std::string mDeathEffect
Definition: particle.h:290
signed char mDeathEffectConditions
Definition: particle.h:317

◆ 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.

References mAcceleration, mMomentum, and mTarget.

Referenced by ParticleEmitter::createParticles().

186  { mTarget = target; mAcceleration = accel; mMomentum = moment; }
float mMomentum
Definition: particle.h:307
float mAcceleration
Definition: particle.h:300
Particle * mTarget
Definition: particle.h:310

◆ 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.

References adjustEmitterSize(), mInvDieDistance, and restrict2.

Referenced by ParticleEmitter::createParticles().

194  { mInvDieDistance = 1.0F / dist; }
float mInvDieDistance
Definition: particle.h:304

◆ 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.

References mFadeIn.

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

137  { mFadeIn = fadeIn; }
int mFadeIn
Definition: particle.h:262

◆ 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.

References mFadeOut.

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

130  { mFadeOut = fadeOut; }
int mFadeOut
Definition: particle.h:259

◆ 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.

References ServerCommandType::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.

References mGravity.

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

151  { mGravity = gravity; }
float mGravity
Definition: particle.h:294

◆ 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.

References mLifetimeLeft, and mLifetimePast.

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

123  { mLifetimeLeft = lifetime; mLifetimePast = 0; }
int mLifetimeLeft
Definition: particle.h:253
int mLifetimePast
Definition: particle.h:256

◆ setRandomness()

void Particle::setRandomness ( const int  r)
inline

Sets the ammount of random vector changes

Definition at line 156 of file particle.h.

References mRandomness.

Referenced by ParticleEmitter::createParticles().

157  { mRandomness = r; }
int mRandomness
Definition: particle.h:313

◆ 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.

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

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

145  { mVelocity.x = x; mVelocity.y = y; mVelocity.z = z; }
Vector mVelocity
Definition: particle.h:265
float x
Definition: vector.h:208
float y
Definition: vector.h:208
float z
Definition: vector.h:208

◆ update()

bool Particle::update ( )

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

Definition at line 253 of file particle.cpp.

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(), ServerCommandType::size, SimpleAnimation::update(), updateSelf(), Vector::x, and Vector::y.

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 }
static const double PI
Definition: particle.cpp:53
Vector mVelocity
Definition: particle.h:265
Definition: vector.h:38
Vector mPos
Definition: actor.h:139
void updateSelf()
Definition: particle.cpp:129
#define A_UNLIKELY(x)
Definition: likely.h:29
int mLifetimeLeft
Definition: particle.h:253
ParticleTypeT mType
Definition: particle.h:270
Particles::iterator ParticleIterator
bool mAutoDelete
Definition: particle.h:320
static const float PI2
Definition: particle.cpp:54
bool update(const int timePassed)
Image * mImage
Definition: particle.h:276
SimpleAnimation * mAnimation
Definition: particle.h:273
#define A_LIKELY(x)
Definition: likely.h:28
float x
Definition: vector.h:208
int getLength() const
Image * getCurrentImage() const
float y
Definition: vector.h:208
Particles mChildMoveParticles
Definition: particle.h:287
void setFrame(int frame)
#define restrict
Definition: localconsts.h:176
Particles mChildParticles
Definition: particle.h:285
AliveStatusT mAlive
Definition: particle.h:268

◆ updateSelf()

void Particle::updateSelf ( )
protected

Definition at line 129 of file particle.cpp.

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 setActor(), and update().

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);
246  if (deathEffect != nullptr)
247  deathEffect->moveBy(mPos);
248  }
250  }
251 }
#define CAST_U32
Definition: cast.h:30
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
float fastInvSqrt(float x)
Definition: mathutils.h:209
Vector mVelocity
Definition: particle.h:265
Definition: vector.h:38
int mRandomness
Definition: particle.h:313
Vector mPos
Definition: actor.h:139
ParticleEngine * particleEngine
float mGravity
Definition: particle.h:294
std::string mDeathEffect
Definition: particle.h:290
Particle * addEffect(const std::string &particleEffectFile, const int pixelX, const int pixelY, const int rotation=0)
static int emitterSkip
float mInvDieDistance
Definition: particle.h:304
#define A_UNLIKELY(x)
Definition: likely.h:29
int mLifetimeLeft
Definition: particle.h:253
static const float PARTICLE_SKY
float mMomentum
Definition: particle.h:307
static ParticlePhysicsT fastPhysics
float mAcceleration
Definition: particle.h:300
#define A_LIKELY(x)
Definition: likely.h:28
float x
Definition: vector.h:208
float mBounce
Definition: particle.h:297
Emitters::const_iterator EmitterConstIterator
int mLifetimePast
Definition: particle.h:256
int mrand()
Definition: mrand.cpp:40
float y
Definition: vector.h:208
Particles mChildMoveParticles
Definition: particle.h:287
Emitters mChildEmitters
Definition: particle.h:282
#define restrict
Definition: localconsts.h:176
void moveBy(const Vector &change)
Definition: particle.cpp:396
static const float SIN45
Definition: particle.cpp:52
Particles mChildParticles
Definition: particle.h:285
Particle * mTarget
Definition: particle.h:310
signed char mDeathEffectConditions
Definition: particle.h:317
bool mFollow
Definition: particle.h:326
float z
Definition: vector.h:208
AliveStatusT mAlive
Definition: particle.h:268

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 ImageParticle::draw(), isAlive(), kill(), update(), and updateSelf().

◆ mAllowSizeAdjust

bool Particle::mAllowSizeAdjust
private

Definition at line 323 of file particle.h.

Referenced by adjustEmitterSize(), and setAllowSizeAdjust().

◆ mAlpha

float Particle::mAlpha
protected

Definition at line 250 of file particle.h.

Referenced by TextParticle::draw(), ImageParticle::draw(), and 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(), adjustEmitterSize(), clear(), and updateSelf().

◆ mChildMoveParticles

Particles Particle::mChildMoveParticles
private

Definition at line 287 of file particle.h.

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

◆ mChildParticles

Particles Particle::mChildParticles
private

Definition at line 285 of file particle.h.

Referenced by addEffect(), 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 TextParticle::draw(), ImageParticle::draw(), and setFadeIn().

◆ mFadeOut

int Particle::mFadeOut
protected

Definition at line 259 of file particle.h.

Referenced by TextParticle::draw(), ImageParticle::draw(), and 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::draw(), 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 TextParticle::draw(), ImageParticle::draw(), setLifetime(), update(), and updateSelf().

◆ mLifetimePast

int Particle::mLifetimePast
protected

Definition at line 256 of file particle.h.

Referenced by TextParticle::draw(), ImageParticle::draw(), 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: