38ec13b42cd2c03cc3ccab8131df7bf0f4586579
[supertux.git] / src / badguy / bomb.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.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 "bomb.hpp"
23 #include "random_generator.hpp"
24 #include "object/sprite_particle.hpp"
25 #include "object/explosion.hpp"
26
27 Bomb::Bomb(const Vector& pos, Direction dir, std::string custom_sprite /*= "images/creatures/mr_bomb/mr_bomb.sprite"*/ )
28         : BadGuy( pos, dir, custom_sprite )
29 {
30   state = STATE_TICKING;
31   set_action(dir == LEFT ? "ticking-left" : "ticking-right", 1);
32   countMe = false;
33
34   ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav"));
35   ticking->set_position(get_pos());
36   ticking->set_looping(true);
37   ticking->set_gain(2.0);
38   ticking->set_reference_distance(32);
39   ticking->play();
40 }
41
42 Bomb::Bomb(const Bomb& other)
43         : BadGuy(other), state(other.state)
44 {
45   if (state == STATE_TICKING) {
46     ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav"));
47     ticking->set_position(get_pos());
48     ticking->set_looping(true);
49     ticking->set_gain(2.0);
50     ticking->set_reference_distance(32);
51     ticking->play();
52   }
53 }
54
55 void
56 Bomb::write(lisp::Writer& )
57 {
58   // bombs are only temporarily so don't write them out...
59 }
60
61 void
62 Bomb::collision_solid(const CollisionHit& hit)
63 {
64   if(hit.bottom)
65     physic.set_velocity_y(0);
66 }
67
68 HitResponse
69 Bomb::collision_player(Player& player, const CollisionHit& )
70 {
71   if(state == STATE_EXPLODING) {
72     player.kill(false);
73   }
74   return ABORT_MOVE;
75 }
76
77 HitResponse
78 Bomb::collision_badguy(BadGuy& badguy, const CollisionHit& )
79 {
80   if(state == STATE_EXPLODING)
81     badguy.kill_fall();
82   return ABORT_MOVE;
83 }
84
85 void
86 Bomb::active_update(float )
87 {
88   switch(state) {
89     case STATE_TICKING:
90       ticking->set_position(get_pos());
91       if(sprite->animation_done()) {
92         explode();
93       }
94       break;
95     case STATE_EXPLODING:
96       if(sprite->animation_done()) {
97         remove_me();
98       }
99       break;
100   }
101 }
102
103 void
104 Bomb::explode()
105 {
106   ticking->stop();
107
108   remove_me();
109   Explosion* explosion = new Explosion(get_bbox().get_middle());
110   Sector::current()->add_object(explosion);
111
112   run_dead_script();
113 }
114
115 void
116 Bomb::kill_fall()
117 {
118   if (state != STATE_EXPLODING)  // we don't want it exploding again
119     explode();
120 }