1 // SuperTux -- Explosion object
2 // Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.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/explosion.hpp"
19 #include "audio/sound_manager.hpp"
20 #include "badguy/badguy.hpp"
21 #include "math/random_generator.hpp"
22 #include "object/player.hpp"
23 #include "object/sprite_particle.hpp"
24 #include "supertux/object_factory.hpp"
25 #include "supertux/sector.hpp"
29 Explosion::Explosion(const Vector& pos)
30 : MovingSprite(pos, "images/objects/explosion/explosion.sprite", LAYER_OBJECTS+40, COLGROUP_TOUCHABLE), state(STATE_WAITING)
32 sound_manager->preload("sounds/explosion.wav");
33 set_pos(get_pos() - (get_bbox().get_middle() - get_pos()));
36 Explosion::Explosion(const Reader& reader)
37 : MovingSprite(reader, "images/objects/explosion/explosion.sprite", LAYER_OBJECTS+40, COLGROUP_TOUCHABLE), state(STATE_WAITING)
39 sound_manager->preload("sounds/explosion.wav");
45 if (state != STATE_WAITING) return;
46 state = STATE_EXPLODING;
48 set_action("default", 1);
49 sprite->set_animation_loops(1); //TODO: this is necessary because set_action will not set "loops" when "action" is the default action
50 sound_manager->play("sounds/explosion.wav", get_pos());
54 // spawn some particles
55 // TODO: provide convenience function in MovingSprite or MovingObject?
56 for (int i = 0; i < 100; i++) {
57 Vector ppos = bbox.get_middle();
58 float angle = systemRandom.randf(-M_PI_2, M_PI_2);
59 float velocity = systemRandom.randf(450, 900);
60 float vx = sin(angle)*velocity;
61 float vy = -cos(angle)*velocity;
62 Vector pspeed = Vector(vx, vy);
63 Vector paccel = Vector(0, 1000);
64 Sector::current()->add_object(new SpriteParticle("images/objects/particles/explosion.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
70 Explosion::update(float )
77 if(sprite->animation_done()) {
85 Explosion::collision(GameObject& other, const CollisionHit& )
87 if(state != STATE_EXPLODING) return ABORT_MOVE;
89 Player* player = dynamic_cast<Player*>(&other);
94 BadGuy* badguy = dynamic_cast<BadGuy*>(&other);