fix cr/lfs and remove trailing whitespaces...
[supertux.git] / src / object / trampoline.cpp
index 58f0cc3..ed83793 100644 (file)
@@ -2,7 +2,7 @@
 //
 //  SuperTux - Trampoline
 //  Copyright (C) 2006 Wolfgang Becker <uafr@gmx.de>
-//  
+//
 //  This program is free software; you can redistribute it and/or
 //  modify it under the terms of the GNU General Public License
 //  as published by the Free Software Foundation; either version 2
 #include "audio/sound_manager.hpp"
 #include "sprite/sprite_manager.hpp"
 
-/* Trampoline will accelerate player by VY_FACTOR (or to VY_INITIAL if 
- * that's faster) if he jumps on it. Acceleration is capped at VY_MIN. */
-namespace{
-  static const std::string TRAMPOLINE_SOUND = "sounds/trampoline.wav";
-  static const float VY_MIN = -1000; //negative, upwards
-  static const float VY_FACTOR = -1.5;
-  static const float VY_INITIAL = -500;
+/* Trampoline will accelerate player to to VY_INITIAL, if
+ * he jumps on it to VY_MIN. */
+namespace {
+  const std::string TRAMPOLINE_SOUND = "sounds/trampoline.wav";
+  const float VY_MIN = -900; //negative, upwards
+  const float VY_INITIAL = -500;
 }
 
 Trampoline::Trampoline(const lisp::Lisp& lisp)
@@ -39,21 +38,21 @@ Trampoline::Trampoline(const lisp::Lisp& lisp)
 {
   sound_manager->preload( TRAMPOLINE_SOUND );
   flags |= FLAG_PORTABLE;
-  physic.set_velocity_y(0);
+  physic.set_velocity(0, 0);
   physic.enable_gravity(true);
   on_ground = false;
 
-  //Check if we need another sprite
-  if( !lisp.get( "sprite", sprite_name ) ){
-    return;
-  }
-  if( sprite_name == "" ){
-    sprite_name = "images/objects/trampoline/trampoline.sprite";
-    return;
+  bool portable = true;
+  //Check if this trampoline is not portable
+  if( lisp.get( "portable", portable ) ){
+    if( !portable ){
+        flags ^= FLAG_PORTABLE;
+        //we need another sprite
+        sprite_name = "images/objects/trampoline/trampoline_fix.sprite";
+        sprite = sprite_manager->create( sprite_name );
+        sprite->set_action("normal");
+    }
   }
-  //Replace sprite 
-  sprite = sprite_manager->create( sprite_name );
-
 }
 
 void
@@ -61,52 +60,73 @@ Trampoline::update( float elapsed_time ){
     if( !on_ground ){
         movement = physic.get_movement(elapsed_time);
     }
+    if(sprite->animation_done()) {
+      sprite->set_action("normal");
+    }
 }
 
 HitResponse
 Trampoline::collision(GameObject& other, const CollisionHit& hit )
 {
+  //Tramponine has to be on ground to work.
+  if( !on_ground ){
+      return FORCE_MOVE;
+  }
   Player* player = dynamic_cast<Player*> (&other);
   if ( player ) {
     float vy = player->physic.get_velocity_y();
-    //player is falling down on trampolin holding "jump"
-    if( hit.top  && vy > 0 && player->get_controller()->hold( Controller::JUMP )){ 
-      vy *= VY_FACTOR;
-      if( vy < VY_MIN ){
-          vy = VY_MIN;
-      }
-      if( vy > VY_INITIAL ){
-          vy = VY_INITIAL;
+    //player is falling down on trampoline
+    if(hit.top && vy > 0) {
+      if(player->get_controller()->hold(Controller::JUMP)) {
+        vy = VY_MIN;
+      } else {
+        vy = VY_INITIAL;
       }
       player->physic.set_velocity_y( vy );
-      //printf("nachher velocity y = %f\n", player->physic.get_velocity_y());
       sound_manager->play( TRAMPOLINE_SOUND );
-      return  SOLID;
+      sprite->set_action("swinging", 1);
+      return FORCE_MOVE;
     }
   }
-  
-  return SOLID; //TODO: Nobody should be able to walk through the trampoline.
+  //Fake being solid for moving_object.
+  MovingObject* moving_object = dynamic_cast<MovingObject*> (&other);
+  if( moving_object ){
+      if( hit.top ){
+        float inside = moving_object->get_bbox().get_bottom() - get_bbox().get_top();
+        if( inside > 0 ){
+          Vector pos = moving_object->get_pos();
+          pos.y -= inside;
+          moving_object->set_pos( pos );
+        }
+      }
+      CollisionHit hit_other = hit;
+      std::swap(hit_other.left, hit_other.right);
+      std::swap(hit_other.top, hit_other.bottom);
+      moving_object->collision_solid( hit_other );
+  }
+  return FORCE_MOVE;
 }
 
-void 
+void
 Trampoline::collision_solid( const CollisionHit& hit ){
   if( hit.bottom ){
      on_ground = true;
   }
-} 
+}
 
 void
 Trampoline::grab( MovingObject&, const Vector& pos, Direction ){
   movement = pos - get_pos();
   set_group( COLGROUP_DISABLED );
   on_ground = true;
+  sprite->set_animation_loops( 0 );
 }
 
 void
 Trampoline::ungrab(MovingObject& , Direction ){
   set_group( COLGROUP_MOVING );
   on_ground = false;
-  physic.set_velocity_y(0);
+  physic.set_velocity(0, 0);
 }