Kugelblitz now appears at specified position, not at [0, 0]
[supertux.git] / src / badguy / bomb.cpp
index 451fe00..540b289 100644 (file)
@@ -1,7 +1,7 @@
 //  $Id$
-// 
+//
 //  SuperTux
-//  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
+//  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
 //
 //  This program is free software; you can redistribute it and/or
 //  modify it under the terms of the GNU General Public License
 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 //  GNU General Public License for more details.
-// 
+//
 //  You should have received a copy of the GNU General Public License
 //  along with this program; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-//  02111-1307, USA.
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #include <config.h>
 
 #include "bomb.hpp"
+#include "random_generator.hpp"
+#include "object/explosion.hpp"
 
-static const float TICKINGTIME = 1;
-static const float EXPLOSIONTIME = 1;
-
-Bomb::Bomb(const Vector& pos, Direction dir)
+Bomb::Bomb(const Vector& pos, Direction dir, std::string custom_sprite /*= "images/creatures/mr_bomb/mr_bomb.sprite"*/ )
+        : BadGuy( pos, dir, custom_sprite ), grabbed(false), grabber(NULL)
 {
-  start_position = pos;
-  bbox.set_pos(pos);
-  bbox.set_size(31.8, 31.8);
-  sprite = sprite_manager->create("bomb");
-  state = 0;
-  timer.start(TICKINGTIME);
-  this->dir = dir;
-  sprite->set_action(dir == LEFT ? "ticking-left" : "ticking-right");
+  state = STATE_TICKING;
+  set_action(dir == LEFT ? "ticking-left" : "ticking-right", 1);
   countMe = false;
+
+  ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav"));
+  ticking->set_position(get_pos());
+  ticking->set_looping(true);
+  ticking->set_gain(2.0);
+  ticking->set_reference_distance(32);
+  ticking->play();
+}
+
+Bomb::Bomb(const Bomb& other)
+        : BadGuy(other), Portable(other), state(other.state)
+{
+  if (state == STATE_TICKING) {
+    ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav"));
+    ticking->set_position(get_pos());
+    ticking->set_looping(true);
+    ticking->set_gain(2.0);
+    ticking->set_reference_distance(32);
+    ticking->play();
+  }
 }
 
 void
@@ -44,62 +57,89 @@ Bomb::write(lisp::Writer& )
   // bombs are only temporarily so don't write them out...
 }
 
-HitResponse
-Bomb::collision_solid(GameObject& , const CollisionHit& hit)
+void
+Bomb::collision_solid(const CollisionHit& hit)
 {
-  if(fabsf(hit.normal.y) > .5)
+  if(hit.bottom)
     physic.set_velocity_y(0);
 
-  return CONTINUE;
+    update_on_ground_flag(hit);
 }
 
 HitResponse
-Bomb::collision_player(Player& player, const CollisionHit& )
+Bomb::collision_player(Player& , const CollisionHit& )
 {
-  if(state == 1) {
-    player.kill(Player::SHRINK);
-  }
   return ABORT_MOVE;
 }
 
 HitResponse
-Bomb::collision_badguy(BadGuy& badguy, const CollisionHit& )
+Bomb::collision_badguy(BadGuy& , const CollisionHit& )
 {
-  if(state == 1)
-    badguy.kill_fall();
   return ABORT_MOVE;
 }
 
 void
-Bomb::active_update(float )
+Bomb::active_update(float elapsed_time)
 {
-  switch(state) {
-    case 0:
-      if(timer.check()) {
-        explode();
-      }
-      break;
-    case 1:
-      if(timer.check()) {
-        remove_me();
-      }
-      break;
-  } 
+  ticking->set_position(get_pos());
+  if(sprite->animation_done()) {
+    explode();
+  }
+  else if (!grabbed) {
+    movement = physic.get_movement(elapsed_time);
+  }
 }
 
 void
 Bomb::explode()
 {
-  state = 1;
-  sprite->set_action("explosion");
-  sound_manager->play("sounds/explosion.ogg", get_pos());
-  timer.start(EXPLOSIONTIME);
+  ticking->stop();
+
+  // Make the player let go before we explode, otherwise the player is holding
+  // an invalid object. There's probably a better way to do this than in the
+  // Bomb class.
+  if (grabber != NULL) {
+    Player* player = dynamic_cast<Player*>(grabber);
+    
+    if (player)
+        player->stop_grabbing();
+  }
+
+  remove_me();
+  Explosion* explosion = new Explosion(get_bbox().get_middle());
+  Sector::current()->add_object(explosion);
+
+  run_dead_script();
 }
 
 void
 Bomb::kill_fall()
 {
-  if (state != 1)  // we don't want it exploding again
-    explode();
+  explode();
+}
+
+void
+Bomb::grab(MovingObject& object, const Vector& pos, Direction dir)
+{
+  movement = pos - get_pos();
+  this->dir = dir;
+
+  // We actually face the opposite direction of Tux here to make the fuse more
+  // visible instead of hiding it behind Tux
+  sprite->set_action_continued(dir == LEFT ? "ticking-right" : "ticking-left");
+  set_colgroup_active(COLGROUP_DISABLED);
+  grabbed = true;
+  grabber = &object;
+}
+
+void
+Bomb::ungrab(MovingObject& object, Direction dir)
+{
+  this->dir = dir;
+  // portable objects are usually pushed away from Tux when dropped, but we
+  // don't want that, so we set the position
+  set_pos(object.get_pos() + Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32));
+  set_colgroup_active(COLGROUP_MOVING);
+  grabbed = false;
 }