src/badguy/walking_badguy.cpp

Go to the documentation of this file.
00001 //  SuperTux - WalkingBadguy
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 <math.h>
00018 
00019 #include "badguy/walking_badguy.hpp"
00020 
00021 #include "sprite/sprite.hpp"
00022 
00023 WalkingBadguy::WalkingBadguy(const Vector& pos, 
00024                              const std::string& sprite_name, 
00025                              const std::string& walk_left_action, 
00026                              const std::string& walk_right_action, 
00027                              int layer) :
00028   BadGuy(pos, sprite_name, layer), 
00029   walk_left_action(walk_left_action), 
00030   walk_right_action(walk_right_action), 
00031   walk_speed(80), 
00032   max_drop_height(-1),
00033   turn_around_timer(),
00034   turn_around_counter()
00035 {
00036 }
00037 
00038 WalkingBadguy::WalkingBadguy(const Vector& pos, 
00039                              Direction direction, 
00040                              const std::string& sprite_name, 
00041                              const std::string& walk_left_action, 
00042                              const std::string& walk_right_action, 
00043                              int layer) :
00044   BadGuy(pos, direction, sprite_name, layer), 
00045   walk_left_action(walk_left_action), 
00046   walk_right_action(walk_right_action), 
00047   walk_speed(80), 
00048   max_drop_height(-1),
00049   turn_around_timer(),
00050   turn_around_counter()
00051 {
00052 }
00053 
00054 WalkingBadguy::WalkingBadguy(const Reader& reader, 
00055                              const std::string& sprite_name, 
00056                              const std::string& walk_left_action, 
00057                              const std::string& walk_right_action, 
00058                              int layer) :
00059   BadGuy(reader, sprite_name, layer), 
00060   walk_left_action(walk_left_action), 
00061   walk_right_action(walk_right_action), 
00062   walk_speed(80), 
00063   max_drop_height(-1),
00064   turn_around_timer(),
00065   turn_around_counter()
00066 {
00067 }
00068 
00069 void
00070 WalkingBadguy::initialize()
00071 {
00072   if(frozen)
00073     return;
00074   sprite->set_action(dir == LEFT ? walk_left_action : walk_right_action);
00075   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00076   physic.set_velocity_x(dir == LEFT ? -walk_speed : walk_speed);
00077   physic.set_acceleration_x (0.0);
00078 }
00079 
00080 void
00081 WalkingBadguy::set_walk_speed (float ws)
00082 {
00083   walk_speed = fabs (ws);
00084   /* physic.set_velocity_x(dir == LEFT ? -walk_speed : walk_speed); */
00085 }
00086 
00087 void
00088 WalkingBadguy::add_velocity (const Vector& velocity)
00089 {
00090   physic.set_velocity(physic.get_velocity() + velocity);
00091 }
00092 
00093 void
00094 WalkingBadguy::active_update(float elapsed_time, float dest_x_velocity)
00095 {
00096   BadGuy::active_update(elapsed_time);
00097 
00098   float current_x_velocity = physic.get_velocity_x ();
00099 
00100   if (frozen)
00101   {
00102     physic.set_velocity_x (0.0);
00103     physic.set_acceleration_x (0.0);
00104   }
00105   /* We're very close to our target speed. Just set it to avoid oscillation */
00106   else if ((current_x_velocity > (dest_x_velocity - 5.0))
00107       && (current_x_velocity < (dest_x_velocity + 5.0)))
00108   {
00109     physic.set_velocity_x (dest_x_velocity);
00110     physic.set_acceleration_x (0.0);
00111   }
00112   /* Check if we're going too slow or even in the wrong direction */
00113   else if (((dest_x_velocity <= 0.0) && (current_x_velocity > dest_x_velocity))
00114       || ((dest_x_velocity > 0.0) && (current_x_velocity < dest_x_velocity)))
00115   {
00116     /* acceleration == walk-speed => it will take one second to get from zero
00117      * to full speed. */
00118     physic.set_acceleration_x (dest_x_velocity);
00119   }
00120   /* Check if we're going too fast */
00121   else if (((dest_x_velocity <= 0.0) && (current_x_velocity < dest_x_velocity))
00122       || ((dest_x_velocity > 0.0) && (current_x_velocity > dest_x_velocity)))
00123   {
00124     /* acceleration == walk-speed => it will take one second to get twice the
00125      * speed to normal speed. */
00126     physic.set_acceleration_x ((-1.0) * dest_x_velocity);
00127   }
00128   else
00129   {
00130     /* The above should have covered all cases. */
00131     assert (23 == 42);
00132   }
00133 
00134   if (max_drop_height > -1) {
00135     if (on_ground() && might_fall(max_drop_height+1))
00136     {
00137       turn_around();
00138     }
00139   }
00140 
00141   if ((dir == LEFT) && (physic.get_velocity_x () > 0.0)) {
00142     dir = RIGHT;
00143     set_action (walk_right_action, /* loops = */ -1);
00144   }
00145   else if ((dir == RIGHT) && (physic.get_velocity_x () < 0.0)) {
00146     dir = LEFT;
00147     set_action (walk_left_action, /* loops = */ -1);
00148   }
00149 }
00150 
00151 void
00152 WalkingBadguy::active_update(float elapsed_time)
00153 {
00154   this->active_update (elapsed_time, (dir == LEFT) ? -walk_speed : +walk_speed);
00155 }
00156 
00157 void
00158 WalkingBadguy::collision_solid(const CollisionHit& hit)
00159 {
00160 
00161   update_on_ground_flag(hit);
00162 
00163   if (hit.top) {
00164     if (physic.get_velocity_y() < 0) physic.set_velocity_y(0);
00165   }
00166   if (hit.bottom) {
00167     if (physic.get_velocity_y() > 0) physic.set_velocity_y(0);
00168   }
00169 
00170   if ((hit.left && (hit.slope_normal.y == 0) && (dir == LEFT)) || (hit.right && (hit.slope_normal.y == 0) && (dir == RIGHT))) {
00171     turn_around();
00172   }
00173 
00174 }
00175 
00176 HitResponse
00177 WalkingBadguy::collision_badguy(BadGuy& , const CollisionHit& hit)
00178 {
00179 
00180   if ((hit.left && (dir == LEFT)) || (hit.right && (dir == RIGHT))) {
00181     turn_around();
00182   }
00183 
00184   return CONTINUE;
00185 }
00186 
00187 void
00188 WalkingBadguy::turn_around()
00189 {
00190   if(frozen)
00191     return;
00192   dir = dir == LEFT ? RIGHT : LEFT;
00193   sprite->set_action(dir == LEFT ? walk_left_action : walk_right_action);
00194   physic.set_velocity_x(-physic.get_velocity_x());
00195   physic.set_acceleration_x (-physic.get_acceleration_x ());
00196 
00197   // if we get dizzy, we fall off the screen
00198   if (turn_around_timer.started()) {
00199     if (turn_around_counter++ > 10) kill_fall();
00200   } else {
00201     turn_around_timer.start(1);
00202     turn_around_counter = 0;
00203   }
00204 
00205 }
00206 
00207 void
00208 WalkingBadguy::freeze()
00209 {
00210   BadGuy::freeze();
00211   physic.set_velocity_x(0);
00212 }
00213 
00214 void
00215 WalkingBadguy::unfreeze()
00216 {
00217   BadGuy::unfreeze();
00218   WalkingBadguy::initialize();
00219 }
00220 
00221 float
00222 WalkingBadguy::get_velocity_y() const
00223 {
00224   return physic.get_velocity_y();
00225 }
00226 
00227 void
00228 WalkingBadguy::set_velocity_y(float vy)
00229 {
00230   physic.set_velocity_y(vy);
00231 }
00232 
00233 /* EOF */

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