Compiler warning fix, init variables to 0
[supertux.git] / src / badguy / haywire.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //  Copyright (C) 2010 Florian Forster <supertux at octo.it>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "audio/sound_manager.hpp"
19 #include "badguy/bomb.hpp"
20 #include "badguy/haywire.hpp"
21 #include "object/explosion.hpp"
22 #include "object/player.hpp"
23 #include "sprite/sprite.hpp"
24 #include "sprite/sprite_manager.hpp"
25 #include "supertux/object_factory.hpp"
26 #include "supertux/sector.hpp"
27 #include "util/reader.hpp"
28
29 #define TIME_EXPLOSION 5.0
30 #define TIME_STUNNED   0.5
31
32 Haywire::Haywire(const Reader& reader) :
33   WalkingBadguy(reader, "images/creatures/haywire/haywire.sprite", "left", "right"),
34   is_exploding(false),
35   time_until_explosion(0.0f),
36   is_stunned(false),
37   time_stunned(0.0f)
38 {
39   walk_speed = 80;
40   max_drop_height = 16;
41
42   //Prevent stutter when Tux jumps on Mr Bomb
43   sound_manager->preload("sounds/explosion.wav");
44
45   //Check if we need another sprite
46   if( !reader.get( "sprite", sprite_name ) ){
47     return;
48   }
49   if( sprite_name == "" ){
50     sprite_name = "images/creatures/haywire/haywire.sprite";
51     return;
52   }
53   //Replace sprite
54   sprite = sprite_manager->create( sprite_name );
55 }
56
57 /* Haywire created by a dispenser always gets default sprite atm.*/
58 Haywire::Haywire(const Vector& pos, Direction d) :
59   WalkingBadguy(pos, d, "images/creatures/haywire/haywire.sprite", "left", "right"),
60   is_exploding(false),
61   time_until_explosion(0.0f),
62   is_stunned(false),
63   time_stunned(0.0f)
64 {
65   walk_speed = 80;
66   max_drop_height = 16;
67   sound_manager->preload("sounds/explosion.wav");
68 }
69
70 HitResponse
71 Haywire::collision(GameObject& object, const CollisionHit& hit)
72 {
73   return WalkingBadguy::collision(object, hit);
74 }
75
76 HitResponse
77 Haywire::collision_player(Player& player, const CollisionHit& hit)
78 {
79   return WalkingBadguy::collision_player(player, hit);
80 }
81
82 bool
83 Haywire::collision_squished(GameObject& object)
84 {
85   Player* player = dynamic_cast<Player*>(&object);
86   if (player && player->is_invincible()) {
87     player->bounce (*this);
88     kill_fall();
89     return true;
90   }
91
92   if (is_stunned) {
93     player->bounce (*this);
94     return true;
95   }
96
97   if (!is_exploding) {
98     set_action ((dir == LEFT) ? "ticking-left" : "ticking-right", /* loops = */ -1);
99     walk_left_action = "ticking-left";
100     walk_right_action = "ticking-right";
101     set_walk_speed (160);
102     time_until_explosion = TIME_EXPLOSION;
103     is_exploding = true;
104   }
105
106   time_stunned = TIME_STUNNED;
107   is_stunned = true;
108
109   player->bounce (*this);
110   return true;
111 }
112
113 void
114 Haywire::active_update(float elapsed_time)
115 {
116   if (is_exploding) {
117     if (elapsed_time >= time_until_explosion) {
118       kill_fall ();
119       return;
120     }
121     else
122       time_until_explosion -= elapsed_time;
123   }
124
125   if (is_stunned) {
126     if (time_stunned > elapsed_time) {
127       time_stunned -= elapsed_time;
128       return;
129     }
130     else { /* if (time_stunned <= elapsed_time) */
131       elapsed_time -= time_stunned;
132       time_stunned = 0.0;
133       is_stunned = false;
134     }
135   }
136
137   if (is_exploding && !turn_around_timer.started()) {
138     Player *p = this->get_nearest_player ();
139
140     if (p) {
141       Direction player_dir = LEFT;
142
143       if (p->get_pos ().x > this->get_pos ().x)
144         player_dir = RIGHT;
145
146       if (player_dir != dir)
147         turn_around ();
148     }
149   }
150
151   WalkingBadguy::active_update(elapsed_time);
152 }
153
154 void
155 Haywire::kill_fall()
156 {
157   if(is_valid()) {
158     remove_me();
159     Explosion* explosion = new Explosion(get_bbox().get_middle());
160     Sector::current()->add_object(explosion);
161   }
162
163   run_dead_script();
164 }
165
166 void
167 Haywire::freeze()
168 {
169   WalkingBadguy::freeze();
170   sprite->set_action(dir == LEFT ? "iced-left" : "iced-right");
171 }
172
173 bool
174 Haywire::is_freezable() const
175 {
176   return true;
177 }
178
179 /* vim: set sw=2 sts=2 et : */
180 /* EOF */