[cppcheck] Part 2: Some further style fixes etc.
[supertux.git] / src / object / rain_particle_system.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/rain_particle_system.hpp"
18
19 #include "math/random_generator.hpp"
20 #include "object/camera.hpp"
21 #include "object/rainsplash.hpp"
22 #include "util/reader.hpp"
23
24 RainParticleSystem::RainParticleSystem()
25 {
26   rainimages[0] = Surface::create("images/objects/particles/rain0.png");
27   rainimages[1] = Surface::create("images/objects/particles/rain1.png");
28
29   virtual_width = SCREEN_WIDTH * 2;
30
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];
39     do {
40       particle->speed = (rainsize+1)*45 + graphicsRandom.randf(3.6);
41     } while(particle->speed < 1);
42     particle->speed *= 10; // gravity
43
44     particles.push_back(particle);
45   }
46 }
47
48 void
49 RainParticleSystem::parse(const Reader& reader)
50 {
51   z_pos = reader_get_layer (reader, /* default = */ LAYER_BACKGROUND1);
52 }
53
54 RainParticleSystem::~RainParticleSystem()
55 {
56 }
57
58 void RainParticleSystem::update(float elapsed_time)
59 {
60   std::vector<Particle*>::iterator i;
61   for(
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)) {
71       //Create rainsplash
72       if ((particle->pos.y <= SCREEN_HEIGHT + abs_y) && (col >= 1)){
73         bool vertical = (col == 2);
74         if (!vertical) { //check if collision happened from above
75           int splash_x, splash_y; // move outside if statement when
76                                   // uncommenting the else statement below.
77           splash_x = int(particle->pos.x);
78           splash_y = int(particle->pos.y) - (int(particle->pos.y) % 32) + 32;
79           Sector::current()->add_object(std::make_shared<RainSplash>(Vector(splash_x, splash_y),vertical));
80         }
81         // Uncomment the following to display vertical splashes, too
82         /* else {
83            splash_x = int(particle->pos.x) - (int(particle->pos.x) % 32) + 32;
84            splash_y = int(particle->pos.y);
85            Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical));
86            } */
87       }
88       int new_x = graphicsRandom.rand(int(virtual_width)) + int(abs_x);
89       int new_y = 0;
90       //FIXME: Don't move particles over solid tiles
91       particle->pos.x = new_x;
92       particle->pos.y = new_y;
93     }
94   }
95 }
96
97 /* EOF */