Bug 471: Fix upward release of ice block, and grabbing cleanups.
[supertux.git] / src / object / player.cpp
index 6e756f3..77ef593 100644 (file)
@@ -161,6 +161,7 @@ Player::Player(PlayerStatus* _player_status, const std::string& name) :
   sound_manager->preload("sounds/bigjump.wav");
   sound_manager->preload("sounds/jump.wav");
   sound_manager->preload("sounds/hurt.wav");
+  sound_manager->preload("sounds/kill.wav");
   sound_manager->preload("sounds/skid.wav");
   sound_manager->preload("sounds/flip.wav");
   sound_manager->preload("sounds/invincible_start.ogg");
@@ -374,9 +375,7 @@ Player::update(float elapsed_time)
   movement = physic.get_movement(elapsed_time);
 
   if(grabbed_object != NULL && !dying) {
-    Vector pos = get_pos() +
-      Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
-    grabbed_object->grab(*this, pos, dir);
+    position_grabbed_object();
   }
 
   if(grabbed_object != NULL && dying){
@@ -657,15 +656,10 @@ Player::handle_vertical_input()
   if(controller->hold(Controller::JUMP) && jump_button_timer.started() && can_jump) {
     jump_button_timer.stop();
     if (duck) {
-      // when running, only jump a little bit; else do a backflip
-      if ((physic.get_velocity_x() != 0) || 
-          (controller->hold(Controller::LEFT)) || 
-          (controller->hold(Controller::RIGHT))) 
-      {
+      // jump a little bit if moving in same direction; otherwise, do a backflip
+      if ((physic.get_velocity_x() < 0 && dir == LEFT) || (physic.get_velocity_x() > 0 && dir == RIGHT)) {
         do_jump(-300);
-      }
-      else 
-      {
+      } else {
         do_backflip();
       }
     } else {
@@ -757,6 +751,7 @@ Player::handle_input()
     if(Sector::current()->add_bullet(
          get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
                       : Vector(32, bbox.get_height()/2)),
+         player_status,
          physic.get_velocity_x(), dir))
       shooting_timer.start(SHOOTING_TIME);
   }
@@ -772,29 +767,54 @@ Player::handle_input()
   try_grab();
 
   if(!controller->hold(Controller::ACTION) && grabbed_object) {
-    // move the grabbed object a bit away from tux
-    Vector pos = get_pos() +
-      Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
-             bbox.get_height()*0.66666 - 32);
-    Rectf dest(pos, pos + Vector(32, 32));
-    if(Sector::current()->is_free_of_movingstatics(dest)) {
-      MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
-      if(moving_object) {
-        moving_object->set_pos(pos);
+    MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
+    if(moving_object) {
+      // move the grabbed object a bit away from tux
+      Rectf grabbed_bbox = moving_object->get_bbox();
+      Rectf dest;
+      dest.p2.y = bbox.get_top() + bbox.get_height()*0.66666;
+      dest.p1.y = dest.p2.y - grabbed_bbox.get_height();
+      if(dir == LEFT) {
+        dest.p2.x = bbox.get_left() - 1;
+        dest.p1.x = dest.p2.x - grabbed_bbox.get_width();
       } else {
-        log_debug << "Non MovingObject grabbed?!?" << std::endl;
+        dest.p1.x = bbox.get_right() + 1;
+        dest.p2.x = dest.p1.x + grabbed_bbox.get_width();
       }
-      if(controller->hold(Controller::UP)) {
-        grabbed_object->ungrab(*this, UP);
-      } else {
-        grabbed_object->ungrab(*this, dir);
+      if(Sector::current()->is_free_of_movingstatics(dest)) {
+        moving_object->set_pos(dest.p1);
+        if(controller->hold(Controller::UP)) {
+          grabbed_object->ungrab(*this, UP);
+        } else {
+          grabbed_object->ungrab(*this, dir);
+        }
+        grabbed_object = NULL;
       }
-      grabbed_object = NULL;
+    } else {
+      log_debug << "Non MovingObject grabbed?!?" << std::endl;
     }
   }
 }
 
 void
+Player::position_grabbed_object()
+{
+  MovingObject* moving_object = dynamic_cast<MovingObject*>(grabbed_object);
+  assert(moving_object);
+
+  // Position where we will hold the lower-inner corner
+  Vector pos(get_bbox().get_left() + get_bbox().get_width()/2,
+      get_bbox().get_top() + get_bbox().get_height()*0.66666);
+
+  // Adjust to find the grabbed object's upper-left corner
+  if (dir == LEFT)
+    pos.x -= moving_object->get_bbox().get_width();
+  pos.y -= moving_object->get_bbox().get_height();
+
+  grabbed_object->grab(*this, pos, dir);
+}
+
+void
 Player::try_grab()
 {
   if(controller->hold(Controller::ACTION) && !grabbed_object
@@ -826,7 +846,7 @@ Player::try_grab()
       if(moving_object->get_bbox().contains(pos)) {
         if (climbing) stop_climbing(*climbing);
         grabbed_object = portable;
-        grabbed_object->grab(*this, get_pos(), dir);
+        position_grabbed_object();
         break;
       }
     }
@@ -995,7 +1015,7 @@ Player::draw(DrawingContext& context)
     float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
     float py = Sector::current()->camera->get_translation().y;
     py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
-    context.draw_surface(airarrow.get(), Vector(px, py), LAYER_HUD - 1);
+    context.draw_surface(airarrow, Vector(px, py), LAYER_HUD - 1);
   }
 
   std::string sa_prefix = "";
@@ -1148,6 +1168,7 @@ Player::collision_solid(const CollisionHit& hit)
                                       90-40, 90-20,
                                       Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
                                       LAYER_OBJECTS+1));
+      Sector::current()->camera->shake(.1f, 0, 5);
     }
 
   } else if(hit.top) {
@@ -1223,13 +1244,13 @@ Player::kill(bool completely)
 
   growing = false;
 
-  sound_manager->play("sounds/hurt.wav");
-
   if (climbing) stop_climbing(*climbing);
 
   physic.set_velocity_x(0);
 
   if(!completely && is_big()) {
+    sound_manager->play("sounds/hurt.wav");
+
     if(player_status->bonus == FIRE_BONUS
        || player_status->bonus == ICE_BONUS) {
       safe_timer.start(TUX_SAFE_TIME);
@@ -1246,6 +1267,7 @@ Player::kill(bool completely)
       duck = false;
     }
   } else {
+    sound_manager->play("sounds/kill.wav");
 
     // do not die when in edit mode
     if (edit_mode) {