2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
17 #include "object/rain_particle_system.hpp"
19 #include "math/random_generator.hpp"
20 #include "object/camera.hpp"
21 #include "object/rainsplash.hpp"
22 #include "util/reader.hpp"
24 RainParticleSystem::RainParticleSystem()
26 rainimages[0] = Surface::create("images/objects/particles/rain0.png");
27 rainimages[1] = Surface::create("images/objects/particles/rain1.png");
29 virtual_width = SCREEN_WIDTH * 2;
31 // create some random raindrops
32 size_t raindropcount = size_t(virtual_width/6.0);
33 for(size_t i=0; i<raindropcount; ++i) {
34 RainParticle* particle = new RainParticle;
35 particle->pos.x = graphicsRandom.rand(int(virtual_width));
36 particle->pos.y = graphicsRandom.rand(int(virtual_height));
37 int rainsize = graphicsRandom.rand(2);
38 particle->texture = rainimages[rainsize];
40 particle->speed = (rainsize+1)*45 + graphicsRandom.randf(3.6);
41 } while(particle->speed < 1);
42 particle->speed *= 10; // gravity
44 particles.push_back(particle);
49 RainParticleSystem::parse(const Reader& reader)
51 z_pos = reader_get_layer (reader, /* default = */ LAYER_BACKGROUND1);
54 RainParticleSystem::~RainParticleSystem()
58 void RainParticleSystem::update(float elapsed_time)
60 std::vector<Particle*>::iterator i;
62 i = particles.begin(); i != particles.end(); ++i) {
63 RainParticle* particle = (RainParticle*) *i;
64 float movement = particle->speed * elapsed_time;
65 float abs_x = Sector::current()->camera->get_translation().x;
66 float abs_y = Sector::current()->camera->get_translation().y;
67 particle->pos.y += movement;
68 particle->pos.x -= movement;
69 int col = collision(particle, Vector(-movement, movement));
70 if ((particle->pos.y > SCREEN_HEIGHT + abs_y) || (col >= 0)) {
72 if ((particle->pos.y <= SCREEN_HEIGHT + abs_y) && (col >= 1)){
73 bool vertical = (col == 2);
74 int splash_x, splash_y;
75 if (!vertical) { //check if collision happened from above
76 splash_x = int(particle->pos.x);
77 splash_y = int(particle->pos.y) - (int(particle->pos.y) % 32) + 32;
78 Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical));
80 // Uncomment the following to display vertical splashes, too
82 splash_x = int(particle->pos.x) - (int(particle->pos.x) % 32) + 32;
83 splash_y = int(particle->pos.y);
84 Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical));
87 int new_x = graphicsRandom.rand(int(virtual_width)) + int(abs_x);
89 //FIXME: Don't move particles over solid tiles
90 particle->pos.x = new_x;
91 particle->pos.y = new_y;