parent
d4a6308705
commit
262f54ce3b
@ -0,0 +1,21 @@
|
|||||||
|
#include "particlesmanager.h"
|
||||||
|
|
||||||
|
ParticlesManager g_particlesManager;
|
||||||
|
|
||||||
|
void ParticlesManager::add(ParticlesSystem particleSystem)
|
||||||
|
{
|
||||||
|
// check it has emitters
|
||||||
|
m_particlesSystems.push_back(particleSystem);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ParticlesManager::render()
|
||||||
|
{
|
||||||
|
for(auto it = m_particlesSystems.begin(), end = m_particlesSystems.end(); it != end; ++it)
|
||||||
|
(*it).render();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ParticlesManager::update()
|
||||||
|
{
|
||||||
|
for(auto it = m_particlesSystems.begin(), end = m_particlesSystems.end(); it != end; ++it)
|
||||||
|
(*it).update();
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
#include "particlessystem.h"
|
||||||
|
|
||||||
|
class ParticlesManager {
|
||||||
|
public:
|
||||||
|
void add(ParticlesSystem particleSystem);
|
||||||
|
|
||||||
|
void render();
|
||||||
|
void update();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::list<ParticlesSystem> m_particlesSystems;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern ParticlesManager g_particlesManager;
|
@ -0,0 +1,75 @@
|
|||||||
|
#include "particlessystem.h"
|
||||||
|
#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)
|
||||||
|
{
|
||||||
|
m_rect = rect;
|
||||||
|
m_vx = vx; m_vy = vy;
|
||||||
|
m_ax = ax; m_ay = ay;
|
||||||
|
m_color = color;
|
||||||
|
m_texture = texture;
|
||||||
|
m_finished = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Particle::render()
|
||||||
|
{
|
||||||
|
g_graphics.bindColor(m_color);
|
||||||
|
|
||||||
|
if(!m_texture)
|
||||||
|
g_graphics.drawFilledRect(m_rect);
|
||||||
|
else
|
||||||
|
g_graphics.drawTexturedRect(m_rect, m_texture);
|
||||||
|
}
|
||||||
|
|
||||||
|
Emitter::Emitter(const Point& position, int duration, int particlesPerSecond)
|
||||||
|
{
|
||||||
|
m_position = position;
|
||||||
|
m_duration = duration;
|
||||||
|
m_particlesPerSecond = particlesPerSecond;
|
||||||
|
m_createdParticles = 0;
|
||||||
|
m_startTicks = g_clock.ticks();
|
||||||
|
m_finished = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Emitter::render()
|
||||||
|
{
|
||||||
|
for(auto it = m_particles.begin(), end = m_particles.end(); it != end; ++it)
|
||||||
|
(*it).render();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Emitter::update()
|
||||||
|
{
|
||||||
|
ticks_t elapsedTicks = g_clock.ticks() - m_startTicks;
|
||||||
|
|
||||||
|
// check if finished
|
||||||
|
if(m_duration > 0 && elapsedTicks > m_duration * 1000)
|
||||||
|
m_finished = true;
|
||||||
|
|
||||||
|
// create some particles
|
||||||
|
int currentParticles = elapsedTicks / 1000.0 * m_particlesPerSecond;
|
||||||
|
for(int i = m_createdParticles; i < currentParticles; ++i) {
|
||||||
|
Particle particle = Particle(Rect(100, 100, 16, 16), 0, 0, 0, 0);
|
||||||
|
m_particles.push_back(particle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ParticlesSystem::add(Emitter emitter)
|
||||||
|
{
|
||||||
|
m_emitters.push_back(emitter);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ParticlesSystem::render()
|
||||||
|
{
|
||||||
|
for(auto it = m_emitters.begin(), end = m_emitters.end(); it != end; ++it)
|
||||||
|
(*it).render();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ParticlesSystem::update()
|
||||||
|
{
|
||||||
|
for(auto it = m_emitters.begin(), end = m_emitters.end(); it != end;) {
|
||||||
|
Emitter emitter = *it;
|
||||||
|
emitter.update();
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,78 @@
|
|||||||
|
#include <framework/global.h>
|
||||||
|
#include <framework/graphics/texture.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
void render();
|
||||||
|
|
||||||
|
private:
|
||||||
|
Rect m_rect;
|
||||||
|
float m_vx, m_vy;
|
||||||
|
float m_ax, m_ay;
|
||||||
|
|
||||||
|
Color m_color;
|
||||||
|
TexturePtr m_texture;
|
||||||
|
bool m_finished;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Emitter {
|
||||||
|
public:
|
||||||
|
|
||||||
|
Emitter(const Point& position, int duration, int particlesPerSecond);
|
||||||
|
|
||||||
|
void render();
|
||||||
|
void update();
|
||||||
|
|
||||||
|
private:
|
||||||
|
// self related
|
||||||
|
Point m_position;
|
||||||
|
int m_duration;
|
||||||
|
ticks_t m_startTicks;
|
||||||
|
bool m_finished;
|
||||||
|
int m_particlesPerSecond, m_createdParticles;
|
||||||
|
std::list<Particle> m_particles;
|
||||||
|
|
||||||
|
// particles initial position related to emitter position
|
||||||
|
float positionMinRadius, positionMaxRadius;
|
||||||
|
float positionMinAngle, positionMaxAngle;
|
||||||
|
|
||||||
|
// particles initial velocity
|
||||||
|
float minVelocity, maxVelocity;
|
||||||
|
float minVelocityAngle, maxVelocityAngle;
|
||||||
|
|
||||||
|
// particles initial acceleration
|
||||||
|
float minAcceleration, maxAcceleration;
|
||||||
|
float minAccelerationAngle, maxAccelerationAngle;
|
||||||
|
|
||||||
|
// color ralated
|
||||||
|
Color color;
|
||||||
|
|
||||||
|
|
||||||
|
// texture related
|
||||||
|
};
|
||||||
|
|
||||||
|
class Affector {
|
||||||
|
public:
|
||||||
|
virtual void update() {};
|
||||||
|
};
|
||||||
|
|
||||||
|
class Gravity270Affector : public Affector {
|
||||||
|
public:
|
||||||
|
void update() {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class ParticlesSystem {
|
||||||
|
public:
|
||||||
|
void add(Emitter emitter);
|
||||||
|
|
||||||
|
void render();
|
||||||
|
void update();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::list<Emitter> m_emitters;
|
||||||
|
std::list<Affector> m_affectors;
|
||||||
|
};
|
Loading…
Reference in new issue