Massive copyright update. I'm sorry if I'm crediting Matze for something he didn...
[supertux.git] / src / object / particles.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include <math.h>
23
24 #include "particles.hpp"
25 #include "sector.hpp"
26 #include "camera.hpp"
27 #include "main.hpp"
28
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_)
33 {
34   if(life_time == 0) {
35     live_forever = true;
36   } else {
37     live_forever = false;
38     timer.start(life_time);
39   }
40
41   // create particles
42   for(int p = 0; p < number; p++)
43     {
44     Particle* particle = new Particle;
45     particle->pos = epicenter;
46
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;
55
56     particles.push_back(particle);
57     }
58 }
59
60 Particles::~Particles()
61 {
62   // free particles
63   for(std::vector<Particle*>::iterator i = particles.begin();
64       i < particles.end(); i++)
65     delete (*i);
66 }
67
68 void
69 Particles::update(float elapsed_time)
70 {
71   Vector camera = Sector::current()->camera->get_translation();
72
73   // update particles
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;
78
79     (*i)->vel.x += accel.x * elapsed_time;
80     (*i)->vel.y += accel.y * elapsed_time;
81
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) {
84       delete (*i);
85       i = particles.erase(i);
86     } else {
87       ++i;
88     }
89   }
90
91   if((timer.check() && !live_forever) || particles.size() == 0)
92     remove_me();
93 }
94
95 void
96 Particles::draw(DrawingContext& context)
97 {
98   // draw particles
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);
102   }
103 }
104