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/particlesystem_interactive.hpp"
18 #include "supertux/main.hpp"
20 #include "math/aatriangle.hpp"
21 #include "math/random_generator.hpp"
22 #include "object/camera.hpp"
23 #include "object/rainsplash.hpp"
24 #include "object/tilemap.hpp"
25 #include "supertux/collision.hpp"
26 #include "supertux/tile.hpp"
28 //TODO: Find a way to make rain collide with objects like bonus blocks
29 // Add an option to set rain strength
30 // Fix rain being "respawned" over solid tiles
31 ParticleSystem_Interactive::ParticleSystem_Interactive()
33 virtual_width = SCREEN_WIDTH;
34 virtual_height = SCREEN_HEIGHT;
38 ParticleSystem_Interactive::~ParticleSystem_Interactive()
40 std::vector<Particle*>::iterator i;
41 for(i = particles.begin(); i != particles.end(); ++i) {
46 void ParticleSystem_Interactive::draw(DrawingContext& context)
48 context.push_transform();
50 std::vector<Particle*>::iterator i;
51 for(i = particles.begin(); i != particles.end(); ++i) {
52 Particle* particle = *i;
53 context.draw_surface(particle->texture, particle->pos, z_pos);
56 context.pop_transform();
60 ParticleSystem_Interactive::collision(Particle* object, Vector movement)
62 using namespace collision;
64 // calculate rectangle where the object will move
68 x2 = x1 + 32 + movement.x;
70 y2 = y1 + 32 + movement.y;
73 // test with all tiles in this rectangle
74 int starttilex = int(x1-1) / 32;
75 int starttiley = int(y1-1) / 32;
76 int max_x = int(x2+1);
77 int max_y = int(y2+1);
79 Rect dest = Rect(x1, y1, x2, y2);
81 Constraints constraints;
83 for(std::list<TileMap*>::const_iterator i = Sector::current()->solid_tilemaps.begin(); i != Sector::current()->solid_tilemaps.end(); i++) {
85 for(int x = starttilex; x*32 < max_x; ++x) {
86 for(int y = starttiley; y*32 < max_y; ++y) {
87 const Tile* tile = solids->get_tile(x, y);
90 // skip non-solid tiles, except water
91 if(! (tile->getAttributes() & (Tile::WATER | Tile::SOLID)))
94 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
96 Vector p1(x*32, y*32);
97 Vector p2((x+1)*32, (y+1)*32);
98 triangle = AATriangle(p1, p2, tile->getData());
100 if(rectangle_aatriangle(&constraints, dest, triangle)) {
101 if(tile->getAttributes() & Tile::WATER)
104 } else { // normal rectangular tile
105 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
106 if(intersects(dest, rect)) {
107 if(tile->getAttributes() & Tile::WATER)
109 set_rectangle_rectangle_constraints(&constraints, dest, rect);
116 // TODO don't use magic numbers here...
118 // did we collide at all?
119 if(!constraints.has_constraints())
122 const CollisionHit& hit = constraints.hit;
124 return 0; //collision with water tile - don't draw splash
126 if (hit.right || hit.left) {
127 return 2; //collision from right
129 return 1; //collision from above
136 RainParticleSystem::RainParticleSystem()
138 rainimages[0] = new Surface("images/objects/particles/rain0.png");
139 rainimages[1] = new Surface("images/objects/particles/rain1.png");
141 virtual_width = SCREEN_WIDTH * 2;
143 // create some random raindrops
144 size_t raindropcount = size_t(virtual_width/6.0);
145 for(size_t i=0; i<raindropcount; ++i) {
146 RainParticle* particle = new RainParticle;
147 particle->pos.x = systemRandom.rand(int(virtual_width));
148 particle->pos.y = systemRandom.rand(int(virtual_height));
149 int rainsize = systemRandom.rand(2);
150 particle->texture = rainimages[rainsize];
152 particle->speed = (rainsize+1)*45 + systemRandom.randf(3.6);
153 } while(particle->speed < 1);
154 particle->speed *= 10; // gravity
156 particles.push_back(particle);
161 RainParticleSystem::parse(const Reader& reader)
163 reader.get("z-pos", z_pos);
166 RainParticleSystem::~RainParticleSystem()
169 delete rainimages[i];
172 void RainParticleSystem::update(float elapsed_time)
174 std::vector<Particle*>::iterator i;
176 i = particles.begin(); i != particles.end(); ++i) {
177 RainParticle* particle = (RainParticle*) *i;
178 float movement = particle->speed * elapsed_time;
179 float abs_x = Sector::current()->camera->get_translation().x;
180 float abs_y = Sector::current()->camera->get_translation().y;
181 particle->pos.y += movement;
182 particle->pos.x -= movement;
183 int col = collision(particle, Vector(-movement, movement));
184 if ((particle->pos.y > SCREEN_HEIGHT + abs_y) || (col >= 0)) {
186 if ((particle->pos.y <= SCREEN_HEIGHT + abs_y) && (col >= 1)){
187 bool vertical = (col == 2);
188 int splash_x, splash_y;
189 if (!vertical) { //check if collision happened from above
190 splash_x = int(particle->pos.x);
191 splash_y = int(particle->pos.y) - (int(particle->pos.y) % 32) + 32;
192 Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical));
194 // Uncomment the following to display vertical splashes, too
196 splash_x = int(particle->pos.x) - (int(particle->pos.x) % 32) + 32;
197 splash_y = int(particle->pos.y);
198 Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical));
201 int new_x = systemRandom.rand(int(virtual_width)) + int(abs_x);
203 //FIXME: Don't move particles over solid tiles
204 particle->pos.x = new_x;
205 particle->pos.y = new_y;
210 CometParticleSystem::CometParticleSystem()
212 cometimages[0] = new Surface("images/creatures/mr_bomb/exploding-left-0.png");
213 cometimages[1] = new Surface("images/creatures/mr_bomb/exploding-left-0.png");
215 virtual_width = SCREEN_WIDTH * 2;
217 // create some random comets
218 size_t cometcount = 2;
219 for(size_t i=0; i<cometcount; ++i) {
220 CometParticle* particle = new CometParticle;
221 particle->pos.x = systemRandom.rand(int(virtual_width));
222 particle->pos.y = systemRandom.rand(int(virtual_height));
223 int cometsize = systemRandom.rand(2);
224 particle->texture = cometimages[cometsize];
226 particle->speed = (cometsize+1)*30 + systemRandom.randf(3.6);
227 } while(particle->speed < 1);
228 particle->speed *= 10; // gravity
230 particles.push_back(particle);
235 CometParticleSystem::parse(const Reader& reader)
237 reader.get("z-pos", z_pos);
240 CometParticleSystem::~CometParticleSystem()
243 delete cometimages[i];
246 void CometParticleSystem::update(float elapsed_time)
250 std::vector<Particle*>::iterator i;
252 i = particles.begin(); i != particles.end(); ++i) {
253 CometParticle* particle = (CometParticle*) *i;
254 float movement = particle->speed * elapsed_time;
255 float abs_x = Sector::current()->camera->get_translation().x;
256 float abs_y = Sector::current()->camera->get_translation().y;
257 particle->pos.y += movement;
258 particle->pos.x -= movement;
259 int col = collision(particle, Vector(-movement, movement));
260 if ((particle->pos.y > SCREEN_HEIGHT + abs_y) || (col >= 0)) {
261 if ((particle->pos.y <= SCREEN_HEIGHT + abs_y) && (col >= 1)) {
262 Sector::current()->add_object(new Bomb(particle->pos, LEFT));
264 int new_x = systemRandom.rand(int(virtual_width)) + int(abs_x);
266 //FIXME: Don't move particles over solid tiles
267 particle->pos.x = new_x;
268 particle->pos.y = new_y;