ManaPlus
Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Attributes
ParticleEngine Class Reference

#include <particleengine.h>

Public Member Functions

 ParticleEngine ()
 
 ~ParticleEngine ()
 
void clear ()
 
bool update ()
 
ParticlecreateChild ()
 
ParticleaddEffect (const std::string &particleEffectFile, const int pixelX, const int pixelY, const int rotation)
 
ParticleaddTextSplashEffect (const std::string &text, const int x, const int y, const Color *const color, Font *const font, const bool outline)
 
ParticleaddTextRiseFadeOutEffect (const std::string &text, const int x, const int y, const Color *const color, Font *const font, const bool outline)
 
void setMap (Map *const map)
 

Static Public Member Functions

static void setupEngine ()
 

Static Public Attributes

static const float PARTICLE_SKY = 800.0F
 
static ParticlePhysicsT fastPhysics = ParticlePhysics::Best
 
static int particleCount = 0
 
static int maxCount = 0
 
static int emitterSkip = 1
 
static bool enabled = true
 

Private Attributes

Particles mChildParticles
 
Particles mChildMoveParticles
 
MapmMap
 

Detailed Description

Definition at line 46 of file particleengine.h.

Constructor & Destructor Documentation

◆ ParticleEngine()

ParticleEngine::ParticleEngine ( )

Definition at line 52 of file particleengine.cpp.

52  :
55  mMap(nullptr)
56 {
58 }

References particleCount.

◆ ~ParticleEngine()

ParticleEngine::~ParticleEngine ( )

Destructor.

Definition at line 60 of file particleengine.cpp.

61 {
62  // Delete child emitters and child particles
63  clear();
65 }

References clear(), and particleCount.

Member Function Documentation

◆ addEffect()

Particle * ParticleEngine::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 129 of file particleengine.cpp.

134 {
135  Particle *newParticle = nullptr;
136 
137  const size_t pos = particleEffectFile.find('|');
138  const std::string dyePalettes = (pos != std::string::npos)
139  ? particleEffectFile.substr(pos + 1) : "";
141  particleEffectFile.substr(0, pos),
144  if (doc == nullptr)
145  return nullptr;
146 
147  XmlNodeConstPtrConst rootNode = doc->rootNode();
148 
149  if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "effect"))
150  {
151  logger->log("Error loading particle: %s", particleEffectFile.c_str());
152  doc->decRef();
153  return nullptr;
154  }
155 
156  // Parse particles
157  for_each_xml_child_node(effectChildNode, rootNode)
158  {
159  // We're only interested in particles
160  if (!xmlNameEqual(effectChildNode, "particle"))
161  continue;
162 
163  // Determine the exact particle type
164  XmlNodePtr node;
165 
166  // Animation
167  if ((node = XML::findFirstChildByName(effectChildNode, "animation")) !=
168  nullptr)
169  {
170  newParticle = new AnimationParticle(node, dyePalettes);
171  newParticle->setMap(mMap);
172  }
173  // Rotational
174  else if ((node = XML::findFirstChildByName(
175  effectChildNode, "rotation")) != nullptr)
176  {
177  newParticle = new RotationalParticle(node, dyePalettes);
178  newParticle->setMap(mMap);
179  }
180  // Image
181  else if ((node = XML::findFirstChildByName(effectChildNode,
182  "image")) != nullptr)
183  {
184  std::string imageSrc;
185  if (XmlHaveChildContent(node))
186  imageSrc = XmlChildContent(node);
187  if (!imageSrc.empty() && !dyePalettes.empty())
188  Dye::instantiate(imageSrc, dyePalettes);
189  Image *const img = Loader::getImage(imageSrc);
190 
191  newParticle = new ImageParticle(img);
192  newParticle->setMap(mMap);
193  }
194  // Other
195  else
196  {
197  newParticle = new Particle;
198  newParticle->setMap(mMap);
199  }
200 
201  // Read and set the basic properties of the particle
202  const float offsetX = XML::getFloatProperty(
203  effectChildNode, "position-x", 0);
204  const float offsetY = XML::getFloatProperty(
205  effectChildNode, "position-y", 0);
206  const float offsetZ = XML::getFloatProperty(
207  effectChildNode, "position-z", 0);
208  const Vector position(static_cast<float>(pixelX) + offsetX,
209  static_cast<float>(pixelY) + offsetY,
210  offsetZ);
211  newParticle->moveTo(position);
212 
213  const int lifetime = XML::getProperty(effectChildNode, "lifetime", -1);
214  newParticle->setLifetime(lifetime);
215  const bool resizeable = "false" != XML::getProperty(effectChildNode,
216  "size-adjustable", "false");
217 
218  newParticle->setAllowSizeAdjust(resizeable);
219 
220  // Look for additional emitters for this particle
221  for_each_xml_child_node(emitterNode, effectChildNode)
222  {
223  if (xmlNameEqual(emitterNode, "emitter"))
224  {
225  ParticleEmitter *restrict const newEmitter =
226  new ParticleEmitter(
227  emitterNode,
228  newParticle,
229  mMap,
230  rotation,
231  dyePalettes);
232  newParticle->addEmitter(newEmitter);
233  }
234  else if (xmlNameEqual(emitterNode, "deatheffect"))
235  {
236  std::string deathEffect;
237  if ((node != nullptr) && XmlHaveChildContent(node))
238  deathEffect = XmlChildContent(emitterNode);
239 
240  char deathEffectConditions = 0x00;
241  if (XML::getBoolProperty(emitterNode, "on-floor", true))
242  {
243  deathEffectConditions += CAST_S8(
245  }
246  if (XML::getBoolProperty(emitterNode, "on-sky", true))
247  {
248  deathEffectConditions += CAST_S8(
250  }
251  if (XML::getBoolProperty(emitterNode, "on-other", false))
252  {
253  deathEffectConditions += CAST_S8(
255  }
256  if (XML::getBoolProperty(emitterNode, "on-impact", true))
257  {
258  deathEffectConditions += CAST_S8(
260  }
261  if (XML::getBoolProperty(emitterNode, "on-timeout", true))
262  {
263  deathEffectConditions += CAST_S8(
265  }
266  newParticle->setDeathEffect(
267  deathEffect, deathEffectConditions);
268  }
269  }
270 
271  mChildParticles.push_back(newParticle);
272  }
273 
274  doc->decRef();
275  return newParticle;
276 }

References Particle::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, Particle::moveTo(), restrict, XML::Document::rootNode(), Particle::setAllowSizeAdjust(), Particle::setDeathEffect(), Particle::setLifetime(), Actor::setMap(), SkipError_false, and UseVirtFs_true.

Referenced by Being::addItemParticles(), Being::addItemParticlesCards(), StatusEffect::getParticle(), StatusEffect::getStartParticle(), Map::initializeParticleEffects(), Being::recreateItemParticles(), LocalPlayer::setTestParticle(), ActorSprite::setupSpriteDisplay(), EffectManager::trigger(), EffectManager::triggerReturn(), and Particle::updateSelf().

◆ addTextRiseFadeOutEffect()

Particle * ParticleEngine::addTextRiseFadeOutEffect ( const std::string &  text,
const int  x,
const int  y,
const Color *const  color,
Font *const  font,
const bool  outline 
)

Creates a standalone text particle.

Definition at line 309 of file particleengine.cpp.

318 {
319  Particle *const newParticle = new TextParticle(
320  text,
321  color,
322  font,
323  outline);
324  newParticle->setMap(mMap);
325  newParticle->moveTo(static_cast<float>(x),
326  static_cast<float>(y));
327  newParticle->setVelocity(0.0F, 0.0F, 0.5F);
328  newParticle->setGravity(0.0015F);
329  newParticle->setLifetime(300);
330  newParticle->setFadeOut(100);
331  newParticle->setFadeIn(0);
332 
333  mChildParticles.push_back(newParticle);
334 
335  return newParticle;
336 }

References Ea::GameRecv::mMap, Particle::moveTo(), Particle::setFadeIn(), Particle::setFadeOut(), Particle::setGravity(), Particle::setLifetime(), Actor::setMap(), Particle::setVelocity(), x, and y.

Referenced by LocalPlayer::logic().

◆ addTextSplashEffect()

Particle * ParticleEngine::addTextSplashEffect ( const std::string &  text,
const int  x,
const int  y,
const Color *const  color,
Font *const  font,
const bool  outline 
)

Creates a standalone text particle.

Definition at line 278 of file particleengine.cpp.

285 {
286  Particle *const newParticle = new TextParticle(
287  text,
288  color,
289  font,
290  outline);
291  newParticle->setMap(mMap);
292  newParticle->moveTo(static_cast<float>(x),
293  static_cast<float>(y));
294  newParticle->setVelocity(
295  static_cast<float>((rand() % 100) - 50) / 200.0F, // X
296  static_cast<float>((rand() % 100) - 50) / 200.0F, // Y
297  (static_cast<float>((rand() % 100)) / 200.0F) + 4.0F); // Z
298 
299  newParticle->setGravity(0.1F);
300  newParticle->setBounce(0.5F);
301  newParticle->setLifetime(200);
302  newParticle->setFadeOut(100);
303 
304  mChildParticles.push_back(newParticle);
305 
306  return newParticle;
307 }

References Ea::GameRecv::mMap, Particle::moveTo(), Particle::setBounce(), Particle::setFadeOut(), Particle::setGravity(), Particle::setLifetime(), Actor::setMap(), Particle::setVelocity(), x, and y.

Referenced by Being::takeDamage().

◆ clear()

void ParticleEngine::clear ( )

Deletes all child particles and emitters.

Definition at line 338 of file particleengine.cpp.

339 {
341  mChildParticles.clear();
342  mChildMoveParticles.clear();
343 }

References delete_all(), mChildMoveParticles, and mChildParticles.

Referenced by Game::changeMap(), and ~ParticleEngine().

◆ createChild()

Particle * ParticleEngine::createChild ( )

Creates a blank particle as a child of the current particle Useful for creating target particles

Definition at line 121 of file particleengine.cpp.

122 {
123  Particle *const newParticle = new Particle;
124  newParticle->setMap(mMap);
125  mChildParticles.push_back(newParticle);
126  return newParticle;
127 }

References mChildParticles, mMap, and Actor::setMap().

Referenced by Being::fireMissile().

◆ setMap()

void ParticleEngine::setMap ( Map *const  map)
inline

Definition at line 121 of file particleengine.h.

122  { mMap = map; }

References mMap.

Referenced by Game::changeMap(), and initEngines().

◆ setupEngine()

void ParticleEngine::setupEngine ( )
static

Gives a particle the properties of an engine root particle and loads the particle-related config settings.

Definition at line 67 of file particleengine.cpp.

68 {
69  ParticleEngine::maxCount = config.getIntValue("particleMaxCount");
71  "particleFastPhysics"),
74  config.getIntValue("particleEmitterSkip") + 1;
77  ParticleEngine::enabled = config.getBoolValue("particleeffects");
78  logger->log1("Particle engine set up");
79 }

References config, emitterSkip, enabled, fastPhysics, fromInt, Configuration::getBoolValue(), Configuration::getIntValue(), Logger::log1(), logger, and maxCount.

Referenced by initEngines().

◆ update()

bool ParticleEngine::update ( )

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

Definition at line 81 of file particleengine.cpp.

82 {
83  if (mChildParticles.empty() || (mMap == nullptr))
84  return true;
85 
86  // Update child particles
87 
88  const int cameraX = viewport->getCameraX();
89  const int cameraY = viewport->getCameraY();
90  const float x1 = static_cast<float>(cameraX - 3000);
91  const float y1 = static_cast<float>(cameraY - 2000);
92  const float x2 = static_cast<float>(cameraX + 3000);
93  const float y2 = static_cast<float>(cameraY + 2000);
94 
95  for (ParticleIterator p = mChildParticles.begin(),
96  fp2 = mChildParticles.end(); p != fp2; )
97  {
98  Particle *restrict const particle = *p;
99  const float posX = particle->mPos.x;
100  const float posY = particle->mPos.y;
101  if (posX < x1 || posX > x2 || posY < y1 || posY > y2)
102  {
103  ++p;
104  continue;
105  }
106  // update particle
107  if (particle->update())
108  {
109  ++p;
110  }
111  else
112  {
113  mChildMoveParticles.remove(*p);
114  delete particle;
115  p = mChildParticles.erase(p);
116  }
117  }
118  return true;
119 }

References Viewport::getCameraX(), Viewport::getCameraY(), mChildMoveParticles, mChildParticles, mMap, restrict, and viewport.

Referenced by Game::logic().

Field Documentation

◆ emitterSkip

int ParticleEngine::emitterSkip = 1
static

◆ enabled

bool ParticleEngine::enabled = true
static

◆ fastPhysics

ParticlePhysicsT ParticleEngine::fastPhysics = ParticlePhysics::Best
static

Definition at line 50 of file particleengine.h.

Referenced by setupEngine(), and Particle::updateSelf().

◆ maxCount

int ParticleEngine::maxCount = 0
static

Definition at line 52 of file particleengine.h.

Referenced by ParticleEmitter::createParticles(), and setupEngine().

◆ mChildMoveParticles

Particles ParticleEngine::mChildMoveParticles
private

Definition at line 127 of file particleengine.h.

Referenced by clear(), and update().

◆ mChildParticles

Particles ParticleEngine::mChildParticles
private

Definition at line 126 of file particleengine.h.

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

◆ mMap

Map* ParticleEngine::mMap
private

Definition at line 128 of file particleengine.h.

Referenced by createChild(), setMap(), and update().

◆ PARTICLE_SKY

const float ParticleEngine::PARTICLE_SKY = 800.0F
static

Definition at line 49 of file particleengine.h.

Referenced by Particle::updateSelf().

◆ particleCount

int ParticleEngine::particleCount = 0
static

The documentation for this class was generated from the following files:
ParticleEngine::mMap
Map * mMap
Definition: particleengine.h:128
ParticleEngine::maxCount
static int maxCount
Definition: particleengine.h:52
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
Particle
Definition: particle.h:44
AnimationParticle
Definition: animationparticle.h:32
Particle::addEmitter
void addEmitter(ParticleEmitter *const emitter)
Definition: particle.h:100
SkipError_false
const bool SkipError_false
Definition: skiperror.h:29
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
ParticleEngine::particleCount
static int particleCount
Definition: particleengine.h:51
XML::findFirstChildByName
xmlNodePtr findFirstChildByName(const xmlNode *const parent, const char *const name)
Definition: libxml.cpp:288
XML::Document
Definition: libxml.h:52
AliveStatus::DEAD_IMPACT
@ DEAD_IMPACT
Definition: alivestatus.h:34
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
logger
Logger * logger
Definition: logger.cpp:88
Viewport::getCameraY
int getCameraY() const
Definition: viewport.h:120
XML::getBoolProperty
bool getBoolProperty(const xmlNodePtr node, const char *const name, const bool def)
Definition: libxml.cpp:268
Particle::setVelocity
void setVelocity(const float x, const float y, const float z)
Definition: particle.h:142
CAST_S8
#define CAST_S8
Definition: cast.h:25
Loader::getImage
Image * getImage(const std::string &idPath)
Definition: imageloader.cpp:85
ParticleEmitter
Definition: particleemitter.h:43
ParticleEngine::emitterSkip
static int emitterSkip
Definition: particleengine.h:53
ParticleEngine::enabled
static bool enabled
Definition: particleengine.h:55
ParticleEngine::mChildMoveParticles
Particles mChildMoveParticles
Definition: particleengine.h:127
Particle::setGravity
void setGravity(const float gravity)
Definition: particle.h:150
x
x
Definition: graphics_calcImageRect.hpp:72
UseVirtFs_true
const bool UseVirtFs_true
Definition: usevirtfs.h:29
Particle::setBounce
void setBounce(const float bouncieness)
Definition: particle.h:163
ParticleEngine::mChildParticles
Particles mChildParticles
Definition: particleengine.h:126
ParticleEngine::clear
void clear()
Definition: particleengine.cpp:338
Logger::log1
void log1(const char *const log_text)
Definition: logger.cpp:237
Configuration::getIntValue
int getIntValue(const std::string &key) const
Definition: configuration.cpp:403
Particle::setFadeOut
void setFadeOut(const int fadeOut)
Definition: particle.h:129
viewport
Viewport * viewport
Definition: viewport.cpp:35
config
Configuration config
Definition: configuration.cpp:51
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
Actor::setMap
virtual void setMap(Map *const map)
Definition: actor.cpp:47
Particle::setAllowSizeAdjust
void setAllowSizeAdjust(const bool adjust)
Definition: particle.h:202
Resource::decRef
virtual void decRef()
Definition: resource.cpp:49
restrict
#define restrict
Definition: localconsts.h:164
Dye::instantiate
static void instantiate(std::string &target, const std::string &palettes)
Definition: dye.cpp:96
fromInt
#define fromInt(val, name)
Definition: intdefines.h:45
XML::Document::rootNode
xmlNodePtr rootNode()
Definition: libxml.cpp:168
ParticlePhysicsT
ParticlePhysics ::T ParticlePhysicsT
Definition: particlephysics.h:32
Viewport::getCameraX
int getCameraX() const
Definition: viewport.h:114
TextParticle
Definition: textparticle.h:28
for_each_xml_child_node
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:160
Particle::setFadeIn
void setFadeIn(const int fadeIn)
Definition: particle.h:136
RotationalParticle
Definition: rotationalparticle.h:32
Vector
Definition: vector.h:38
AliveStatus::DEAD_TIMEOUT
@ DEAD_TIMEOUT
Definition: alivestatus.h:31
y
y
Definition: graphics_calcImageRect.hpp:72
ImageParticle
Definition: imageparticle.h:35
Particle::moveTo
void moveTo(const Vector &pos)
Definition: particle.h:106
Logger::log
void log(const char *const log_text,...)
Definition: logger.cpp:268
AliveStatus::DEAD_FLOOR
@ DEAD_FLOOR
Definition: alivestatus.h:32
Configuration::getBoolValue
bool getBoolValue(const std::string &key) const
Definition: configuration.cpp:596