451fe006993dda6296f342af24ae1df265667d18
[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.hpp"
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   countMe = false;
39 }
40
41 void
42 Bomb::write(lisp::Writer& )
43 {
44   // bombs are only temporarily so don't write them out...
45 }
46
47 HitResponse
48 Bomb::collision_solid(GameObject& , const CollisionHit& hit)
49 {
50   if(fabsf(hit.normal.y) > .5)
51     physic.set_velocity_y(0);
52
53   return CONTINUE;
54 }
55
56 HitResponse
57 Bomb::collision_player(Player& player, const CollisionHit& )
58 {
59   if(state == 1) {
60     player.kill(Player::SHRINK);
61   }
62   return ABORT_MOVE;
63 }
64
65 HitResponse
66 Bomb::collision_badguy(BadGuy& badguy, const CollisionHit& )
67 {
68   if(state == 1)
69     badguy.kill_fall();
70   return ABORT_MOVE;
71 }
72
73 void
74 Bomb::active_update(float )
75 {
76   switch(state) {
77     case 0:
78       if(timer.check()) {
79         explode();
80       }
81       break;
82     case 1:
83       if(timer.check()) {
84         remove_me();
85       }
86       break;
87   } 
88 }
89
90 void
91 Bomb::explode()
92 {
93   state = 1;
94   sprite->set_action("explosion");
95   sound_manager->play("sounds/explosion.ogg", 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