particles duration

This commit is contained in:
Henrique Santiago 2011-12-08 06:04:16 -02:00
parent 34617091bc
commit 7233e4e039
2 changed files with 15 additions and 7 deletions

View File

@ -2,7 +2,7 @@
#include "graphics.h"
#include <framework/core/clock.h>
Particle::Particle(const Rect& rect, float vx, float vy, float ax, float ay, const Color& color, TexturePtr texture)
Particle::Particle(const Rect& rect, float vx, float vy, float ax, float ay, float duration, const Color& color, TexturePtr texture)
{
m_rect = rect;
m_ix = rect.x(); m_iy = rect.y();
@ -10,6 +10,7 @@ Particle::Particle(const Rect& rect, float vx, float vy, float ax, float ay, con
m_ax = ax; m_ay = ay;
m_color = color;
m_texture = texture;
m_duration = duration;
m_startTicks = g_clock.ticks();
m_finished = false;
}
@ -33,11 +34,18 @@ void Particle::update()
{
ticks_t t = g_clock.ticks() - m_startTicks;
// check if finished
if(m_duration > 0 && t > m_duration * 1000) {
m_finished = true;
return;
}
//update position
m_rect.moveTo(m_ix + (m_vx * t / 1000.0) + (m_ax * t*t / 2000.0),
m_iy + (m_vy * t / 1000.0) + (m_ay * t*t / 2000.0));
}
Emitter::Emitter(const Point& position, int duration, int particlesPerSecond)
Emitter::Emitter(const Point& position, float duration, int particlesPerSecond)
{
m_position = position;
m_duration = duration;
@ -74,10 +82,10 @@ void Emitter::update()
}
// create some particles
int currentParticles = elapsedTicks / 1000.0 * m_particlesPerSecond;
int currentParticles = 1 + elapsedTicks / 1000.0 * m_particlesPerSecond;
for(int i = m_createdParticles; i < currentParticles; ++i) {
// todo: add random data generation
m_particles.push_back(Particle(Rect(100, 100, 16, 16), 16, 8, 0, 0));
m_particles.push_back(Particle(Rect(100, 100, 16, 16), 16, 8, 0, 0, 1.5));
}
m_createdParticles = currentParticles;
}
@ -100,7 +108,6 @@ void ParticlesSystem::update()
it = m_emitters.erase(it);
continue;
}
(*it).update();
++it;
}

View File

@ -4,7 +4,7 @@
struct Particle {
public:
Particle(const Rect& rect, float vx, float vy, float ax, float ay, const Color& color = Color(255, 255, 255), TexturePtr texture = nullptr);
Particle(const Rect& rect, float vx, float vy, float ax, float ay, float duration, const Color& color = Color(255, 255, 255), TexturePtr texture = nullptr);
~Particle();
void render();
@ -22,6 +22,7 @@ private:
Color m_color;
TexturePtr m_texture;
float m_duration;
ticks_t m_startTicks;
bool m_finished;
};
@ -29,7 +30,7 @@ private:
class Emitter {
public:
Emitter(const Point& position, int duration, int particlesPerSecond);
Emitter(const Point& position, float duration, int particlesPerSecond);
void render();
void update();