cea6c82f8a60db6ad50110e2da6bcdb2899d3e9e
[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 #include "random_generator.hpp"
29
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_)
34 {
35   if(life_time == 0) {
36     live_forever = true;
37   } else {
38     live_forever = false;
39     timer.start(life_time);
40   }
41
42   // create particles
43   for(int p = 0; p < number; p++)
44     {
45     Particle* particle = new Particle;
46     particle->pos = epicenter;
47
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;
56
57     particles.push_back(particle);
58     }
59 }
60
61 Particles::~Particles()
62 {
63   // free particles
64   for(std::vector<Particle*>::iterator i = particles.begin();
65       i < particles.end(); i++)
66     delete (*i);
67 }
68
69 void
70 Particles::update(float elapsed_time)
71 {
72   Vector camera = Sector::current()->camera->get_translation();
73
74   // update particles
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;
79
80     (*i)->vel.x += accel.x * elapsed_time;
81     (*i)->vel.y += accel.y * elapsed_time;
82
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) {
85       delete (*i);
86       i = particles.erase(i);
87     } else {
88       ++i;
89     }
90   }
91
92   if((timer.check() && !live_forever) || particles.size() == 0)
93     remove_me();
94 }
95
96 void
97 Particles::draw(DrawingContext& context)
98 {
99   // draw particles
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);
103   }
104 }
105