Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / badguy / bomb.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "audio/sound_manager.hpp"
18 #include "badguy/bomb.hpp"
19 #include "object/explosion.hpp"
20 #include "object/player.hpp"
21 #include "sprite/sprite.hpp"
22 #include "supertux/sector.hpp"
23
24 Bomb::Bomb(const Vector& pos, Direction dir, std::string custom_sprite /*= "images/creatures/mr_bomb/mr_bomb.sprite"*/ ) :
25   BadGuy( pos, dir, custom_sprite ), 
26   state(),
27   grabbed(false), 
28   grabber(NULL),
29   ticking()
30 {
31   state = STATE_TICKING;
32   set_action(dir == LEFT ? "ticking-left" : "ticking-right", 1);
33   countMe = false;
34
35   ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav"));
36   ticking->set_position(get_pos());
37   ticking->set_looping(true);
38   ticking->set_gain(2.0);
39   ticking->set_reference_distance(32);
40   ticking->play();
41 }
42
43 void
44 Bomb::collision_solid(const CollisionHit& hit)
45 {
46   if(hit.bottom)
47     physic.set_velocity_y(0);
48
49   update_on_ground_flag(hit);
50 }
51
52 HitResponse
53 Bomb::collision_player(Player& , const CollisionHit& )
54 {
55   return ABORT_MOVE;
56 }
57
58 HitResponse
59 Bomb::collision_badguy(BadGuy& , const CollisionHit& )
60 {
61   return ABORT_MOVE;
62 }
63
64 void
65 Bomb::active_update(float elapsed_time)
66 {
67   ticking->set_position(get_pos());
68   if(sprite->animation_done()) {
69     explode();
70   }
71   else if (!grabbed) {
72     movement = physic.get_movement(elapsed_time);
73   }
74 }
75
76 void
77 Bomb::explode()
78 {
79   ticking->stop();
80
81   // Make the player let go before we explode, otherwise the player is holding
82   // an invalid object. There's probably a better way to do this than in the
83   // Bomb class.
84   if (grabber != NULL) {
85     Player* player = dynamic_cast<Player*>(grabber);
86     
87     if (player)
88       player->stop_grabbing();
89   }
90
91   if(is_valid()) {
92     remove_me();
93     Explosion* explosion = new Explosion(get_bbox().get_middle());
94     Sector::current()->add_object(explosion);
95   }
96
97   run_dead_script();
98 }
99
100 void
101 Bomb::kill_fall()
102 {
103   explode();
104 }
105
106 void
107 Bomb::grab(MovingObject& object, const Vector& pos, Direction dir)
108 {
109   movement = pos - get_pos();
110   this->dir = dir;
111
112   // We actually face the opposite direction of Tux here to make the fuse more
113   // visible instead of hiding it behind Tux
114   sprite->set_action_continued(dir == LEFT ? "ticking-right" : "ticking-left");
115   set_colgroup_active(COLGROUP_DISABLED);
116   grabbed = true;
117   grabber = &object;
118 }
119
120 void
121 Bomb::ungrab(MovingObject& object, Direction dir)
122 {
123   this->dir = dir;
124   // portable objects are usually pushed away from Tux when dropped, but we
125   // don't want that, so we set the position
126   set_pos(object.get_pos() + Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32));
127   set_colgroup_active(COLGROUP_MOVING);
128   grabbed = false;
129 }
130
131 /* EOF */