Otclient  14/8/2020
particleaffector.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 "particleaffector.h"
25 #include <framework/core/clock.h>
26 
28 {
29  m_active = false;
30  m_finished = false;
31  m_delay = 0;
32  m_duration = 0;
33  m_elapsedTime = 0;
34 }
35 
36 void ParticleAffector::update(float elapsedTime)
37 {
38  if(m_duration >= 0 && m_elapsedTime >= m_duration + m_delay) {
39  m_finished = true;
40  return;
41  }
42 
44  m_active = true;
45 
46  m_elapsedTime += elapsedTime;
47 }
48 
50 {
51  float minDelay = 0, maxDelay = 0;
52  float minDuration = -1, maxDuration = -1;
53 
54  for(const OTMLNodePtr& childNode : node->children()) {
55  if(childNode->tag() == "delay") {
56  minDelay = childNode->value<float>();
57  maxDelay = childNode->value<float>();
58  } else if(childNode->tag() == "min-delay")
59  minDelay = childNode->value<float>();
60  else if(childNode->tag() == "max-delay")
61  maxDelay = childNode->value<float>();
62  else if(childNode->tag() == "duration") {
63  minDuration = childNode->value<float>();
64  maxDuration = childNode->value<float>();
65  } else if(childNode->tag() == "min-duration")
66  minDuration = childNode->value<float>();
67  else if(childNode->tag() == "max-duration")
68  maxDuration = childNode->value<float>();
69  }
70 
71  m_delay = stdext::random_range(minDelay, maxDelay);
72  m_duration = stdext::random_range(minDuration, maxDuration);
73 }
74 
76 {
78 
79  m_angle = 270 * DEG_TO_RAD;
80  m_gravity = 9.8;
81 
82  for(const OTMLNodePtr& childNode : node->children()) {
83  if(childNode->tag() == "angle")
84  m_angle = childNode->value<float>() * DEG_TO_RAD;
85  else if(childNode->tag() == "gravity")
86  m_gravity = childNode->value<float>();
87  }
88 }
89 
90 void GravityAffector::updateParticle(const ParticlePtr& particle, float elapsedTime)
91 {
92  if(!m_active)
93  return;
94 
95  PointF velocity = particle->getVelocity();
96  velocity += PointF(m_gravity * elapsedTime * std::cos(m_angle), m_gravity * elapsedTime * std::sin(m_angle));
97  particle->setVelocity(velocity);
98 }
99 
101 {
103 
104  m_acceleration = 32;
105  m_reduction = 0;
106  m_repelish = false;
107 
108  for(const OTMLNodePtr& childNode : node->children()) {
109  if(childNode->tag() == "position")
110  m_position = childNode->value<Point>();
111  else if(childNode->tag() == "acceleration")
112  m_acceleration = childNode->value<float>();
113  else if(childNode->tag() == "velocity-reduction-percent")
114  m_reduction = childNode->value<float>();
115  else if(childNode->tag() == "repelish")
116  m_repelish = childNode->value<bool>();
117  }
118 }
119 
120 void AttractionAffector::updateParticle(const ParticlePtr& particle, float elapsedTime)
121 {
122  if(!m_active)
123  return;
124 
125  PointF pPosition = particle->getPosition();
126  PointF d = PointF(m_position.x - pPosition.x, pPosition.y - m_position.y);
127  if(d.length() == 0)
128  return;
129 
130  PointF direction = PointF(1, 1);
131  if(m_repelish)
132  direction = PointF(-1, -1);
133 
134  PointF pVelocity = particle->getVelocity() + (d / d.length() * m_acceleration * elapsedTime) * direction;
135  particle->setVelocity(pVelocity - pVelocity * m_reduction/100.0 * elapsedTime);
136 }
ParticleAffector::m_elapsedTime
float m_elapsedTime
Definition: particleaffector.h:43
AttractionAffector::load
void load(const OTMLNodePtr &node)
Definition: particleaffector.cpp:100
PointF
TPoint< float > PointF
Definition: point.h:87
TPoint::y
T y
Definition: point.h:83
AttractionAffector::updateParticle
void updateParticle(const ParticlePtr &particle, float elapsedTime)
Definition: particleaffector.cpp:120
ParticleAffector::m_finished
bool m_finished
Definition: particleaffector.h:41
OTMLNode::children
OTMLNodeList children()
Definition: otmlnode.cpp:170
particleaffector.h
clock.h
DEG_TO_RAD
#define DEG_TO_RAD
Definition: const.h:28
ParticleAffector::m_active
bool m_active
Definition: particleaffector.h:41
TPoint::x
T x
Definition: point.h:83
GravityAffector::updateParticle
void updateParticle(const ParticlePtr &particle, float elapsedTime)
Definition: particleaffector.cpp:90
GravityAffector::load
void load(const OTMLNodePtr &node)
Definition: particleaffector.cpp:75
ParticleAffector::ParticleAffector
ParticleAffector()
Definition: particleaffector.cpp:27
TPoint::length
float length() const
Definition: point.h:76
particle.h
ParticleAffector::load
virtual void load(const OTMLNodePtr &node)
Definition: particleaffector.cpp:49
stdext::shared_object_ptr< OTMLNode >
TPoint< float >
stdext::random_range
long random_range(long min, long max)
Definition: math.cpp:48
ParticleAffector::m_duration
float m_duration
Definition: particleaffector.h:42
ParticleAffector::m_delay
float m_delay
Definition: particleaffector.h:42
ParticleAffector::update
void update(float elapsedTime)
Definition: particleaffector.cpp:36