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"
29 Particles::Particles(const Vector& epicenter, int min_angle, int max_angle,
30 const Vector& initial_velocity, const Vector& acceleration, int number,
31 Color color_, int size_, float life_time, int drawing_layer_)
32 : accel(acceleration), color(color_), size(size_), drawing_layer(drawing_layer_)
38 timer.start(life_time);
42 for(int p = 0; p < number; p++)
44 Particle* particle = new Particle;
45 particle->pos = epicenter;
47 float angle = ((rand() % (max_angle-min_angle))+min_angle)
48 * (M_PI / 180); // convert to radius
49 particle->vel.x = /*fabs*/(sin(angle)) * initial_velocity.x;
50 // if(angle >= M_PI && angle < M_PI*2)
51 // particle->vel.x *= -1; // work around to fix signal
52 particle->vel.y = /*fabs*/(cos(angle)) * initial_velocity.y;
53 // if(angle >= M_PI_2 && angle < 3*M_PI_2)
54 // particle->vel.y *= -1;
56 particles.push_back(particle);
60 Particles::~Particles()
63 for(std::vector<Particle*>::iterator i = particles.begin();
64 i < particles.end(); i++)
69 Particles::update(float elapsed_time)
71 Vector camera = Sector::current()->camera->get_translation();
74 for(std::vector<Particle*>::iterator i = particles.begin();
75 i != particles.end(); ) {
76 (*i)->pos.x += (*i)->vel.x * elapsed_time;
77 (*i)->pos.y += (*i)->vel.y * elapsed_time;
79 (*i)->vel.x += accel.x * elapsed_time;
80 (*i)->vel.y += accel.y * elapsed_time;
82 if((*i)->pos.x < camera.x || (*i)->pos.x > SCREEN_WIDTH + camera.x ||
83 (*i)->pos.y < camera.y || (*i)->pos.y > SCREEN_HEIGHT + camera.y) {
85 i = particles.erase(i);
91 if((timer.check() && !live_forever) || particles.size() == 0)
96 Particles::draw(DrawingContext& context)
99 for(std::vector<Particle*>::iterator i = particles.begin();
100 i != particles.end(); i++) {
101 context.draw_filled_rect((*i)->pos, Vector(size,size), color,drawing_layer);