540b28970e06ba5d5f1d362155c93b27b7e18c30
[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 #include "random_generator.hpp"
24 #include "object/explosion.hpp"
25
26 Bomb::Bomb(const Vector& pos, Direction dir, std::string custom_sprite /*= "images/creatures/mr_bomb/mr_bomb.sprite"*/ )
27         : BadGuy( pos, dir, custom_sprite ), grabbed(false), grabber(NULL)
28 {
29   state = STATE_TICKING;
30   set_action(dir == LEFT ? "ticking-left" : "ticking-right", 1);
31   countMe = false;
32
33   ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav"));
34   ticking->set_position(get_pos());
35   ticking->set_looping(true);
36   ticking->set_gain(2.0);
37   ticking->set_reference_distance(32);
38   ticking->play();
39 }
40
41 Bomb::Bomb(const Bomb& other)
42         : BadGuy(other), Portable(other), state(other.state)
43 {
44   if (state == STATE_TICKING) {
45     ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav"));
46     ticking->set_position(get_pos());
47     ticking->set_looping(true);
48     ticking->set_gain(2.0);
49     ticking->set_reference_distance(32);
50     ticking->play();
51   }
52 }
53
54 void
55 Bomb::write(lisp::Writer& )
56 {
57   // bombs are only temporarily so don't write them out...
58 }
59
60 void
61 Bomb::collision_solid(const CollisionHit& hit)
62 {
63   if(hit.bottom)
64     physic.set_velocity_y(0);
65
66     update_on_ground_flag(hit);
67 }
68
69 HitResponse
70 Bomb::collision_player(Player& , const CollisionHit& )
71 {
72   return ABORT_MOVE;
73 }
74
75 HitResponse
76 Bomb::collision_badguy(BadGuy& , const CollisionHit& )
77 {
78   return ABORT_MOVE;
79 }
80
81 void
82 Bomb::active_update(float elapsed_time)
83 {
84   ticking->set_position(get_pos());
85   if(sprite->animation_done()) {
86     explode();
87   }
88   else if (!grabbed) {
89     movement = physic.get_movement(elapsed_time);
90   }
91 }
92
93 void
94 Bomb::explode()
95 {
96   ticking->stop();
97
98   // Make the player let go before we explode, otherwise the player is holding
99   // an invalid object. There's probably a better way to do this than in the
100   // Bomb class.
101   if (grabber != NULL) {
102     Player* player = dynamic_cast<Player*>(grabber);
103     
104     if (player)
105         player->stop_grabbing();
106   }
107
108   remove_me();
109   Explosion* explosion = new Explosion(get_bbox().get_middle());
110   Sector::current()->add_object(explosion);
111
112   run_dead_script();
113 }
114
115 void
116 Bomb::kill_fall()
117 {
118   explode();
119 }
120
121 void
122 Bomb::grab(MovingObject& object, const Vector& pos, Direction dir)
123 {
124   movement = pos - get_pos();
125   this->dir = dir;
126
127   // We actually face the opposite direction of Tux here to make the fuse more
128   // visible instead of hiding it behind Tux
129   sprite->set_action_continued(dir == LEFT ? "ticking-right" : "ticking-left");
130   set_colgroup_active(COLGROUP_DISABLED);
131   grabbed = true;
132   grabber = &object;
133 }
134
135 void
136 Bomb::ungrab(MovingObject& object, Direction dir)
137 {
138   this->dir = dir;
139   // portable objects are usually pushed away from Tux when dropped, but we
140   // don't want that, so we set the position
141   set_pos(object.get_pos() + Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32));
142   set_colgroup_active(COLGROUP_MOVING);
143   grabbed = false;
144 }
145