aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Graphics/particles/je_particle.cpp
blob: 53f4570d6b3284ca1408f92cb882d48e0c1b525d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "../../math/je_math.h"

#include "je_particle.h"

using namespace JinEngine::Math;

namespace JinEngine
{
    namespace Graphics
    {
        namespace Particles
        {

            Particle::Particle(const Graphic* grc)
                : graphic(grc)
            {
                reset();
            }

            void Particle::reset()
            {
                transform.set(0, 0, 1, 1, 0, 0, 0);
                lifeTime = 1.0f; 
                life = 0.0f; 
                speed.set(0, 0); 
                linearAcceleration.set(0, 0); 
                radialAcceleration = 0.0f; 
                angularSpeed = 0;
                scaleBegin = 1;
                scaleEnd = 1;
                color = Color::WHITE;
                colorStart = Color::WHITE;
                colorEnd = Color::WHITE;
                alive = true;
            }

            void Particle::update(float dt)
            {
                float t = life / lifeTime;
                // Lerp color 
                color.r = lerp<int>(colorStart.r, colorEnd.r, t);
                color.g = lerp<int>(colorStart.g, colorEnd.g, t);
                color.b = lerp<int>(colorStart.b, colorEnd.b, t);
                color.a = lerp<int>(colorStart.a, colorEnd.a, t);
                // Lerp scale.
                Vector2<float> scale = transform.getScale();
                scale.x = lerp<float>(scaleBegin, scaleEnd, t);
                scale.y = scale.x;
                transform.setScale(scale.x, scale.y);
                // Calculate position.
                speed += linearAcceleration * dt;
                transform.move(speed * dt);
                // Calculate rotation.
                angularSpeed += radialAcceleration * dt;
                transform.rotate(angularSpeed * dt);
                // Update life time.
                life += dt;
                alive = life < lifeTime;
            }

            void Particle::render()
            {
                Color c = gl.getColor();
                gl.setColor(color);
                if (graphic != nullptr)
                    graphic->render(transform);
                gl.getColor();
            }

        }
    }
}