Addded property to Worldmap Spawnpoint to let Tux automatically start walking (or...
[supertux.git] / src / object / trampoline.cpp
index 7df1e31..c467dfc 100644 (file)
@@ -1,8 +1,8 @@
-//  $Id$
+//  $Id:$
 //
 //  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
@@ -25,7 +25,7 @@
 #include "audio/sound_manager.hpp"
 #include "sprite/sprite_manager.hpp"
 
-/* Trampoline will accelerate player to to VY_INITIAL, if 
+/* 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";
@@ -34,93 +34,98 @@ namespace {
 }
 
 Trampoline::Trampoline(const lisp::Lisp& lisp)
-       : MovingSprite(lisp, "images/objects/trampoline/trampoline.sprite" )
+       : MovingSprite(lisp, "images/objects/trampoline/trampoline.sprite")
 {
-  sound_manager->preload( TRAMPOLINE_SOUND );
-  flags |= FLAG_PORTABLE;
-  flags |= FLAG_SOLID;
+  set_group(COLGROUP_MOVING_STATIC);
+  sound_manager->preload(TRAMPOLINE_SOUND);
   physic.set_velocity(0, 0);
   physic.enable_gravity(true);
   on_ground = false;
-
-  bool portable = true;
+  grabbed = false;
+  portable = true;
   //Check if this trampoline is not portable
-  if( lisp.get( "portable", portable ) ){
-    if( !portable ){
-        flags ^= FLAG_PORTABLE;
+  if(lisp.get("portable", portable)) {
+    if(!portable) {
         //we need another sprite
         sprite_name = "images/objects/trampoline/trampoline_fix.sprite";
-        sprite = sprite_manager->create( sprite_name );
+        sprite = sprite_manager->create(sprite_name);
         sprite->set_action("normal");
     }
   }
 }
 
 void
-Trampoline::update( float elapsed_time ){
-    if( !on_ground ){
-        movement = physic.get_movement(elapsed_time);
-    }
-    if(sprite->animation_done()) {
-      sprite->set_action("normal");
-    }
+Trampoline::update(float elapsed_time)
+{
+  if(sprite->animation_done()) {
+    sprite->set_action("normal");
+  }
+  if(!grabbed) {
+    movement = physic.get_movement(elapsed_time);
+  }
 }
 
 HitResponse
-Trampoline::collision(GameObject& other, const CollisionHit& hit )
+Trampoline::collision(GameObject& other, const CollisionHit& hit)
 {
   //Tramponine has to be on ground to work.
-  if( !on_ground ){
+  if(!on_ground) {
       return FORCE_MOVE;
   }
   Player* player = dynamic_cast<Player*> (&other);
-  if ( player ) {
+  if(player) {
     float vy = player->physic.get_velocity_y();
     //player is falling down on trampoline
-    if(hit.top && vy > 0) {
-      if(player->get_controller()->hold(Controller::JUMP)) { 
+    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 );
-      sound_manager->play( TRAMPOLINE_SOUND );
+      player->physic.set_velocity_y(vy);
+      sound_manager->play(TRAMPOLINE_SOUND);
       sprite->set_action("swinging", 1);
       return FORCE_MOVE;
     }
   }
-  //Fake being solid for moving_object. 
-  MovingObject* moving_object = dynamic_cast<MovingObject*> (&other);
-  if( moving_object ){
-      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 
-Trampoline::collision_solid( const CollisionHit& hit ){
-  if( hit.bottom ){
+void
+Trampoline::collision_solid(const CollisionHit& hit) {
+  if(hit.top || hit.bottom)
+    physic.set_velocity_y(0);
+  if(hit.left || hit.right)
+    physic.set_velocity_x(0);
+  if(hit.crush)
+    physic.set_velocity(0, 0);
+
+  if(hit.bottom && !on_ground) {
      on_ground = true;
   }
-} 
+}
 
 void
-Trampoline::grab( MovingObject&, const Vector& pos, Direction ){
+Trampoline::grab(MovingObject&, const Vector& pos, Direction) {
   movement = pos - get_pos();
-  set_group( COLGROUP_DISABLED );
-  on_ground = true;
-  sprite->set_animation_loops( 0 );
+  set_group(COLGROUP_DISABLED);
+  sprite->set_animation_loops(0);
+  on_ground = false;
+  grabbed = true;
 }
 
 void
-Trampoline::ungrab(MovingObject& , Direction ){
-  set_group( COLGROUP_MOVING );
+Trampoline::ungrab(MovingObject& , Direction{
+  set_group(COLGROUP_MOVING_STATIC);
   on_ground = false;
   physic.set_velocity(0, 0);
+  grabbed = false;
 }
 
+bool
+Trampoline::is_portable() const
+{
+  return portable;
+}
 
 IMPLEMENT_FACTORY(Trampoline, "trampoline");