src/badguy/haywire.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //  Copyright (C) 2010 Florian Forster <supertux at octo.it>
00004 //
00005 //  This program is free software: you can redistribute it and/or modify
00006 //  it under the terms of the GNU General Public License as published by
00007 //  the Free Software Foundation, either version 3 of the License, or
00008 //  (at your option) any later version.
00009 //
00010 //  This program is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //  GNU General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU General Public License
00016 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 #include "audio/sound_manager.hpp"
00019 #include "badguy/bomb.hpp"
00020 #include "badguy/haywire.hpp"
00021 #include "object/explosion.hpp"
00022 #include "object/player.hpp"
00023 #include "sprite/sprite.hpp"
00024 #include "sprite/sprite_manager.hpp"
00025 #include "supertux/object_factory.hpp"
00026 #include "supertux/sector.hpp"
00027 #include "util/reader.hpp"
00028 
00029 #define TIME_EXPLOSION 5.0
00030 #define TIME_STUNNED   0.5
00031 
00032 Haywire::Haywire(const Reader& reader) :
00033   WalkingBadguy(reader, "images/creatures/haywire/haywire.sprite", "left", "right"),
00034   is_exploding(false),
00035   time_until_explosion(0.0f),
00036   is_stunned(false),
00037   time_stunned(0.0f)
00038 {
00039   walk_speed = 80;
00040   max_drop_height = 16;
00041 
00042   //Prevent stutter when Tux jumps on Mr Bomb
00043   sound_manager->preload("sounds/explosion.wav");
00044 
00045   //Check if we need another sprite
00046   if( !reader.get( "sprite", sprite_name ) ){
00047     return;
00048   }
00049   if( sprite_name == "" ){
00050     sprite_name = "images/creatures/haywire/haywire.sprite";
00051     return;
00052   }
00053   //Replace sprite
00054   sprite = sprite_manager->create( sprite_name );
00055 }
00056 
00057 /* Haywire created by a dispenser always gets default sprite atm.*/
00058 Haywire::Haywire(const Vector& pos, Direction d) :
00059   WalkingBadguy(pos, d, "images/creatures/haywire/haywire.sprite", "left", "right"),
00060   is_exploding(false),
00061   time_until_explosion(0.0f),
00062   is_stunned(false),
00063   time_stunned(0.0f)
00064 {
00065   walk_speed = 80;
00066   max_drop_height = 16;
00067   sound_manager->preload("sounds/explosion.wav");
00068 }
00069 
00070 HitResponse
00071 Haywire::collision(GameObject& object, const CollisionHit& hit)
00072 {
00073   return WalkingBadguy::collision(object, hit);
00074 }
00075 
00076 HitResponse
00077 Haywire::collision_player(Player& player, const CollisionHit& hit)
00078 {
00079   return WalkingBadguy::collision_player(player, hit);
00080 }
00081 
00082 bool
00083 Haywire::collision_squished(GameObject& object)
00084 {
00085   Player* player = dynamic_cast<Player*>(&object);
00086   if (player && player->is_invincible()) {
00087     player->bounce (*this);
00088     kill_fall();
00089     return true;
00090   }
00091 
00092   if (is_stunned) {
00093     if (player)
00094       player->bounce (*this);
00095     return true;
00096   }
00097 
00098   if (!is_exploding) {
00099     set_action ((dir == LEFT) ? "ticking-left" : "ticking-right", /* loops = */ -1);
00100     walk_left_action = "ticking-left";
00101     walk_right_action = "ticking-right";
00102     set_walk_speed (160);
00103     time_until_explosion = TIME_EXPLOSION;
00104     is_exploding = true;
00105   }
00106 
00107   time_stunned = TIME_STUNNED;
00108   is_stunned = true;
00109   physic.set_velocity_x (0.0);
00110   physic.set_acceleration_x (0.0);
00111 
00112   if (player)
00113     player->bounce (*this);
00114 
00115   return true;
00116 }
00117 
00118 void
00119 Haywire::active_update(float elapsed_time)
00120 {
00121   if (is_exploding) {
00122     if (elapsed_time >= time_until_explosion) {
00123       kill_fall ();
00124       return;
00125     }
00126     else
00127       time_until_explosion -= elapsed_time;
00128   }
00129 
00130   if (is_stunned) {
00131     if (time_stunned > elapsed_time) {
00132       time_stunned -= elapsed_time;
00133       return;
00134     }
00135     else { /* if (time_stunned <= elapsed_time) */
00136       elapsed_time -= time_stunned;
00137       time_stunned = 0.0;
00138       is_stunned = false;
00139     }
00140   }
00141 
00142   if (is_exploding) {
00143     Player *p = this->get_nearest_player ();
00144     float target_velocity = 0.0;
00145 
00146     if (p) {
00147       /* Player is on the right */
00148       if (p->get_pos ().x > this->get_pos ().x)
00149         target_velocity = walk_speed;
00150       else /* player in on the left */
00151         target_velocity = (-1.0) * walk_speed;
00152     } /* if (player) */
00153 
00154     WalkingBadguy::active_update(elapsed_time, target_velocity);
00155   }
00156   else {
00157     WalkingBadguy::active_update(elapsed_time);
00158   }
00159 }
00160 
00161 void
00162 Haywire::kill_fall()
00163 {
00164   if(is_valid()) {
00165     remove_me();
00166     Explosion* explosion = new Explosion(get_bbox().get_middle());
00167     Sector::current()->add_object(explosion);
00168   }
00169 
00170   run_dead_script();
00171 }
00172 
00173 void
00174 Haywire::freeze()
00175 {
00176   WalkingBadguy::freeze();
00177   sprite->set_action(dir == LEFT ? "iced-left" : "iced-right");
00178 }
00179 
00180 bool
00181 Haywire::is_freezable() const
00182 {
00183   return true;
00184 }
00185 
00186 /* vim: set sw=2 sts=2 et : */
00187 /* EOF */

Generated on Mon Apr 14 03:38:18 2014 for SuperTux by  doxygen 1.5.1