3 // SuperTux -- Explosion object
4 // Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "explosion.hpp"
23 #include "badguy/badguy.hpp"
24 #include "object/sprite_particle.hpp"
25 #include "random_generator.hpp"
27 Explosion::Explosion(const Vector& pos)
28 : MovingSprite(pos, "images/objects/explosion/explosion.sprite", LAYER_OBJECTS+40, COLGROUP_TOUCHABLE), state(STATE_WAITING)
30 sound_manager->preload("sounds/explosion.wav");
31 set_pos(get_pos() - (get_bbox().get_middle() - get_pos()));
34 Explosion::Explosion(const lisp::Lisp& reader)
35 : MovingSprite(reader, "images/objects/explosion/explosion.sprite", LAYER_OBJECTS+40, COLGROUP_TOUCHABLE), state(STATE_WAITING)
37 sound_manager->preload("sounds/explosion.wav");
43 if (state != STATE_WAITING) return;
44 state = STATE_EXPLODING;
46 set_action("default", 1);
47 sprite->set_animation_loops(1); //TODO: this is necessary because set_action will not set "loops" when "action" is the default action
48 sound_manager->play("sounds/explosion.wav", get_pos());
50 // spawn some particles
51 // TODO: provide convenience function in MovingSprite or MovingObject?
52 for (int i = 0; i < 100; i++) {
53 Vector ppos = bbox.get_middle();
54 float angle = systemRandom.randf(-M_PI_2, M_PI_2);
55 float velocity = systemRandom.randf(450, 900);
56 float vx = sin(angle)*velocity;
57 float vy = -cos(angle)*velocity;
58 Vector pspeed = Vector(vx, vy);
59 Vector paccel = Vector(0, 1000);
60 Sector::current()->add_object(new SpriteParticle("images/objects/particles/explosion.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
65 Explosion::update(float )
72 if(sprite->animation_done()) {
80 Explosion::collision(GameObject& other, const CollisionHit& )
82 if(state != STATE_EXPLODING) return ABORT_MOVE;
84 Player* player = dynamic_cast<Player*>(&other);
89 BadGuy* badguy = dynamic_cast<BadGuy*>(&other);
97 IMPLEMENT_FACTORY(Explosion, "explosion");