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 <assert.h> |
20 | |
21 | #include "math/random.hpp" |
22 | #include "object/camera.hpp" |
23 | #include "object/rainsplash.hpp" |
24 | #include "supertux/sector.hpp" |
25 | #include "video/surface.hpp" |
26 | #include "video/video_system.hpp" |
27 | #include "video/viewport.hpp" |
28 | |
29 | RainParticleSystem::RainParticleSystem() |
30 | { |
31 | init(); |
32 | } |
33 | |
34 | RainParticleSystem::RainParticleSystem(const ReaderMapping& reader) : |
35 | ParticleSystem_Interactive(reader) |
36 | { |
37 | init(); |
38 | } |
39 | |
40 | RainParticleSystem::~RainParticleSystem() |
41 | { |
42 | } |
43 | |
44 | void RainParticleSystem::init() |
45 | { |
46 | rainimages[0] = Surface::from_file("images/objects/particles/rain0.png" ); |
47 | rainimages[1] = Surface::from_file("images/objects/particles/rain1.png" ); |
48 | |
49 | virtual_width = static_cast<float>(SCREEN_WIDTH) * 2.0f; |
50 | |
51 | // create some random raindrops |
52 | size_t raindropcount = size_t(virtual_width/6.0f); |
53 | for (size_t i=0; i<raindropcount; ++i) { |
54 | auto particle = std::make_unique<RainParticle>(); |
55 | particle->pos.x = static_cast<float>(graphicsRandom.rand(int(virtual_width))); |
56 | particle->pos.y = static_cast<float>(graphicsRandom.rand(int(virtual_height))); |
57 | int rainsize = graphicsRandom.rand(2); |
58 | particle->texture = rainimages[rainsize]; |
59 | do { |
60 | particle->speed = (static_cast<float>(rainsize) + 1.0f) * 45.0f + graphicsRandom.randf(3.6f); |
61 | } while(particle->speed < 1); |
62 | |
63 | particles.push_back(std::move(particle)); |
64 | } |
65 | } |
66 | |
67 | void RainParticleSystem::update(float dt_sec) |
68 | { |
69 | if (!enabled) |
70 | return; |
71 | |
72 | for (auto& it : particles) { |
73 | auto particle = dynamic_cast<RainParticle*>(it.get()); |
74 | assert(particle); |
75 | |
76 | float movement = particle->speed * dt_sec * Sector::get().get_gravity(); |
77 | float abs_x = Sector::get().get_camera().get_translation().x; |
78 | float abs_y = Sector::get().get_camera().get_translation().y; |
79 | particle->pos.y += movement; |
80 | particle->pos.x -= movement; |
81 | int col = collision(particle, Vector(-movement, movement)); |
82 | if ((particle->pos.y > static_cast<float>(SCREEN_HEIGHT) + abs_y) || (col >= 0)) { |
83 | //Create rainsplash |
84 | if ((particle->pos.y <= static_cast<float>(SCREEN_HEIGHT) + abs_y) && (col >= 1)){ |
85 | bool vertical = (col == 2); |
86 | if (!vertical) { //check if collision happened from above |
87 | int splash_x, splash_y; // move outside if statement when |
88 | // uncommenting the else statement below. |
89 | splash_x = int(particle->pos.x); |
90 | splash_y = int(particle->pos.y) - (int(particle->pos.y) % 32) + 32; |
91 | Sector::get().add<RainSplash>(Vector(static_cast<float>(splash_x), static_cast<float>(splash_y)), |
92 | vertical); |
93 | } |
94 | // Uncomment the following to display vertical splashes, too |
95 | /* else { |
96 | splash_x = int(particle->pos.x) - (int(particle->pos.x) % 32) + 32; |
97 | splash_y = int(particle->pos.y); |
98 | Sector::get().add<RainSplash>(Vector(splash_x, splash_y),vertical); |
99 | } */ |
100 | } |
101 | int new_x = graphicsRandom.rand(int(virtual_width)) + int(abs_x); |
102 | int new_y = 0; |
103 | //FIXME: Don't move particles over solid tiles |
104 | particle->pos.x = static_cast<float>(new_x); |
105 | particle->pos.y = static_cast<float>(new_y); |
106 | } |
107 | } |
108 | } |
109 | |
110 | /* EOF */ |
111 | |