- fix debugging functions for killing player
[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
24 static const float TICKINGTIME = 1;
25 static const float EXPLOSIONTIME = 1;
26
27 Bomb::Bomb(const Vector& pos, Direction dir)
28 {
29   start_position = pos;
30   bbox.set_pos(pos);
31   bbox.set_size(31.8, 31.8);
32   sprite = sprite_manager->create("images/creatures/mr_bomb/bomb.sprite");
33   state = STATE_TICKING;
34   timer.start(TICKINGTIME);
35   this->dir = dir;
36   sprite->set_action(dir == LEFT ? "ticking-left" : "ticking-right");
37   countMe = false;
38 }
39
40 void
41 Bomb::write(lisp::Writer& )
42 {
43   // bombs are only temporarily so don't write them out...
44 }
45
46 HitResponse
47 Bomb::collision_solid(GameObject& , const CollisionHit& hit)
48 {
49   if(fabsf(hit.normal.y) > .5)
50     physic.set_velocity_y(0);
51
52   return CONTINUE;
53 }
54
55 HitResponse
56 Bomb::collision_player(Player& player, const CollisionHit& )
57 {
58   if(state == STATE_EXPLODING) {
59     player.kill(false);
60   }
61   return ABORT_MOVE;
62 }
63
64 HitResponse
65 Bomb::collision_badguy(BadGuy& badguy, const CollisionHit& )
66 {
67   if(state == STATE_EXPLODING)
68     badguy.kill_fall();
69   return ABORT_MOVE;
70 }
71
72 void
73 Bomb::active_update(float )
74 {
75   switch(state) {
76     case STATE_TICKING:
77       if(timer.check()) {
78         explode();
79       }
80       break;
81     case STATE_EXPLODING:
82       if(timer.check()) {
83         remove_me();
84       }
85       break;
86   } 
87 }
88
89 void
90 Bomb::explode()
91 {
92   state = STATE_EXPLODING;
93   set_group(COLGROUP_TOUCHABLE);
94   sprite->set_action("explosion");
95   sound_manager->play("sounds/explosion.wav", get_pos());
96   timer.start(EXPLOSIONTIME);
97 }
98
99 void
100 Bomb::kill_fall()
101 {
102   if (state != STATE_EXPLODING)  // we don't want it exploding again
103     explode();
104 }
105