Added new "Explosion" object
[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
26 #include "resources.hpp"
27 #include "video/drawing_context.hpp"
28 #include "sprite/sprite_manager.hpp"
29 #include "player.hpp"
30 #include "sector.hpp"
31 #include "player_status.hpp"
32 #include "gameobjs.hpp"
33 #include "statistics.hpp"
34 #include "object_factory.hpp"
35 #include "level.hpp"
36 #include "random_generator.hpp"
37 #include "audio/sound_source.hpp"
38 #include "audio/sound_manager.hpp"
39 #include "timer.hpp"
40
41 Explosion::Explosion(const Vector& pos)
42         : MovingSprite(pos, "images/objects/explosion/explosion.sprite", LAYER_BACKGROUNDTILES+10, COLGROUP_TOUCHABLE), state(STATE_WAITING)
43 {
44   sound_manager->preload("sounds/explosion.wav");
45   set_pos(get_pos() - (get_bbox().get_middle() - get_pos()));
46 }
47
48 Explosion::Explosion(const lisp::Lisp& reader)
49         : MovingSprite(reader, "images/objects/explosion/explosion.sprite", LAYER_BACKGROUNDTILES+10, COLGROUP_TOUCHABLE), state(STATE_WAITING)
50 {
51   sound_manager->preload("sounds/explosion.wav");
52 }
53
54 void
55 Explosion::explode()
56 {
57   if (state != STATE_WAITING) return;
58   state = STATE_EXPLODING;
59
60   set_action("default", 1);
61   sprite->set_animation_loops(1); //TODO: this is necessary because set_action will not set "loops" when "action" is the default action
62   sound_manager->play("sounds/explosion.wav", get_pos());
63
64   // spawn some particles
65   // TODO: provide convenience function in MovingSprite or MovingObject?
66   for (int i = 0; i < 100; i++) {
67     Vector ppos = bbox.get_middle();
68     float angle = systemRandom.randf(-M_PI_2, M_PI_2);
69     float velocity = systemRandom.randf(450, 900);
70     float vx = sin(angle)*velocity;
71     float vy = -cos(angle)*velocity;
72     Vector pspeed = Vector(vx, vy);
73     Vector paccel = Vector(0, 1000);
74     Sector::current()->add_object(new SpriteParticle("images/objects/particles/explosion.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
75   }
76 }
77
78 void 
79 Explosion::update(float )
80 {
81   switch(state) {
82     case STATE_WAITING:
83       explode();
84       break;
85     case STATE_EXPLODING:
86       if(sprite->animation_done()) {
87         remove_me();
88       }
89       break;
90   }
91 }
92
93 HitResponse
94 Explosion::collision(GameObject& other, const CollisionHit& )
95 {
96   if(state != STATE_EXPLODING) return ABORT_MOVE;
97
98   Player* player = dynamic_cast<Player*>(&other);
99   if(player != 0) {
100     player->kill(false);
101   }
102
103   BadGuy* badguy = dynamic_cast<BadGuy*>(&other);
104   if(badguy != 0) {
105     badguy->kill_fall();
106   }
107
108   return ABORT_MOVE;
109 }
110
111 IMPLEMENT_FACTORY(Explosion, "explosion");
112