src/object/particles.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "object/particles.hpp"
00018 
00019 #include <math.h>
00020 
00021 #include "math/random_generator.hpp"
00022 #include "object/camera.hpp"
00023 #include "supertux/globals.hpp"
00024 #include "supertux/sector.hpp"
00025 #include "video/drawing_context.hpp"
00026 
00027 Particles::Particles(const Vector& epicenter, int min_angle, int max_angle,
00028                      const Vector& initial_velocity, const Vector& acceleration, int number,
00029                      Color color_, int size_, float life_time, int drawing_layer_) :
00030   accel(acceleration), 
00031   timer(),
00032   live_forever(),
00033   color(color_), 
00034   size(size_), 
00035   drawing_layer(drawing_layer_),
00036   particles()
00037 {
00038   if(life_time == 0) {
00039     live_forever = true;
00040   } else {
00041     live_forever = false;
00042     timer.start(life_time);
00043   }
00044 
00045   // create particles
00046   for(int p = 0; p < number; p++)
00047   {
00048     Particle* particle = new Particle;
00049     particle->pos = epicenter;
00050 
00051     float angle = graphicsRandom.rand(min_angle, max_angle)
00052       * (M_PI / 180);  // convert to radius (radians?)
00053     particle->vel.x = /*fabs*/(sin(angle)) * initial_velocity.x;
00054     //    if(angle >= M_PI && angle < M_PI*2)
00055     //      particle->vel.x *= -1;  // work around to fix signal
00056     particle->vel.y = /*fabs*/(cos(angle)) * initial_velocity.y;
00057     //    if(angle >= M_PI_2 && angle < 3*M_PI_2)
00058     //      particle->vel.y *= -1;
00059 
00060     particles.push_back(particle);
00061   }
00062 }
00063 
00064 Particles::~Particles()
00065 {
00066   // free particles
00067   for(std::vector<Particle*>::iterator i = particles.begin();
00068       i < particles.end(); i++)
00069     delete (*i);
00070 }
00071 
00072 void
00073 Particles::update(float elapsed_time)
00074 {
00075   Vector camera = Sector::current()->camera->get_translation();
00076 
00077   // update particles
00078   for(std::vector<Particle*>::iterator i = particles.begin();
00079       i != particles.end(); ) {
00080     (*i)->pos.x += (*i)->vel.x * elapsed_time;
00081     (*i)->pos.y += (*i)->vel.y * elapsed_time;
00082 
00083     (*i)->vel.x += accel.x * elapsed_time;
00084     (*i)->vel.y += accel.y * elapsed_time;
00085 
00086     if((*i)->pos.x < camera.x || (*i)->pos.x > SCREEN_WIDTH + camera.x ||
00087        (*i)->pos.y < camera.y || (*i)->pos.y > SCREEN_HEIGHT + camera.y) {
00088       delete (*i);
00089       i = particles.erase(i);
00090     } else {
00091       ++i;
00092     }
00093   }
00094 
00095   if((timer.check() && !live_forever) || particles.size() == 0)
00096     remove_me();
00097 }
00098 
00099 void
00100 Particles::draw(DrawingContext& context)
00101 {
00102   // draw particles
00103   for(std::vector<Particle*>::iterator i = particles.begin();
00104       i != particles.end(); i++) {
00105     context.draw_filled_rect((*i)->pos, Vector(size,size), color,drawing_layer);
00106   }
00107 }
00108 
00109 /* EOF */

Generated on Mon Jun 9 03:38:19 2014 for SuperTux by  doxygen 1.5.1