Split particlesystem_interactive.?pp into separate files
[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] = new Surface("images/objects/particles/rain0.png");
27   rainimages[1] = new Surface("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 = systemRandom.rand(int(virtual_width));
36     particle->pos.y = systemRandom.rand(int(virtual_height));
37     int rainsize = systemRandom.rand(2);
38     particle->texture = rainimages[rainsize];
39     do {
40       particle->speed = (rainsize+1)*45 + systemRandom.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   reader.get("z-pos", z_pos);
52 }
53
54 RainParticleSystem::~RainParticleSystem()
55 {
56   for(int i=0;i<2;++i)
57     delete rainimages[i];
58 }
59
60 void RainParticleSystem::update(float elapsed_time)
61 {
62   std::vector<Particle*>::iterator i;
63   for(
64     i = particles.begin(); i != particles.end(); ++i) {
65     RainParticle* particle = (RainParticle*) *i;
66     float movement = particle->speed * elapsed_time;
67     float abs_x = Sector::current()->camera->get_translation().x;
68     float abs_y = Sector::current()->camera->get_translation().y;
69     particle->pos.y += movement;
70     particle->pos.x -= movement;
71     int col = collision(particle, Vector(-movement, movement));
72     if ((particle->pos.y > SCREEN_HEIGHT + abs_y) || (col >= 0)) {
73       //Create rainsplash
74       if ((particle->pos.y <= SCREEN_HEIGHT + abs_y) && (col >= 1)){
75         bool vertical = (col == 2);
76         int splash_x, splash_y;
77         if (!vertical) { //check if collision happened from above
78           splash_x = int(particle->pos.x);
79           splash_y = int(particle->pos.y) - (int(particle->pos.y) % 32) + 32;
80           Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical));
81         }
82         // Uncomment the following to display vertical splashes, too
83         /* else {
84            splash_x = int(particle->pos.x) - (int(particle->pos.x) % 32) + 32;
85            splash_y = int(particle->pos.y);
86            Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical));
87            } */
88       }
89       int new_x = systemRandom.rand(int(virtual_width)) + int(abs_x);
90       int new_y = 0;
91       //FIXME: Don't move particles over solid tiles
92       particle->pos.x = new_x;
93       particle->pos.y = new_y;
94     }
95   }
96 }
97
98 /* EOF */