2011-12-07 20:58:09 +01:00
|
|
|
#include "particlessystem.h"
|
|
|
|
#include "graphics.h"
|
|
|
|
#include <framework/core/clock.h>
|
2011-12-09 06:44:44 +01:00
|
|
|
#include <framework/graphics/texturemanager.h>
|
|
|
|
#include <framework/util/tools.h>
|
2011-12-07 20:58:09 +01:00
|
|
|
|
2011-12-08 09:04:16 +01:00
|
|
|
Particle::Particle(const Rect& rect, float vx, float vy, float ax, float ay, float duration, const Color& color, TexturePtr texture)
|
2011-12-07 20:58:09 +01:00
|
|
|
{
|
|
|
|
m_rect = rect;
|
2011-12-08 08:28:52 +01:00
|
|
|
m_ix = rect.x(); m_iy = rect.y();
|
2011-12-07 20:58:09 +01:00
|
|
|
m_vx = vx; m_vy = vy;
|
|
|
|
m_ax = ax; m_ay = ay;
|
|
|
|
m_color = color;
|
|
|
|
m_texture = texture;
|
2011-12-08 09:04:16 +01:00
|
|
|
m_duration = duration;
|
2011-12-08 08:28:52 +01:00
|
|
|
m_startTicks = g_clock.ticks();
|
2011-12-07 20:58:09 +01:00
|
|
|
m_finished = false;
|
|
|
|
}
|
|
|
|
|
2011-12-08 08:28:52 +01:00
|
|
|
Particle::~Particle()
|
|
|
|
{
|
2011-12-09 15:23:32 +01:00
|
|
|
//dump << "deleted";
|
2011-12-08 08:28:52 +01:00
|
|
|
}
|
|
|
|
|
2011-12-07 20:58:09 +01:00
|
|
|
void Particle::render()
|
|
|
|
{
|
2011-12-09 15:23:32 +01:00
|
|
|
g_painter.setColor(m_color);
|
2011-12-07 20:58:09 +01:00
|
|
|
|
|
|
|
if(!m_texture)
|
2011-12-09 15:23:32 +01:00
|
|
|
g_painter.drawFilledRect(m_rect);
|
2011-12-09 06:44:44 +01:00
|
|
|
else {
|
2011-12-09 15:23:32 +01:00
|
|
|
g_painter.setCompositionMode(Painter::CompositionMode_AdditiveSource);
|
|
|
|
g_painter.drawTexturedRect(m_rect, m_texture);
|
|
|
|
g_painter.setCompositionMode(Painter::CompositionMode_SourceOver);
|
2011-12-09 06:44:44 +01:00
|
|
|
}
|
2011-12-07 20:58:09 +01:00
|
|
|
}
|
|
|
|
|
2011-12-08 08:28:52 +01:00
|
|
|
void Particle::update()
|
|
|
|
{
|
|
|
|
ticks_t t = g_clock.ticks() - m_startTicks;
|
|
|
|
|
2011-12-08 09:04:16 +01:00
|
|
|
// check if finished
|
2011-12-09 06:44:44 +01:00
|
|
|
if(m_duration >= 0 && t > m_duration * 1000) {
|
2011-12-08 09:04:16 +01:00
|
|
|
m_finished = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//update position
|
2011-12-09 06:44:44 +01:00
|
|
|
m_rect.moveTo(m_ix + (m_vx * t / 1000.0) + (m_ax * t*t / (2.0 * 1000 * 1000)),
|
|
|
|
m_iy + (m_vy * t / 1000.0) + (m_ay * t*t / (2.0 * 1000 * 1000)));
|
2011-12-08 08:28:52 +01:00
|
|
|
}
|
|
|
|
|
2011-12-09 06:44:44 +01:00
|
|
|
ParticleEmitter::ParticleEmitter(const Point& position, float duration, int particlesPerSecond)
|
2011-12-07 20:58:09 +01:00
|
|
|
{
|
|
|
|
m_position = position;
|
|
|
|
m_duration = duration;
|
|
|
|
m_particlesPerSecond = particlesPerSecond;
|
|
|
|
m_createdParticles = 0;
|
|
|
|
m_startTicks = g_clock.ticks();
|
|
|
|
m_finished = false;
|
2011-12-09 06:44:44 +01:00
|
|
|
|
|
|
|
// particles default configuration. (make them reasonable detect missing properties on scripts)
|
|
|
|
m_pPositionMinRadius = 0; m_pPositionMaxRadius = 3;
|
|
|
|
m_pPositionMinAngle = 0; m_pPositionMaxAngle = 90;
|
|
|
|
m_pMinSize = Size(32, 32); m_pMaxSize = Size(32, 32);
|
|
|
|
m_pMinDuration = 0; m_pMaxDuration = 10;
|
2011-12-07 20:58:09 +01:00
|
|
|
}
|
|
|
|
|
2011-12-09 06:44:44 +01:00
|
|
|
void ParticleEmitter::render()
|
2011-12-07 20:58:09 +01:00
|
|
|
{
|
2011-12-09 06:44:44 +01:00
|
|
|
// testing
|
|
|
|
//g_graphics.bindColor(Color(255, 255, 255));
|
|
|
|
//g_graphics.drawFilledRect(Rect(0, 0, 400, 400));
|
|
|
|
|
2011-12-07 20:58:09 +01:00
|
|
|
for(auto it = m_particles.begin(), end = m_particles.end(); it != end; ++it)
|
2011-12-09 06:44:44 +01:00
|
|
|
(*it)->render();
|
2011-12-07 20:58:09 +01:00
|
|
|
}
|
|
|
|
|
2011-12-09 06:44:44 +01:00
|
|
|
void ParticleEmitter::update()
|
2011-12-07 20:58:09 +01:00
|
|
|
{
|
|
|
|
ticks_t elapsedTicks = g_clock.ticks() - m_startTicks;
|
|
|
|
|
|
|
|
// check if finished
|
2011-12-09 06:44:44 +01:00
|
|
|
if(m_duration >= 0 && elapsedTicks > m_duration * 1000) {
|
2011-12-07 20:58:09 +01:00
|
|
|
m_finished = true;
|
2011-12-08 08:28:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update particles
|
|
|
|
for(auto it = m_particles.begin(), end = m_particles.end(); it != end;) {
|
2011-12-09 06:44:44 +01:00
|
|
|
const ParticlePtr& particle = *it;
|
|
|
|
if(particle->hasFinished()) {
|
2011-12-08 08:28:52 +01:00
|
|
|
it = m_particles.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
2011-12-09 06:44:44 +01:00
|
|
|
particle->update();
|
2011-12-08 08:28:52 +01:00
|
|
|
++it;
|
|
|
|
}
|
2011-12-07 20:58:09 +01:00
|
|
|
|
|
|
|
// create some particles
|
2011-12-09 06:44:44 +01:00
|
|
|
TexturePtr tex = g_textures.getTexture("circle2.png");
|
|
|
|
|
2011-12-08 09:04:16 +01:00
|
|
|
int currentParticles = 1 + elapsedTicks / 1000.0 * m_particlesPerSecond;
|
2011-12-07 20:58:09 +01:00
|
|
|
for(int i = m_createdParticles; i < currentParticles; ++i) {
|
2011-12-09 06:44:44 +01:00
|
|
|
|
|
|
|
// \/ not working properly
|
|
|
|
float pRadius = Fw::randomRange(m_pPositionMinRadius, m_pPositionMaxRadius);
|
|
|
|
float pAngle = Fw::randomRange(m_pPositionMinAngle, m_pPositionMaxAngle) * 3.141592 / 180.0;
|
|
|
|
|
|
|
|
Point pPosition = Point(cos(pAngle * pRadius), sin(pAngle * pRadius));
|
|
|
|
|
|
|
|
Size pSize = Size(Fw::randomRange(m_pMinSize.width(), m_pMaxSize.width()), Fw::randomRange(m_pMinSize.height(), m_pMaxSize.height()));
|
|
|
|
float pDuration = Fw::randomRange(m_pMinDuration, m_pMaxDuration);
|
|
|
|
|
2011-12-08 08:28:52 +01:00
|
|
|
// todo: add random data generation
|
2011-12-09 06:44:44 +01:00
|
|
|
m_particles.push_back(ParticlePtr(new Particle(Rect(m_position + pPosition, pSize), 16, 8, 0, 0, pDuration, Color(255, 0, 0, 32), tex)));
|
2011-12-07 20:58:09 +01:00
|
|
|
}
|
2011-12-08 08:28:52 +01:00
|
|
|
m_createdParticles = currentParticles;
|
2011-12-07 20:58:09 +01:00
|
|
|
}
|
|
|
|
|
2011-12-09 06:44:44 +01:00
|
|
|
void ParticlesSystem::add(const ParticleEmitterPtr& emitter)
|
2011-12-07 20:58:09 +01:00
|
|
|
{
|
|
|
|
m_emitters.push_back(emitter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticlesSystem::render()
|
|
|
|
{
|
|
|
|
for(auto it = m_emitters.begin(), end = m_emitters.end(); it != end; ++it)
|
2011-12-09 06:44:44 +01:00
|
|
|
(*it)->render();
|
2011-12-07 20:58:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParticlesSystem::update()
|
|
|
|
{
|
|
|
|
for(auto it = m_emitters.begin(), end = m_emitters.end(); it != end;) {
|
2011-12-09 06:44:44 +01:00
|
|
|
const ParticleEmitterPtr& emitter = *it;
|
|
|
|
if(emitter->hasFinished()) {
|
2011-12-08 08:28:52 +01:00
|
|
|
it = m_emitters.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
2011-12-09 06:44:44 +01:00
|
|
|
emitter->update();
|
2011-12-07 20:58:09 +01:00
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|