2eadc6c1b18fac2bb674ba50ee774da792cd0357
[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
21 #include <config.h>
22
23 #include "bomb.h"
24
25 static const float TICKINGTIME = 1;
26 static const float EXPLOSIONTIME = 1;
27
28 Bomb::Bomb(const Vector& pos, Direction dir)
29 {
30   start_position = pos;
31   bbox.set_pos(pos);
32   bbox.set_size(31.8, 31.8);
33   sprite = sprite_manager->create("bomb");
34   state = 0;
35   timer.start(TICKINGTIME);
36   this->dir = dir;
37   sprite->set_action(dir == LEFT ? "ticking-left" : "ticking-right");
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 == 1) {
59     player.kill(Player::SHRINK);
60   }
61   return ABORT_MOVE;
62 }
63
64 HitResponse
65 Bomb::collision_badguy(BadGuy& badguy, const CollisionHit& )
66 {
67   if(state == 1)
68     badguy.kill_fall();
69   return ABORT_MOVE;
70 }
71
72 void
73 Bomb::active_update(float )
74 {
75   switch(state) {
76     case 0:
77       if(timer.check()) {
78         explode();
79       }
80       break;
81     case 1:
82       if(timer.check()) {
83         remove_me();
84       }
85       break;
86   } 
87 }
88
89 void
90 Bomb::explode()
91 {
92   state = 1;
93   sprite->set_action("explosion");
94   sound_manager->play_sound("explosion", get_pos(),
95                             Sector::current()->player->get_pos());
96   timer.start(EXPLOSIONTIME);
97 }
98
99 void
100 Bomb::kill_fall()
101 {
102   if (state != 1)  // we don't want it exploding again
103     explode();
104 }
105