This particle system is just a rip off of one that I did for a game of mine.
I guess that with more customization it would be pretty good.
SVN-Revision: 1903
#include "resources.h"
#include "sector.h"
#include "tilemap.h"
+#include "video/drawing_context.h"
BouncyDistro::BouncyDistro(const Vector& pos)
: position(pos)
img_smoke_cloud->draw(context, position, LAYER_OBJECTS+1);
}
+Particles::Particles(const Vector& epicenter, int number, Color color_, int size_, float velocity_, int life_time)
+ : color(color_), size(size_), velocity(velocity_)
+{
+ timer.start(life_time);
+
+ // create particles
+ for(int p = 0; p < number; p++)
+ {
+ Particle* particle = new Particle;
+ particle->pos = epicenter;
+ particle->angle = (rand() % 360) * (M_PI / 180); // in radius
+
+ particles.push_back(particle);
+ }
+}
+
+Particles::~Particles()
+{
+ // free particles
+ for(std::vector<Particle*>::iterator i = particles.begin(); i < particles.end(); i++)
+ delete (*i);
+}
+
+void
+Particles::action(float elapsed_time)
+{
+ // update particles
+ for(int p = 0; p < particles.size(); p++)
+ {
+ particles[p]->pos.x += sin(particles[p]->angle) * velocity * elapsed_time;
+ particles[p]->pos.y += cos(particles[p]->angle) * velocity * elapsed_time;
+ }
+
+ if(!timer.check())
+ remove_me();
+}
+
+void
+Particles::draw(DrawingContext& context)
+{
+ // draw particles
+ for(int p = 0; p < particles.size(); p++)
+ {
+ context.draw_filled_rect(particles[p]->pos, Vector(size,size), color, LAYER_OBJECTS+10);
+ }
+}
+
void load_object_gfx()
{
img_trampoline = sprite_manager->load("trampoline");
Vector position;
};
+class Particles : public GameObject
+{
+public:
+ Particles(const Vector& epicenter, int number, Color color, int size, float velocity, int life_time);
+ ~Particles();
+
+ virtual void action(float elapsed_time);
+ virtual void draw(DrawingContext& context);
+
+private:
+ Color color;
+ float size;
+ float velocity;
+ Timer timer;
+
+ struct Particle {
+ Vector pos;
+ float angle;
+ };
+ std::vector <Particle*> particles;
+};
+
void load_object_gfx();
#endif
std::remove(smoke_clouds.begin(), smoke_clouds.end(), smoke_cloud),
smoke_clouds.end());
}
-
+ Particles* particle = dynamic_cast<Particles*> (*i);
+ if(particle) {
+ particles.erase(
+ std::remove(particles.begin(), particles.end(), particle),
+ particles.end());
+ }
+
delete *i;
i = gameobjects.erase(i);
} else {
SmokeCloud* smoke_cloud = dynamic_cast<SmokeCloud*> (*i);
if(smoke_cloud)
smoke_clouds.push_back(smoke_cloud);
-
+ Particles* particle = dynamic_cast<Particles*> (*i);
+ if(particle)
+ particles.push_back(particle);
gameobjects.push_back(*i);
}
return true;
}
+bool
+Sector::add_particles(const Vector& epicenter, int number, Color color, int size, float velocity, int life_time)
+{
+ add_object(new Particles(epicenter, number, color, size, velocity, life_time));
+ return true;
+}
+
/* Break a brick: */
bool
Sector::trybreakbrick(const Vector& pos, bool small)
class Upgrade;
class Bullet;
class SmokeCloud;
+class Particles;
class BadGuy;
class Tile;
void add_upgrade(const Vector& pos, Direction dir, UpgradeKind kind);
bool add_bullet(const Vector& pos, float xm, Direction dir);
bool add_smoke_cloud(const Vector& pos);
+ bool add_particles(const Vector& epicenter, int number, Color color, int size, float velocity, int life_time);
/** Try to grab the coin at the given coordinates */
void trygrabdistro(const Vector& pos, int bounciness);
std::vector<Upgrade*> upgrades;
std::vector<Bullet*> bullets;
std::vector<SmokeCloud*> smoke_clouds;
+ std::vector<Particles*> particles;
public: // ugly
typedef std::vector<InteractiveObject*> InteractiveObjects;