Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / object / explosion.cpp
1 //  SuperTux -- Explosion object
2 //  Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include "object/explosion.hpp"
18
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"
26
27 #include <math.h>
28
29 Explosion::Explosion(const Vector& pos)
30   : MovingSprite(pos, "images/objects/explosion/explosion.sprite", LAYER_OBJECTS+40, COLGROUP_TOUCHABLE), state(STATE_WAITING)
31 {
32   sound_manager->preload("sounds/explosion.wav");
33   set_pos(get_pos() - (get_bbox().get_middle() - get_pos()));
34 }
35
36 Explosion::Explosion(const Reader& reader)
37   : MovingSprite(reader, "images/objects/explosion/explosion.sprite", LAYER_OBJECTS+40, COLGROUP_TOUCHABLE), state(STATE_WAITING)
38 {
39   sound_manager->preload("sounds/explosion.wav");
40 }
41
42 void
43 Explosion::explode()
44 {
45   if (state != STATE_WAITING) return;
46   state = STATE_EXPLODING;
47
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());
51
52   if (0)
53   {
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));
65     }
66   }
67 }
68
69 void 
70 Explosion::update(float )
71 {
72   switch(state) {
73     case STATE_WAITING:
74       explode();
75       break;
76     case STATE_EXPLODING:
77       if(sprite->animation_done()) {
78         remove_me();
79       }
80       break;
81   }
82 }
83
84 HitResponse
85 Explosion::collision(GameObject& other, const CollisionHit& )
86 {
87   if(state != STATE_EXPLODING) return ABORT_MOVE;
88
89   Player* player = dynamic_cast<Player*>(&other);
90   if(player != 0) {
91     player->kill(false);
92   }
93
94   BadGuy* badguy = dynamic_cast<BadGuy*>(&other);
95   if(badguy != 0) {
96     badguy->kill_fall();
97   }
98
99   return ABORT_MOVE;
100 }
101
102 IMPLEMENT_FACTORY(Explosion, "explosion");
103
104 /* EOF */