src/badguy/toad.cpp

Go to the documentation of this file.
00001 //  Toad - A jumping toad
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/toad.hpp"
00018 
00019 #include "audio/sound_manager.hpp"
00020 #include "object/player.hpp"
00021 #include "sprite/sprite.hpp"
00022 #include "supertux/object_factory.hpp"
00023 
00024 namespace {
00025 const float VERTICAL_SPEED = -450;   
00026 const float HORIZONTAL_SPEED = 320; 
00027 const float TOAD_RECOVER_TIME = 0.5; 
00028 static const std::string HOP_SOUND = "sounds/hop.ogg";
00029 }
00030 
00031 Toad::Toad(const Reader& reader) :
00032   BadGuy(reader, "images/creatures/toad/toad.sprite"),
00033   recover_timer(),
00034   state()
00035 {
00036   sound_manager->preload(HOP_SOUND);
00037 }
00038 
00039 Toad::Toad(const Vector& pos, Direction d) :
00040   BadGuy(pos, d, "images/creatures/toad/toad.sprite"),
00041   recover_timer(),
00042   state()
00043 {
00044   sound_manager->preload(HOP_SOUND);
00045 }
00046 
00047 void
00048 Toad::initialize()
00049 {
00050   // initial state is JUMPING, because we might start airborne
00051   state = JUMPING;
00052   sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
00053 }
00054 
00055 void
00056 Toad::set_state(ToadState newState)
00057 {
00058   if (newState == IDLE) {
00059     physic.set_velocity_x(0);
00060     physic.set_velocity_y(0);
00061     sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
00062 
00063     recover_timer.start(TOAD_RECOVER_TIME);
00064   } else
00065     if (newState == JUMPING) {
00066       sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
00067       physic.set_velocity_x(dir == LEFT ? -HORIZONTAL_SPEED : HORIZONTAL_SPEED);
00068       physic.set_velocity_y(VERTICAL_SPEED);
00069       sound_manager->play( HOP_SOUND, get_pos());
00070     } else
00071       if (newState == FALLING) {
00072         Player* player = get_nearest_player();
00073         // face player
00074         if (player && (player->get_bbox().p2.x < get_bbox().p1.x) && (dir == RIGHT)) dir = LEFT;
00075         if (player && (player->get_bbox().p1.x > get_bbox().p2.x) && (dir == LEFT)) dir = RIGHT;
00076         sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
00077       }
00078 
00079   state = newState;
00080 }
00081 
00082 bool
00083 Toad::collision_squished(GameObject& object)
00084 {
00085   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
00086   kill_squished(object);
00087   return true;
00088 }
00089 
00090 void
00091 Toad::collision_solid(const CollisionHit& hit)
00092 {
00093   // just default behaviour (i.e. stop at floor/walls) when squished
00094   if (BadGuy::get_state() == STATE_SQUISHED) {
00095     BadGuy::collision_solid(hit);
00096     return;
00097   }
00098 
00099   // ignore collisions while standing still
00100   if(state == IDLE) {
00101     return;
00102   }
00103 
00104   // check if we hit left or right while moving in either direction
00105   if(((physic.get_velocity_x() < 0) && hit.left) || ((physic.get_velocity_x() > 0) && hit.right)) {
00106     /*
00107       dir = dir == LEFT ? RIGHT : LEFT;
00108       if (state == JUMPING) {
00109       sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
00110       } else {
00111       sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
00112       }
00113     */
00114     physic.set_velocity_x(-0.25*physic.get_velocity_x());
00115   }
00116 
00117   // check if we hit the floor while falling
00118   if ((state == FALLING) && hit.bottom) {
00119     set_state(IDLE);
00120     return;
00121   }
00122 
00123   // check if we hit the roof while climbing
00124   if ((state == JUMPING) && hit.top) {
00125     physic.set_velocity_y(0);
00126   }
00127 
00128 }
00129 
00130 HitResponse
00131 Toad::collision_badguy(BadGuy& , const CollisionHit& hit)
00132 {
00133   // behaviour for badguy collisions is the same as for collisions with solids
00134   collision_solid(hit);
00135 
00136   return CONTINUE;
00137 }
00138 
00139 void
00140 Toad::active_update(float elapsed_time)
00141 {
00142   BadGuy::active_update(elapsed_time);
00143 
00144   // change sprite when we are falling
00145   if ((state == JUMPING) && (physic.get_velocity_y() > 0)) {
00146     set_state(FALLING);
00147     return;
00148   }
00149 
00150   // jump when fully recovered
00151   if ((state == IDLE) && (recover_timer.check())) {
00152     set_state(JUMPING);
00153     return;
00154   }
00155 
00156 }
00157 
00158 /* EOF */

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