restore trunk
[supertux.git] / src / object / explosion.cpp
1 //  $Id$
2 //
3 //  SuperTux -- Explosion object
4 //  Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include "explosion.hpp"
23 #include "badguy/badguy.hpp"
24 #include "object/sprite_particle.hpp"
25 #include "random_generator.hpp"
26
27 Explosion::Explosion(const Vector& pos)
28         : MovingSprite(pos, "images/objects/explosion/explosion.sprite", LAYER_OBJECTS+40, COLGROUP_TOUCHABLE), state(STATE_WAITING)
29 {
30   sound_manager->preload("sounds/explosion.wav");
31   set_pos(get_pos() - (get_bbox().get_middle() - get_pos()));
32 }
33
34 Explosion::Explosion(const lisp::Lisp& reader)
35         : MovingSprite(reader, "images/objects/explosion/explosion.sprite", LAYER_OBJECTS+40, COLGROUP_TOUCHABLE), state(STATE_WAITING)
36 {
37   sound_manager->preload("sounds/explosion.wav");
38 }
39
40 void
41 Explosion::explode()
42 {
43   if (state != STATE_WAITING) return;
44   state = STATE_EXPLODING;
45
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());
49
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));
61   }
62 }
63
64 void 
65 Explosion::update(float )
66 {
67   switch(state) {
68     case STATE_WAITING:
69       explode();
70       break;
71     case STATE_EXPLODING:
72       if(sprite->animation_done()) {
73         remove_me();
74       }
75       break;
76   }
77 }
78
79 HitResponse
80 Explosion::collision(GameObject& other, const CollisionHit& )
81 {
82   if(state != STATE_EXPLODING) return ABORT_MOVE;
83
84   Player* player = dynamic_cast<Player*>(&other);
85   if(player != 0) {
86     player->kill(false);
87   }
88
89   BadGuy* badguy = dynamic_cast<BadGuy*>(&other);
90   if(badguy != 0) {
91     badguy->kill_fall();
92   }
93
94   return ABORT_MOVE;
95 }
96
97 IMPLEMENT_FACTORY(Explosion, "explosion");
98