From: Christoph Sommer Date: Wed, 1 Nov 2006 15:44:19 +0000 (+0000) Subject: Trampoline is now a Rock. WalkingBadguys can bounce on Trampoline. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=781b60522acc0f34ba4091838d12aa0db7cc9a56;p=supertux.git Trampoline is now a Rock. WalkingBadguys can bounce on Trampoline. SVN-Revision: 4433 --- diff --git a/src/badguy/walking_badguy.cpp b/src/badguy/walking_badguy.cpp index 74c2fd3b0..e57a9b7cd 100644 --- a/src/badguy/walking_badguy.cpp +++ b/src/badguy/walking_badguy.cpp @@ -119,3 +119,18 @@ WalkingBadguy::unfreeze() BadGuy::unfreeze(); WalkingBadguy::activate(); } + + +float +WalkingBadguy::get_velocity_y() const +{ + return physic.get_velocity_y(); +} + +void +WalkingBadguy::set_velocity_y(float vy) +{ + physic.set_velocity_y(vy); +} + + diff --git a/src/badguy/walking_badguy.hpp b/src/badguy/walking_badguy.hpp index 313a52937..b6a934b75 100644 --- a/src/badguy/walking_badguy.hpp +++ b/src/badguy/walking_badguy.hpp @@ -39,6 +39,9 @@ public: HitResponse collision_badguy(BadGuy& badguy, const CollisionHit& hit); void freeze(); void unfreeze(); + + float get_velocity_y() const; + void set_velocity_y(float vy); protected: void turn_around(); diff --git a/src/object/rock.hpp b/src/object/rock.hpp index fb1fb0ca0..379ed07a4 100644 --- a/src/object/rock.hpp +++ b/src/object/rock.hpp @@ -43,7 +43,7 @@ public: void grab(MovingObject& object, const Vector& pos, Direction dir); void ungrab(MovingObject& object, Direction dir); -private: +protected: bool on_ground; bool grabbed; Physic physic; diff --git a/src/object/trampoline.cpp b/src/object/trampoline.cpp index c467dfc31..4871cf639 100644 --- a/src/object/trampoline.cpp +++ b/src/object/trampoline.cpp @@ -1,4 +1,4 @@ -// $Id:$ +// $Id$ // // SuperTux - Trampoline // Copyright (C) 2006 Wolfgang Becker @@ -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,14 +35,10 @@ namespace { } Trampoline::Trampoline(const lisp::Lisp& lisp) - : MovingSprite(lisp, "images/objects/trampoline/trampoline.sprite") + : Rock(lisp, "images/objects/trampoline/trampoline.sprite") { - set_group(COLGROUP_MOVING_STATIC); sound_manager->preload(TRAMPOLINE_SOUND); - physic.set_velocity(0, 0); - physic.enable_gravity(true); - on_ground = false; - grabbed = false; + portable = true; //Check if this trampoline is not portable if(lisp.get("portable", portable)) { @@ -60,72 +57,71 @@ Trampoline::update(float elapsed_time) if(sprite->animation_done()) { sprite->set_action("normal"); } - if(!grabbed) { - movement = physic.get_movement(elapsed_time); - } + + Rock::update(elapsed_time); } 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 (&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; + } + } + 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; } - player->physic.set_velocity_y(vy); - sound_manager->play(TRAMPOLINE_SOUND); - sprite->set_action("swinging", 1); - return FORCE_MOVE; } } - return FORCE_MOVE; + + return Rock::collision(other, hit); } 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; - } + Rock::collision_solid(hit); } void -Trampoline::grab(MovingObject&, const Vector& pos, Direction) { - movement = pos - get_pos(); - set_group(COLGROUP_DISABLED); +Trampoline::grab(MovingObject& object, const Vector& pos, Direction dir) { sprite->set_animation_loops(0); - on_ground = false; - grabbed = true; + Rock::grab(object, pos, dir); } void -Trampoline::ungrab(MovingObject& , Direction) { - set_group(COLGROUP_MOVING_STATIC); - on_ground = false; - physic.set_velocity(0, 0); - grabbed = false; +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"); diff --git a/src/object/trampoline.hpp b/src/object/trampoline.hpp index c534f54c6..c1eca0ffb 100644 --- a/src/object/trampoline.hpp +++ b/src/object/trampoline.hpp @@ -22,13 +22,13 @@ #include "moving_sprite.hpp" #include "lisp/lisp.hpp" -#include "object/portable.hpp" +#include "object/rock.hpp" #include "physic.hpp" /** * Jumping on a trampolin makes tux jump higher. */ -class Trampoline : public MovingSprite, public Portable +class Trampoline : public Rock { public: Trampoline(const lisp::Lisp& reader); @@ -42,10 +42,8 @@ public: bool is_portable() const; private: - Physic physic; - bool on_ground; bool portable; - bool grabbed; + }; #endif