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