2 // Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.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/comet_particle_system.hpp"
19 #include "math/random_generator.hpp"
20 #include "supertux/globals.hpp"
21 #include "video/surface.hpp"
22 #include "util/reader.hpp"
24 CometParticleSystem::CometParticleSystem()
26 cometimages[0] = Surface::create("images/creatures/mr_bomb/exploding-left-0.png");
27 cometimages[1] = Surface::create("images/creatures/mr_bomb/exploding-left-0.png");
29 virtual_width = SCREEN_WIDTH * 2;
31 // create some random comets
32 size_t cometcount = 2;
33 for(size_t i=0; i<cometcount; ++i) {
34 CometParticle* particle = new CometParticle;
35 particle->pos.x = systemRandom.rand(int(virtual_width));
36 particle->pos.y = systemRandom.rand(int(virtual_height));
37 int cometsize = systemRandom.rand(2);
38 particle->texture = cometimages[cometsize];
40 particle->speed = (cometsize+1)*30 + systemRandom.randf(3.6);
41 } while(particle->speed < 1);
42 particle->speed *= 10; // gravity
44 particles.push_back(particle);
49 CometParticleSystem::parse(const Reader& reader)
51 reader.get("z-pos", z_pos);
54 CometParticleSystem::~CometParticleSystem()
58 void CometParticleSystem::update(float elapsed_time)
62 std::vector<Particle*>::iterator i;
64 i = particles.begin(); i != particles.end(); ++i) {
65 CometParticle* particle = (CometParticle*) *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 if ((particle->pos.y <= SCREEN_HEIGHT + abs_y) && (col >= 1)) {
74 Sector::current()->add_object(new Bomb(particle->pos, LEFT));
76 int new_x = systemRandom.rand(int(virtual_width)) + int(abs_x);
78 //FIXME: Don't move particles over solid tiles
79 particle->pos.x = new_x;
80 particle->pos.y = new_y;