00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "object/particles.hpp"
00018
00019 #include <math.h>
00020
00021 #include "math/random_generator.hpp"
00022 #include "object/camera.hpp"
00023 #include "supertux/globals.hpp"
00024 #include "supertux/sector.hpp"
00025 #include "video/drawing_context.hpp"
00026
00027 Particles::Particles(const Vector& epicenter, int min_angle, int max_angle,
00028 const Vector& initial_velocity, const Vector& acceleration, int number,
00029 Color color_, int size_, float life_time, int drawing_layer_) :
00030 accel(acceleration),
00031 timer(),
00032 live_forever(),
00033 color(color_),
00034 size(size_),
00035 drawing_layer(drawing_layer_),
00036 particles()
00037 {
00038 if(life_time == 0) {
00039 live_forever = true;
00040 } else {
00041 live_forever = false;
00042 timer.start(life_time);
00043 }
00044
00045
00046 for(int p = 0; p < number; p++)
00047 {
00048 Particle* particle = new Particle;
00049 particle->pos = epicenter;
00050
00051 float angle = graphicsRandom.rand(min_angle, max_angle)
00052 * (M_PI / 180);
00053 particle->vel.x = (sin(angle)) * initial_velocity.x;
00054
00055
00056 particle->vel.y = (cos(angle)) * initial_velocity.y;
00057
00058
00059
00060 particles.push_back(particle);
00061 }
00062 }
00063
00064 Particles::~Particles()
00065 {
00066
00067 for(std::vector<Particle*>::iterator i = particles.begin();
00068 i < particles.end(); i++)
00069 delete (*i);
00070 }
00071
00072 void
00073 Particles::update(float elapsed_time)
00074 {
00075 Vector camera = Sector::current()->camera->get_translation();
00076
00077
00078 for(std::vector<Particle*>::iterator i = particles.begin();
00079 i != particles.end(); ) {
00080 (*i)->pos.x += (*i)->vel.x * elapsed_time;
00081 (*i)->pos.y += (*i)->vel.y * elapsed_time;
00082
00083 (*i)->vel.x += accel.x * elapsed_time;
00084 (*i)->vel.y += accel.y * elapsed_time;
00085
00086 if((*i)->pos.x < camera.x || (*i)->pos.x > SCREEN_WIDTH + camera.x ||
00087 (*i)->pos.y < camera.y || (*i)->pos.y > SCREEN_HEIGHT + camera.y) {
00088 delete (*i);
00089 i = particles.erase(i);
00090 } else {
00091 ++i;
00092 }
00093 }
00094
00095 if((timer.check() && !live_forever) || particles.size() == 0)
00096 remove_me();
00097 }
00098
00099 void
00100 Particles::draw(DrawingContext& context)
00101 {
00102
00103 for(std::vector<Particle*>::iterator i = particles.begin();
00104 i != particles.end(); i++) {
00105 context.draw_filled_rect((*i)->pos, Vector(size,size), color,drawing_layer);
00106 }
00107 }
00108
00109