45fb8928062ae07606ce21a3723d069a93593bdd
[supertux.git] / src / badguy / bomb.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 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
19 //  02111-1307, USA.
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   set_group(COLGROUP_TOUCHABLE);
40 }
41
42 void
43 Bomb::write(lisp::Writer& )
44 {
45   // bombs are only temporarily so don't write them out...
46 }
47
48 HitResponse
49 Bomb::collision_solid(GameObject& , const CollisionHit& hit)
50 {
51   if(fabsf(hit.normal.y) > .5)
52     physic.set_velocity_y(0);
53
54   return CONTINUE;
55 }
56
57 HitResponse
58 Bomb::collision_player(Player& player, const CollisionHit& )
59 {
60   if(state == STATE_EXPLODING) {
61     player.kill(Player::SHRINK);
62   }
63   return ABORT_MOVE;
64 }
65
66 HitResponse
67 Bomb::collision_badguy(BadGuy& badguy, const CollisionHit& )
68 {
69   if(state == STATE_EXPLODING)
70     badguy.kill_fall();
71   return ABORT_MOVE;
72 }
73
74 void
75 Bomb::active_update(float )
76 {
77   switch(state) {
78     case STATE_TICKING:
79       if(timer.check()) {
80         explode();
81       }
82       break;
83     case STATE_EXPLODING:
84       if(timer.check()) {
85         remove_me();
86       }
87       break;
88   } 
89 }
90
91 void
92 Bomb::explode()
93 {
94   state = STATE_EXPLODING;
95   set_group(COLGROUP_TOUCHABLE);
96   sprite->set_action("explosion");
97   sound_manager->play("sounds/explosion.wav", get_pos());
98   timer.start(EXPLOSIONTIME);
99 }
100
101 void
102 Bomb::kill_fall()
103 {
104   if (state != STATE_EXPLODING)  // we don't want it exploding again
105     explode();
106 }
107