src/badguy/skullyhop.cpp

Go to the documentation of this file.
00001 //  SkullyHop - A Hopping Skull
00002 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.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/skullyhop.hpp"
00018 
00019 #include "audio/sound_manager.hpp"
00020 #include "math/random_generator.hpp"
00021 #include "sprite/sprite.hpp"
00022 #include "supertux/object_factory.hpp"
00023 
00024 namespace {
00025 const float MIN_RECOVER_TIME = 0.1f; 
00026 const float MAX_RECOVER_TIME = 1.0f; 
00027 static const std::string SKULLYHOP_SOUND = "sounds/hop.ogg";
00028 }
00029 
00030 SkullyHop::SkullyHop(const Reader& reader) :
00031   BadGuy(reader, "images/creatures/skullyhop/skullyhop.sprite"),
00032   recover_timer(),
00033   state()
00034 {
00035   sound_manager->preload( SKULLYHOP_SOUND );
00036 }
00037 
00038 SkullyHop::SkullyHop(const Vector& pos, Direction d) :
00039   BadGuy(pos, d, "images/creatures/skullyhop/skullyhop.sprite"),
00040   recover_timer(),
00041   state()
00042 {
00043   sound_manager->preload( SKULLYHOP_SOUND );
00044 }
00045 
00046 void
00047 SkullyHop::initialize()
00048 {
00049   // initial state is JUMPING, because we might start airborne
00050   state = JUMPING;
00051   sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
00052 }
00053 
00054 void
00055 SkullyHop::set_state(SkullyHopState newState)
00056 {
00057   if (newState == STANDING) {
00058     physic.set_velocity_x(0);
00059     physic.set_velocity_y(0);
00060     sprite->set_action(dir == LEFT ? "standing-left" : "standing-right");
00061 
00062     float recover_time = gameRandom.randf(MIN_RECOVER_TIME,MAX_RECOVER_TIME);
00063     recover_timer.start(recover_time);
00064   } else
00065     if (newState == CHARGING) {
00066       sprite->set_action(dir == LEFT ? "charging-left" : "charging-right", 1);
00067     } else
00068       if (newState == JUMPING) {
00069         sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
00070 const float HORIZONTAL_SPEED = 220; 
00071         physic.set_velocity_x(dir == LEFT ? -HORIZONTAL_SPEED : HORIZONTAL_SPEED);
00072 const float VERTICAL_SPEED = -450;   
00073         physic.set_velocity_y(VERTICAL_SPEED);
00074         sound_manager->play( SKULLYHOP_SOUND, get_pos());
00075       }
00076 
00077   state = newState;
00078 }
00079 
00080 bool
00081 SkullyHop::collision_squished(GameObject& object)
00082 {
00083   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
00084   kill_squished(object);
00085   return true;
00086 }
00087 
00088 void
00089 SkullyHop::collision_solid(const CollisionHit& hit)
00090 {
00091   // just default behaviour (i.e. stop at floor/walls) when squished
00092   if (BadGuy::get_state() == STATE_SQUISHED) {
00093     BadGuy::collision_solid(hit);
00094   }
00095 
00096   // ignore collisions while standing still
00097   if(state != JUMPING)
00098     return;
00099 
00100   // check if we hit the floor while falling
00101   if(hit.bottom && physic.get_velocity_y() > 0 ) {
00102     set_state(STANDING);
00103   }
00104   // check if we hit the roof while climbing
00105   if(hit.top) {
00106     physic.set_velocity_y(0);
00107   }
00108 
00109   // check if we hit left or right while moving in either direction
00110   if(hit.left || hit.right) {
00111     dir = dir == LEFT ? RIGHT : LEFT;
00112     sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
00113     physic.set_velocity_x(-0.25*physic.get_velocity_x());
00114   }
00115 }
00116 
00117 HitResponse
00118 SkullyHop::collision_badguy(BadGuy& , const CollisionHit& hit)
00119 {
00120   // behaviour for badguy collisions is the same as for collisions with solids
00121   collision_solid(hit);
00122 
00123   return CONTINUE;
00124 }
00125 
00126 void
00127 SkullyHop::active_update(float elapsed_time)
00128 {
00129   BadGuy::active_update(elapsed_time);
00130 
00131   // charge when fully recovered
00132   if ((state == STANDING) && (recover_timer.check())) {
00133     set_state(CHARGING);
00134     return;
00135   }
00136 
00137   // jump as soon as charging animation completed
00138   if ((state == CHARGING) && (sprite->animation_done())) {
00139     set_state(JUMPING);
00140     return;
00141   }
00142 }
00143 
00144 /* EOF */

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