00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00043 sound_manager->preload("sounds/explosion.wav");
00044
00045
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
00054 sprite = sprite_manager->create( sprite_name );
00055 }
00056
00057
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", -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 {
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
00148 if (p->get_pos ().x > this->get_pos ().x)
00149 target_velocity = walk_speed;
00150 else
00151 target_velocity = (-1.0) * walk_speed;
00152 }
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
00187