424c4f2faf9b91a3045b0d4535afcb29857ded9f
[supertux.git] / src / object / particles.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "object/particles.hpp"
18
19 #include <math.h>
20
21 #include "math/random_generator.hpp"
22 #include "object/camera.hpp"
23 #include "supertux/globals.hpp"
24 #include "supertux/sector.hpp"
25 #include "video/drawing_context.hpp"
26
27 Particles::Particles(const Vector& epicenter, int min_angle, int max_angle,
28                      const Vector& initial_velocity, const Vector& acceleration, int number,
29                      Color color_, int size_, float life_time, int drawing_layer_) :
30   accel(acceleration),
31   timer(),
32   live_forever(),
33   color(color_),
34   size(size_),
35   drawing_layer(drawing_layer_),
36   particles()
37 {
38   if(life_time == 0) {
39     live_forever = true;
40   } else {
41     live_forever = false;
42     timer.start(life_time);
43   }
44
45   // create particles
46   for(int p = 0; p < number; p++)
47   {
48     Particle* particle = new Particle;
49     particle->pos = epicenter;
50
51     float angle = graphicsRandom.rand(min_angle, max_angle)
52       * (M_PI / 180);  // convert to radius (radians?)
53     particle->vel.x = /*fabs*/(sin(angle)) * initial_velocity.x;
54     //    if(angle >= M_PI && angle < M_PI*2)
55     //      particle->vel.x *= -1;  // work around to fix signal
56     particle->vel.y = /*fabs*/(cos(angle)) * initial_velocity.y;
57     //    if(angle >= M_PI_2 && angle < 3*M_PI_2)
58     //      particle->vel.y *= -1;
59
60     particles.push_back(particle);
61   }
62 }
63
64 Particles::~Particles()
65 {
66   // free particles
67   for(std::vector<Particle*>::iterator i = particles.begin();
68       i < particles.end(); i++)
69     delete (*i);
70 }
71
72 void
73 Particles::update(float elapsed_time)
74 {
75   Vector camera = Sector::current()->camera->get_translation();
76
77   // update particles
78   for(std::vector<Particle*>::iterator i = particles.begin();
79       i != particles.end(); ) {
80     (*i)->pos.x += (*i)->vel.x * elapsed_time;
81     (*i)->pos.y += (*i)->vel.y * elapsed_time;
82
83     (*i)->vel.x += accel.x * elapsed_time;
84     (*i)->vel.y += accel.y * elapsed_time;
85
86     if((*i)->pos.x < camera.x || (*i)->pos.x > SCREEN_WIDTH + camera.x ||
87        (*i)->pos.y < camera.y || (*i)->pos.y > SCREEN_HEIGHT + camera.y) {
88       delete (*i);
89       i = particles.erase(i);
90     } else {
91       ++i;
92     }
93   }
94
95   if((timer.check() && !live_forever) || particles.size() == 0)
96     remove_me();
97 }
98
99 void
100 Particles::draw(DrawingContext& context)
101 {
102   // draw particles
103   for(std::vector<Particle*>::iterator i = particles.begin();
104       i != particles.end(); i++) {
105     context.draw_filled_rect((*i)->pos, Vector(size,size), color,drawing_layer);
106   }
107 }
108
109 /* EOF */