src/badguy/jumpy.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "badguy/jumpy.hpp"
00018 
00019 #include "object/player.hpp"
00020 #include "sprite/sprite.hpp"
00021 #include "supertux/object_factory.hpp"
00022 
00023 static const float JUMPYSPEED=-600;
00024 static const float JUMPY_MID_TOLERANCE=4;
00025 static const float JUMPY_LOW_TOLERANCE=2;
00026 
00027 Jumpy::Jumpy(const Reader& reader) :
00028   BadGuy(reader, "images/creatures/snowjumpy/snowjumpy.sprite"), 
00029   pos_groundhit(),
00030   groundhit_pos_set(false)
00031 {
00032   // TODO create a nice sound for this...
00033   //sound_manager->preload("sounds/skid.wav");
00034 }
00035 
00036 void
00037 Jumpy::collision_solid(const CollisionHit& chit)
00038 {
00039   hit(chit);
00040 }
00041 
00042 HitResponse
00043 Jumpy::collision_badguy(BadGuy& , const CollisionHit& chit)
00044 {
00045   return hit(chit);
00046 }
00047 
00048 HitResponse
00049 Jumpy::hit(const CollisionHit& chit)
00050 {
00051   if(chit.bottom) {
00052     if (!groundhit_pos_set)
00053     {
00054       pos_groundhit = get_pos();
00055       groundhit_pos_set = true;
00056     }
00057 
00058     physic.set_velocity_y((frozen || get_state() == STATE_FALLING) ? 0 : JUMPYSPEED);
00059     // TODO create a nice sound for this...
00060     //sound_manager->play("sounds/skid.wav");
00061   } else if(chit.top) {
00062     physic.set_velocity_y(0);
00063   }
00064 
00065   return CONTINUE;
00066 }
00067 
00068 void
00069 Jumpy::active_update(float elapsed_time)
00070 {
00071   BadGuy::active_update(elapsed_time);
00072 
00073   if(frozen)
00074     return;
00075 
00076   Player* player = this->get_nearest_player();
00077   if (player)
00078   {
00079     dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
00080   }
00081 
00082   if (!groundhit_pos_set)
00083   {
00084     sprite->set_action(dir == LEFT ? "left-middle" : "right-middle");
00085     return;
00086   }
00087 
00088   if ( get_pos().y < (pos_groundhit.y - JUMPY_MID_TOLERANCE ) )
00089     sprite->set_action(dir == LEFT ? "left-up" : "right-up");
00090   else if ( get_pos().y >= (pos_groundhit.y - JUMPY_MID_TOLERANCE) &&
00091             get_pos().y < (pos_groundhit.y - JUMPY_LOW_TOLERANCE) )
00092     sprite->set_action(dir == LEFT ? "left-middle" : "right-middle");
00093   else
00094     sprite->set_action(dir == LEFT ? "left-down" : "right-down");
00095 }
00096 
00097 void
00098 Jumpy::freeze()
00099 {
00100   BadGuy::freeze();
00101   physic.set_velocity_y(std::max(0.0f, physic.get_velocity_y()));
00102   sprite->set_action(dir == LEFT ? "left-iced" : "right-iced");
00103 }
00104 
00105 bool
00106 Jumpy::is_freezable() const
00107 {
00108   return true;
00109 }
00110 
00111 /* EOF */

Generated on Mon Jun 9 03:38:17 2014 for SuperTux by  doxygen 1.5.1