X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fobject%2Ftrampoline.cpp;h=4871cf6396f3c19b4b1ea2f75cd5a44ac93d85e0;hb=781b60522acc0f34ba4091838d12aa0db7cc9a56;hp=bd21fd368d564f915729ee7ba198d699553d563c;hpb=4a486d92343d1824b311c234e9321e08f280fe68;p=supertux.git diff --git a/src/object/trampoline.cpp b/src/object/trampoline.cpp index bd21fd368..4871cf639 100644 --- a/src/object/trampoline.cpp +++ b/src/object/trampoline.cpp @@ -24,6 +24,7 @@ #include "player.hpp" #include "audio/sound_manager.hpp" #include "sprite/sprite_manager.hpp" +#include "badguy/walking_badguy.hpp" /* Trampoline will accelerate player to to VY_INITIAL, if * he jumps on it to VY_MIN. */ @@ -34,104 +35,93 @@ namespace { } Trampoline::Trampoline(const lisp::Lisp& lisp) - : MovingSprite(lisp, "images/objects/trampoline/trampoline.sprite" ) + : Rock(lisp, "images/objects/trampoline/trampoline.sprite") { - sound_manager->preload( TRAMPOLINE_SOUND ); - physic.set_velocity(0, 0); - physic.enable_gravity(true); - on_ground = false; + sound_manager->preload(TRAMPOLINE_SOUND); portable = true; //Check if this trampoline is not portable - if( lisp.get( "portable", portable ) ){ - if( !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 ) +Trampoline::update(float elapsed_time) { - if(!on_ground) { - movement = physic.get_movement(elapsed_time); - } if(sprite->animation_done()) { sprite->set_action("normal"); } + + Rock::update(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 ){ - return FORCE_MOVE; - } - Player* player = dynamic_cast (&other); - 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)) { - vy = VY_MIN; - } else { - vy = VY_INITIAL; + if(on_ground) { + Player* player = dynamic_cast (&other); + //Trampoline works for 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)) { + vy = VY_MIN; + } else { + vy = VY_INITIAL; + } + player->physic.set_velocity_y(vy); + sound_manager->play(TRAMPOLINE_SOUND); + sprite->set_action("swinging", 1); + return FORCE_MOVE; } - 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 (&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 ); - } + WalkingBadguy* walking_badguy = dynamic_cast (&other); + //Trampoline also works for WalkingBadguy + if(walking_badguy) { + float vy = walking_badguy->get_velocity_y(); + //walking_badguy is falling down on trampoline + if(hit.top && vy >= 0) { + vy = VY_INITIAL; + walking_badguy->set_velocity_y(vy); + sound_manager->play(TRAMPOLINE_SOUND); + sprite->set_action("swinging", 1); + return FORCE_MOVE; } - 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; + + return Rock::collision(other, hit); } void -Trampoline::collision_solid( const CollisionHit& hit ){ - if( hit.bottom ){ - on_ground = true; - } +Trampoline::collision_solid(const CollisionHit& hit) { + Rock::collision_solid(hit); } void -Trampoline::grab( MovingObject&, const Vector& pos, Direction ){ - movement = pos - get_pos(); - set_group( COLGROUP_DISABLED ); - on_ground = true; - sprite->set_animation_loops( 0 ); +Trampoline::grab(MovingObject& object, const Vector& pos, Direction dir) { + sprite->set_animation_loops(0); + Rock::grab(object, pos, dir); } void -Trampoline::ungrab(MovingObject& , Direction ){ - set_group( COLGROUP_MOVING ); - on_ground = false; - physic.set_velocity(0, 0); +Trampoline::ungrab(MovingObject& object, Direction dir) { + Rock::ungrab(object, dir); } bool Trampoline::is_portable() const { - return portable; + return Rock::is_portable() && portable; } IMPLEMENT_FACTORY(Trampoline, "trampoline");