hx3d  1
2D/3D Simple Game Framework
particle.cpp
1 /*
2  Particle.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/graphics/particle.hpp"
22 
23 #include "hx3d/math/interpolation.hpp"
24 
25 namespace hx3d {
26 namespace graphics {
27 
28 Particle::Particle() {
29  reset();
30 }
31 
33  life = baseLife = 0.f;
34  dead = false;
35  rotation = 0.f;
36  rotationSpeed = 0.f;
37  color = Color::White;
38 }
39 
40 void Particle::setTexture(const Ptr<Texture>& texture) {
41  sprite.setTexture(texture);
42 }
43 
44 void Particle::update(const float delta) {
45  life -= delta;
46 
47  if (life <= 0.f) {
48  dead = true;
49  return;
50  }
51 
52  else {
53  float percentage = life / baseLife;
54  color.a = math::interpolate(0, 255, percentage, math::Interpolation::Linear);
55  float z_pos = color.a / 255.f; // between 0 && 1
56  sprite.setTint(color);
57 
58  position.x -= velocity.x * delta;
59  position.y -= velocity.y * delta;
60  position.z = z_pos;
61 
62  velocity.x -= gravity.x * delta;
63  velocity.y -= gravity.y * delta;
64 
65  rotation += rotationSpeed * delta;
66  }
67 }
68 
69 void Particle::draw(Batch& batch) {
70  sprite.transform.position = position;
71  sprite.transform.size = size;
72  sprite.transform.rotation.z = rotation;
73 
74  batch.draw(sprite);
75 }
76 
77 } /* graphics */
78 } /* hx3d */
void setTint(Color tint)
Set the mesh tint.
Definition: mesh.cpp:43
unsigned char a
Alpha component.
Definition: color.hpp:102
void update(const float delta)
Update the particle.
Definition: particle.cpp:44
glm::vec3 position
Position.
Definition: transform.hpp:80
void setTexture(const Ptr< Texture > &texture)
Set the sprite texture.
Definition: sprite.cpp:36
static Color White
White color.
Definition: color.hpp:107
T interpolate(T a, T b, float t, Interpolation type)
Interpolate between two values.
hx3d framework namespace
Definition: audio.hpp:26
Simple base batch implementation. Draw at each draw call.
Definition: batch.hpp:36
glm::vec3 rotation
Rotation.
Definition: transform.hpp:86
virtual void reset() override
Reset the element.
Definition: particle.cpp:32
virtual void draw(Mesh &mesh) override
Draw the mesh.
Definition: batch.cpp:53
glm::vec3 size
Size.
Definition: transform.hpp:84
Transform transform
Mesh transformation.
Definition: mesh.hpp:90
void setTexture(const Ptr< Texture > &texture)
Set the texture.
Definition: particle.cpp:40
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34
void draw(Batch &batch)
Draw the particle.
Definition: particle.cpp:69