b886b147b232373dd01897e89e0d0358b3497ab2
[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 HitResponse
58 Haywire::collision(GameObject& object, const CollisionHit& hit)
59 {
60   return WalkingBadguy::collision(object, hit);
61 }
62
63 HitResponse
64 Haywire::collision_player(Player& player, const CollisionHit& hit)
65 {
66   return WalkingBadguy::collision_player(player, hit);
67 }
68
69 bool
70 Haywire::collision_squished(GameObject& object)
71 {
72   Player* player = dynamic_cast<Player*>(&object);
73   if (player && player->is_invincible()) {
74     player->bounce (*this);
75     kill_fall();
76     return true;
77   }
78
79   if (is_stunned) {
80     if (player)
81       player->bounce (*this);
82     return true;
83   }
84
85   if (!is_exploding) {
86     set_action ((dir == LEFT) ? "ticking-left" : "ticking-right", /* loops = */ -1);
87     walk_left_action = "ticking-left";
88     walk_right_action = "ticking-right";
89     set_walk_speed (160);
90     time_until_explosion = TIME_EXPLOSION;
91     is_exploding = true;
92
93     ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav"));
94     ticking->set_position(get_pos());
95     ticking->set_looping(true);
96     ticking->set_reference_distance(32);
97     ticking->play();
98     grunting.reset(sound_manager->create_sound_source("sounds/grunts.ogg"));
99     grunting->set_position(get_pos());
100     grunting->set_looping(true);
101     grunting->set_reference_distance(32);
102     grunting->play();    
103   }
104
105   time_stunned = TIME_STUNNED;
106   is_stunned = true;
107   physic.set_velocity_x (0.0);
108   physic.set_acceleration_x (0.0);
109
110   if (player)
111     player->bounce (*this);
112
113   return true;
114 }
115
116 void
117 Haywire::active_update(float elapsed_time)
118 {
119   if (is_exploding) {
120     ticking->set_position(get_pos());
121     grunting->set_position(get_pos());
122     if (elapsed_time >= time_until_explosion) {
123       kill_fall ();
124       return;
125     }
126     else
127       time_until_explosion -= elapsed_time;
128   }
129
130   if (is_stunned) {
131     if (time_stunned > elapsed_time) {
132       time_stunned -= elapsed_time;
133       return;
134     }
135     else { /* if (time_stunned <= elapsed_time) */
136       elapsed_time -= time_stunned;
137       time_stunned = 0.0;
138       is_stunned = false;
139     }
140   }
141
142   if (is_exploding) {
143     Player *p = this->get_nearest_player ();
144     float target_velocity = 0.0;
145
146     if (p) {
147       /* Player is on the right */
148       if (p->get_pos ().x > this->get_pos ().x)
149         target_velocity = walk_speed;
150       else /* player in on the left */
151         target_velocity = (-1.0) * walk_speed;
152     } /* if (player) */
153
154     WalkingBadguy::active_update(elapsed_time, target_velocity);
155   }
156   else {
157     WalkingBadguy::active_update(elapsed_time);
158   }
159 }
160
161 void
162 Haywire::kill_fall()
163 {
164   if(is_exploding) {
165     ticking->stop();
166     grunting->stop();
167   }
168   if(is_valid()) {
169     remove_me();
170     Explosion* explosion = new Explosion(get_bbox().get_middle());
171     Sector::current()->add_object(explosion);
172   }
173
174   run_dead_script();
175 }
176
177 void
178 Haywire::freeze()
179 {
180   WalkingBadguy::freeze();
181   sprite->set_action(dir == LEFT ? "iced-left" : "iced-right");
182 }
183
184 bool
185 Haywire::is_freezable() const
186 {
187   return true;
188 }
189
190 /* vim: set sw=2 sts=2 et : */
191 /* EOF */