src/badguy/bomb.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "audio/sound_manager.hpp"
00018 #include "badguy/bomb.hpp"
00019 #include "object/explosion.hpp"
00020 #include "object/player.hpp"
00021 #include "sprite/sprite.hpp"
00022 #include "supertux/sector.hpp"
00023 
00024 Bomb::Bomb(const Vector& pos, Direction dir, std::string custom_sprite /*= "images/creatures/mr_bomb/mr_bomb.sprite"*/ ) :
00025   BadGuy( pos, dir, custom_sprite ), 
00026   state(),
00027   grabbed(false), 
00028   grabber(NULL),
00029   ticking()
00030 {
00031   state = STATE_TICKING;
00032   set_action(dir == LEFT ? "ticking-left" : "ticking-right", 1);
00033   countMe = false;
00034 
00035   ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav"));
00036   ticking->set_position(get_pos());
00037   ticking->set_looping(true);
00038   ticking->set_gain(2.0);
00039   ticking->set_reference_distance(32);
00040   ticking->play();
00041 }
00042 
00043 void
00044 Bomb::collision_solid(const CollisionHit& hit)
00045 {
00046   if(hit.bottom)
00047     physic.set_velocity_y(0);
00048 
00049   update_on_ground_flag(hit);
00050 }
00051 
00052 HitResponse
00053 Bomb::collision_player(Player& , const CollisionHit& )
00054 {
00055   return ABORT_MOVE;
00056 }
00057 
00058 HitResponse
00059 Bomb::collision_badguy(BadGuy& , const CollisionHit& )
00060 {
00061   return ABORT_MOVE;
00062 }
00063 
00064 void
00065 Bomb::active_update(float elapsed_time)
00066 {
00067   ticking->set_position(get_pos());
00068   if(sprite->animation_done()) {
00069     explode();
00070   }
00071   else if (!grabbed) {
00072     movement = physic.get_movement(elapsed_time);
00073   }
00074 }
00075 
00076 void
00077 Bomb::explode()
00078 {
00079   ticking->stop();
00080 
00081   // Make the player let go before we explode, otherwise the player is holding
00082   // an invalid object. There's probably a better way to do this than in the
00083   // Bomb class.
00084   if (grabber != NULL) {
00085     Player* player = dynamic_cast<Player*>(grabber);
00086     
00087     if (player)
00088       player->stop_grabbing();
00089   }
00090 
00091   if(is_valid()) {
00092     remove_me();
00093     Explosion* explosion = new Explosion(get_bbox().get_middle());
00094     Sector::current()->add_object(explosion);
00095   }
00096 
00097   run_dead_script();
00098 }
00099 
00100 void
00101 Bomb::kill_fall()
00102 {
00103   explode();
00104 }
00105 
00106 void
00107 Bomb::grab(MovingObject& object, const Vector& pos, Direction dir)
00108 {
00109   movement = pos - get_pos();
00110   this->dir = dir;
00111 
00112   // We actually face the opposite direction of Tux here to make the fuse more
00113   // visible instead of hiding it behind Tux
00114   sprite->set_action_continued(dir == LEFT ? "ticking-right" : "ticking-left");
00115   set_colgroup_active(COLGROUP_DISABLED);
00116   grabbed = true;
00117   grabber = &object;
00118 }
00119 
00120 void
00121 Bomb::ungrab(MovingObject& object, Direction dir)
00122 {
00123   this->dir = dir;
00124   // portable objects are usually pushed away from Tux when dropped, but we
00125   // don't want that, so we set the position
00126   set_pos(object.get_pos() + Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32));
00127   set_colgroup_active(COLGROUP_MOVING);
00128   grabbed = false;
00129 }
00130 
00131 /* EOF */

Generated on Mon Jun 9 03:38:16 2014 for SuperTux by  doxygen 1.5.1