4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "particles.hpp"
28 #include "random_generator.hpp"
30 Particles::Particles(const Vector& epicenter, int min_angle, int max_angle,
31 const Vector& initial_velocity, const Vector& acceleration, int number,
32 Color color_, int size_, float life_time, int drawing_layer_)
33 : accel(acceleration), color(color_), size(size_), drawing_layer(drawing_layer_)
39 timer.start(life_time);
43 for(int p = 0; p < number; p++)
45 Particle* particle = new Particle;
46 particle->pos = epicenter;
48 float angle = systemRandom.rand(min_angle, max_angle)
49 * (M_PI / 180); // convert to radius (radians?)
50 particle->vel.x = /*fabs*/(sin(angle)) * initial_velocity.x;
51 // if(angle >= M_PI && angle < M_PI*2)
52 // particle->vel.x *= -1; // work around to fix signal
53 particle->vel.y = /*fabs*/(cos(angle)) * initial_velocity.y;
54 // if(angle >= M_PI_2 && angle < 3*M_PI_2)
55 // particle->vel.y *= -1;
57 particles.push_back(particle);
61 Particles::~Particles()
64 for(std::vector<Particle*>::iterator i = particles.begin();
65 i < particles.end(); i++)
70 Particles::update(float elapsed_time)
72 Vector camera = Sector::current()->camera->get_translation();
75 for(std::vector<Particle*>::iterator i = particles.begin();
76 i != particles.end(); ) {
77 (*i)->pos.x += (*i)->vel.x * elapsed_time;
78 (*i)->pos.y += (*i)->vel.y * elapsed_time;
80 (*i)->vel.x += accel.x * elapsed_time;
81 (*i)->vel.y += accel.y * elapsed_time;
83 if((*i)->pos.x < camera.x || (*i)->pos.x > SCREEN_WIDTH + camera.x ||
84 (*i)->pos.y < camera.y || (*i)->pos.y > SCREEN_HEIGHT + camera.y) {
86 i = particles.erase(i);
92 if((timer.check() && !live_forever) || particles.size() == 0)
97 Particles::draw(DrawingContext& context)
100 for(std::vector<Particle*>::iterator i = particles.begin();
101 i != particles.end(); i++) {
102 context.draw_filled_rect((*i)->pos, Vector(size,size), color,drawing_layer);