src/object/rain_particle_system.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/rain_particle_system.hpp"
00018 
00019 #include "math/random_generator.hpp"
00020 #include "object/camera.hpp"
00021 #include "object/rainsplash.hpp"
00022 #include "util/reader.hpp"
00023 
00024 RainParticleSystem::RainParticleSystem()
00025 {
00026   rainimages[0] = Surface::create("images/objects/particles/rain0.png");
00027   rainimages[1] = Surface::create("images/objects/particles/rain1.png");
00028 
00029   virtual_width = SCREEN_WIDTH * 2;
00030 
00031   // create some random raindrops
00032   size_t raindropcount = size_t(virtual_width/6.0);
00033   for(size_t i=0; i<raindropcount; ++i) {
00034     RainParticle* particle = new RainParticle;
00035     particle->pos.x = graphicsRandom.rand(int(virtual_width));
00036     particle->pos.y = graphicsRandom.rand(int(virtual_height));
00037     int rainsize = graphicsRandom.rand(2);
00038     particle->texture = rainimages[rainsize];
00039     do {
00040       particle->speed = (rainsize+1)*45 + graphicsRandom.randf(3.6);
00041     } while(particle->speed < 1);
00042     particle->speed *= 10; // gravity
00043 
00044     particles.push_back(particle);
00045   }
00046 }
00047 
00048 void
00049 RainParticleSystem::parse(const Reader& reader)
00050 {
00051   z_pos = reader_get_layer (reader, /* default = */ LAYER_BACKGROUND1);
00052 }
00053 
00054 RainParticleSystem::~RainParticleSystem()
00055 {
00056 }
00057 
00058 void RainParticleSystem::update(float elapsed_time)
00059 {
00060   std::vector<Particle*>::iterator i;
00061   for(
00062     i = particles.begin(); i != particles.end(); ++i) {
00063     RainParticle* particle = (RainParticle*) *i;
00064     float movement = particle->speed * elapsed_time;
00065     float abs_x = Sector::current()->camera->get_translation().x;
00066     float abs_y = Sector::current()->camera->get_translation().y;
00067     particle->pos.y += movement;
00068     particle->pos.x -= movement;
00069     int col = collision(particle, Vector(-movement, movement));
00070     if ((particle->pos.y > SCREEN_HEIGHT + abs_y) || (col >= 0)) {
00071       //Create rainsplash
00072       if ((particle->pos.y <= SCREEN_HEIGHT + abs_y) && (col >= 1)){
00073         bool vertical = (col == 2);
00074         int splash_x, splash_y;
00075         if (!vertical) { //check if collision happened from above
00076           splash_x = int(particle->pos.x);
00077           splash_y = int(particle->pos.y) - (int(particle->pos.y) % 32) + 32;
00078           Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical));
00079         }
00080         // Uncomment the following to display vertical splashes, too
00081         /* else {
00082            splash_x = int(particle->pos.x) - (int(particle->pos.x) % 32) + 32;
00083            splash_y = int(particle->pos.y);
00084            Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical));
00085            } */
00086       }
00087       int new_x = graphicsRandom.rand(int(virtual_width)) + int(abs_x);
00088       int new_y = 0;
00089       //FIXME: Don't move particles over solid tiles
00090       particle->pos.x = new_x;
00091       particle->pos.y = new_y;
00092     }
00093   }
00094 }
00095 
00096 /* EOF */

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