Otclient  14/8/2020
particle.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 "graphics.h"
25 #include <framework/core/clock.h>
26 
27 Particle::Particle(const Point& pos, const Size& startSize, const Size& finalSize, const PointF& velocity, const PointF& acceleration, float duration, float ignorePhysicsAfter, const std::vector<Color>& colors, const std::vector<float>& colorsStops, Painter::CompositionMode compositionMode, TexturePtr texture)
28 {
29  m_colors = colors;
30  m_colorsStops = colorsStops;
31 
32  m_position = PointF(pos.x, pos.y);
33  m_startSize = startSize;
34  m_finalSize = finalSize;
35  m_velocity = velocity;
36  m_acceleration = acceleration;
37 
38  m_compositionMode = compositionMode;
39  m_texture = texture;
40  m_duration = duration;
41  m_ignorePhysicsAfter = ignorePhysicsAfter;
42  m_elapsedTime = 0;
43  m_finished = false;
44 }
45 
47 {
49 
50  if(!m_texture)
51  g_painter->drawFilledRect(m_rect);
52  else {
55  }
56 }
57 
58 void Particle::update(float elapsedTime)
59 {
60  // check if finished
61  if(m_duration >= 0 && m_elapsedTime >= m_duration) {
62  m_finished = true;
63  return;
64  }
65 
66  updateColor();
67  updateSize();
68  updatePosition(elapsedTime);
69 
70  m_elapsedTime += elapsedTime;
71 }
72 
73 void Particle::updatePosition(float elapsedTime)
74 {
75  if(m_ignorePhysicsAfter < 0 || m_elapsedTime < m_ignorePhysicsAfter ) {
76  // update position
77  PointF delta = m_velocity * elapsedTime;
78  delta.y *= -1; // painter orientate Y axis in the inverse direction
79 
80  PointF position = m_position + delta;
81 
82  if(m_position != position) {
83  m_position += delta;
84  }
85 
86  // update acceleration
87  m_velocity += m_acceleration * elapsedTime;
88  }
89 
90  m_rect.move((int)m_position.x - m_size.width() / 2, (int)m_position.y - m_size.height() / 2);
91 }
92 
93 void Particle::updateSize()
94 {
95  m_size = m_startSize + (m_finalSize - m_startSize) / m_duration * m_elapsedTime;
96  m_rect.resize(m_size);
97 }
98 
99 void Particle::updateColor()
100 {
101  float currentLife = m_elapsedTime / m_duration;
102  if(currentLife < m_colorsStops[1]) {
103  float range = m_colorsStops[1] - m_colorsStops[0];
104  float factor = (currentLife - m_colorsStops[0])/range;
105  m_color = m_colors[0] * (1.0f - factor) + m_colors[1] * factor;
106  } else {
107  if(m_colors.size() > 1) {
108  m_colors.erase(m_colors.begin());
109  m_colorsStops.erase(m_colorsStops.begin());
110  } else {
111  if(m_color != m_colors[0]) {
112  m_color = m_colors[0];
113  }
114  }
115  }
116 }
Painter::setColor
virtual void setColor(const Color &color)
Definition: painter.h:77
TRect::resize
void resize(const TSize< T > &size)
Definition: rect.h:100
graphics.h
PointF
TPoint< float > PointF
Definition: point.h:87
TPoint::y
T y
Definition: point.h:83
Painter::setCompositionMode
virtual void setCompositionMode(CompositionMode compositionMode)=0
Particle::Particle
Particle(const Point &pos, const Size &startSize, const Size &finalSize, const PointF &velocity, const PointF &acceleration, float duration, float ignorePhysicsAfter, const std::vector< Color > &colors, const std::vector< float > &colorsStops, Painter::CompositionMode compositionMode=Painter::CompositionMode_Normal, TexturePtr texture=nullptr)
Definition: particle.cpp:27
PainterOGL::m_texture
Texture * m_texture
Definition: painterogl.h:110
TSize::width
int width() const
Definition: size.h:43
clock.h
Painter::CompositionMode
CompositionMode
Definition: painter.h:38
Painter::m_color
Color m_color
Definition: painter.h:116
TPoint::x
T x
Definition: point.h:83
Particle::update
void update(float elapsedTime)
Definition: particle.cpp:58
TRect::move
void move(T x, T y)
Definition: rect.h:102
TSize::height
int height() const
Definition: size.h:44
particle.h
stdext::shared_object_ptr< Texture >
g_painter
Painter * g_painter
Definition: painter.cpp:28
TPoint< int >
Painter::drawFilledRect
virtual void drawFilledRect(const Rect &dest)=0
Particle::render
void render()
Definition: particle.cpp:46
TSize< int >
Painter::m_compositionMode
CompositionMode m_compositionMode
Definition: painter.h:115
Painter::drawTexturedRect
virtual void drawTexturedRect(const Rect &dest, const TexturePtr &texture, const Rect &src)=0