Disabled bomb spraying particles all over the screen
[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   if (0)
51     {
52       // spawn some particles
53       // TODO: provide convenience function in MovingSprite or MovingObject?
54       for (int i = 0; i < 100; i++) {
55         Vector ppos = bbox.get_middle();
56         float angle = systemRandom.randf(-M_PI_2, M_PI_2);
57         float velocity = systemRandom.randf(450, 900);
58         float vx = sin(angle)*velocity;
59         float vy = -cos(angle)*velocity;
60         Vector pspeed = Vector(vx, vy);
61         Vector paccel = Vector(0, 1000);
62         Sector::current()->add_object(new SpriteParticle("images/objects/particles/explosion.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
63       }
64     }
65 }
66
67 void 
68 Explosion::update(float )
69 {
70   switch(state) {
71     case STATE_WAITING:
72       explode();
73       break;
74     case STATE_EXPLODING:
75       if(sprite->animation_done()) {
76         remove_me();
77       }
78       break;
79   }
80 }
81
82 HitResponse
83 Explosion::collision(GameObject& other, const CollisionHit& )
84 {
85   if(state != STATE_EXPLODING) return ABORT_MOVE;
86
87   Player* player = dynamic_cast<Player*>(&other);
88   if(player != 0) {
89     player->kill(false);
90   }
91
92   BadGuy* badguy = dynamic_cast<BadGuy*>(&other);
93   if(badguy != 0) {
94     badguy->kill_fall();
95   }
96
97   return ABORT_MOVE;
98 }
99
100 IMPLEMENT_FACTORY(Explosion, "explosion");
101