Otclient  14/8/2020
particleemitter.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "particle.h"
24 #include "particleemitter.h"
25 #include "particlesystem.h"
26 #include <framework/core/clock.h>
28 #include "particlemanager.h"
29 
31 {
32  m_position = Point(0, 0);
33  m_duration = -1;
34  m_delay = 0;
35  m_burstRate = 1;
36  m_burstCount = 32;
37  m_currentBurst = 0;
38  m_elapsedTime = 0;
39  m_finished = false;
40  m_active = false;
41 }
42 
44 {
45  for(const OTMLNodePtr& childNode : node->children()) {
46  // self related
47  if(childNode->tag() == "position")
48  m_position = childNode->value<Point>();
49  else if(childNode->tag() == "duration")
50  m_duration = childNode->value<float>();
51  else if(childNode->tag() == "delay")
52  m_delay = childNode->value<float>();
53  else if(childNode->tag() == "burst-rate")
54  m_burstRate = childNode->value<float>();
55  else if(childNode->tag() == "burst-count")
56  m_burstCount = childNode->value<int>();
57  else if(childNode->tag() == "particle-type")
58  m_particleType = g_particles.getParticleType(childNode->value());
59  }
60 
61  if(!m_particleType)
62  stdext::throw_exception("emitter didn't provide a valid particle type");
63 }
64 
65 void ParticleEmitter::update(float elapsedTime, const ParticleSystemPtr& system)
66 {
67  m_elapsedTime += elapsedTime;
68 
69  // check if finished
70  if(m_duration > 0 && m_elapsedTime >= m_duration + m_delay) {
71  m_finished = true;
72  return;
73  }
74 
75  if(!m_active && m_elapsedTime > m_delay)
76  m_active = true;
77 
78  if(!m_active)
79  return;
80 
81  int nextBurst = std::floor((m_elapsedTime - m_delay) * m_burstRate) + 1;
82  const ParticleType *type = m_particleType.get();
83  for(int b = m_currentBurst; b < nextBurst; ++b) {
84  // every burst created at same position.
85  float pRadius = stdext::random_range(type->pMinPositionRadius, type->pMaxPositionRadius);
86  float pAngle = stdext::random_range(type->pMinPositionAngle, type->pMaxPositionAngle);
87 
88  Point pPosition = m_position + Point(pRadius * std::cos(pAngle), pRadius * std::sin(pAngle));
89 
90  for(int p = 0; p < m_burstCount; ++p) {
91  float pDuration = stdext::random_range(type->pMinDuration, type->pMaxDuration);
92 
93  // particles initial velocity
94  float pVelocityAbs = stdext::random_range(type->pMinVelocity, type->pMaxVelocity);
95  float pVelocityAngle = stdext::random_range(type->pMinVelocityAngle, type->pMaxVelocityAngle);
96  PointF pVelocity(pVelocityAbs * std::cos(pVelocityAngle), pVelocityAbs * std::sin(pVelocityAngle));
97 
98  // particles initial acceleration
99  float pAccelerationAbs = stdext::random_range(type->pMinAcceleration, type->pMaxAcceleration);
100  float pAccelerationAngle = stdext::random_range(type->pMinAccelerationAngle, type->pMaxAccelerationAngle);
101  PointF pAcceleration(pAccelerationAbs * std::cos(pAccelerationAngle), pAccelerationAbs * std::sin(pAccelerationAngle));
102 
103  ParticlePtr particle(new Particle(pPosition, type->pStartSize, type->pFinalSize,
104  pVelocity, pAcceleration,
105  pDuration, type->pIgnorePhysicsAfter,
106  type->pColors, type->pColorsStops,
107  type->pCompositionMode, type->pTexture));
108  system->addParticle(particle);
109  }
110  }
111 
112  m_currentBurst = nextBurst;
113 }
ParticleType::pMaxVelocity
float pMaxVelocity
Definition: particletype.h:52
ParticleType::pStartSize
Size pStartSize
Definition: particletype.h:45
ParticleType::pTexture
TexturePtr pTexture
Definition: particletype.h:65
ParticleType::pMinDuration
float pMinDuration
Definition: particletype.h:60
particlesystem.h
Particle
Definition: particle.h:29
ParticleType::pMaxDuration
float pMaxDuration
Definition: particletype.h:60
ParticleType::pMaxVelocityAngle
float pMaxVelocityAngle
Definition: particletype.h:53
ParticleType::pMinVelocityAngle
float pMinVelocityAngle
Definition: particletype.h:53
particleemitter.h
texturemanager.h
ParticleType::pColorsStops
std::vector< float > pColorsStops
Definition: particletype.h:64
OTMLNode::children
OTMLNodeList children()
Definition: otmlnode.cpp:170
Point
TPoint< int > Point
Definition: point.h:86
particlemanager.h
ParticleType::pMaxAcceleration
float pMaxAcceleration
Definition: particletype.h:56
clock.h
ParticleManager::getParticleType
ParticleTypePtr getParticleType(std::string name)
Definition: particlemanager.h:39
ParticleType::pColors
std::vector< Color > pColors
Definition: particletype.h:63
ParticleType::pMaxAccelerationAngle
float pMaxAccelerationAngle
Definition: particletype.h:57
ParticleEmitter::update
void update(float elapsedTime, const ParticleSystemPtr &system)
Definition: particleemitter.cpp:65
ParticleEmitter::load
void load(const OTMLNodePtr &node)
Definition: particleemitter.cpp:43
ParticleType::pMinVelocity
float pMinVelocity
Definition: particletype.h:52
ParticleType::pMinPositionAngle
float pMinPositionAngle
Definition: particletype.h:49
g_particles
ParticleManager g_particles
Definition: particlemanager.cpp:27
ParticleEmitter::ParticleEmitter
ParticleEmitter()
Definition: particleemitter.cpp:30
ParticleType::pMinAccelerationAngle
float pMinAccelerationAngle
Definition: particletype.h:57
ParticleType::pMinAcceleration
float pMinAcceleration
Definition: particletype.h:56
ParticleType::pFinalSize
Size pFinalSize
Definition: particletype.h:45
ParticleType::pMaxPositionAngle
float pMaxPositionAngle
Definition: particletype.h:49
stdext::throw_exception
void throw_exception(const std::string &what)
Throws a generic exception.
Definition: exception.h:43
particle.h
ParticleType::pCompositionMode
Painter::CompositionMode pCompositionMode
Definition: particletype.h:67
ParticleType::pIgnorePhysicsAfter
float pIgnorePhysicsAfter
Definition: particletype.h:60
stdext::shared_object_ptr< OTMLNode >
ParticleType::pMaxPositionRadius
float pMaxPositionRadius
Definition: particletype.h:48
ParticleType
Definition: particletype.h:31
TPoint< int >
stdext::random_range
long random_range(long min, long max)
Definition: math.cpp:48
stdext::shared_object_ptr::get
T * get() const
Definition: shared_object.h:82
ParticleType::pMinPositionRadius
float pMinPositionRadius
Definition: particletype.h:48