From fc7b4579e49aaeecc81919e247e03f68bd5abfd4 Mon Sep 17 00:00:00 2001 From: chai Date: Sun, 18 Nov 2018 22:32:55 +0800 Subject: =?UTF-8?q?*=E7=B2=92=E5=AD=90=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/Graphics/particles/je_particle.cpp | 56 ++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 5 deletions(-) (limited to 'src/libjin/Graphics/particles/je_particle.cpp') diff --git a/src/libjin/Graphics/particles/je_particle.cpp b/src/libjin/Graphics/particles/je_particle.cpp index 20bbd03..53f4570 100644 --- a/src/libjin/Graphics/particles/je_particle.cpp +++ b/src/libjin/Graphics/particles/je_particle.cpp @@ -1,5 +1,9 @@ +#include "../../math/je_math.h" + #include "je_particle.h" +using namespace JinEngine::Math; + namespace JinEngine { namespace Graphics @@ -7,20 +11,62 @@ namespace JinEngine namespace Particles { + Particle::Particle(const Graphic* grc) + : graphic(grc) + { + reset(); + } + void Particle::reset() { - lifeTime = 0.0f; + transform.set(0, 0, 1, 1, 0, 0, 0); + lifeTime = 1.0f; life = 0.0f; - position.set(0, 0); - direction = 0.0f; speed.set(0, 0); linearAcceleration.set(0, 0); radialAcceleration = 0.0f; - size = 1; - sizeBegin = 1; + 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(colorStart.r, colorEnd.r, t); + color.g = lerp(colorStart.g, colorEnd.g, t); + color.b = lerp(colorStart.b, colorEnd.b, t); + color.a = lerp(colorStart.a, colorEnd.a, t); + // Lerp scale. + Vector2 scale = transform.getScale(); + scale.x = lerp(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(); + } + } } } \ No newline at end of file -- cgit v1.1-26-g67d0