2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "badguy/badguy.hpp"
19 #include "audio/sound_manager.hpp"
20 #include "object/bullet.hpp"
21 #include "object/player.hpp"
22 #include "supertux/level.hpp"
23 #include "supertux/sector.hpp"
24 #include "supertux/tile.hpp"
25 #include "util/reader.hpp"
30 static const float SQUISH_TIME = 2;
32 static const float X_OFFSCREEN_DISTANCE = 1280;
33 static const float Y_OFFSCREEN_DISTANCE = 800;
34 static const int LAYER_FALLING = 500;
36 BadGuy::BadGuy(const Vector& pos, const std::string& sprite_name, int layer) :
37 MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED),
40 is_initialized(false),
50 on_ground_flag(false),
52 colgroup_active(COLGROUP_MOVING)
54 start_position = bbox.p1;
56 sound_manager->preload("sounds/squish.wav");
57 sound_manager->preload("sounds/fall.wav");
59 dir = (start_dir == AUTO) ? LEFT : start_dir;
62 BadGuy::BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name, int layer) :
63 MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED),
66 is_initialized(false),
76 on_ground_flag(false),
78 colgroup_active(COLGROUP_MOVING)
80 start_position = bbox.p1;
82 sound_manager->preload("sounds/squish.wav");
83 sound_manager->preload("sounds/fall.wav");
85 dir = (start_dir == AUTO) ? LEFT : start_dir;
88 BadGuy::BadGuy(const Reader& reader, const std::string& sprite_name, int layer) :
89 MovingSprite(reader, sprite_name, layer, COLGROUP_DISABLED),
92 is_initialized(false),
102 on_ground_flag(false),
104 colgroup_active(COLGROUP_MOVING)
106 start_position = bbox.p1;
108 std::string dir_str = "auto";
109 reader.get("direction", dir_str);
110 start_dir = str2dir( dir_str );
113 reader.get("dead-script", dead_script);
115 sound_manager->preload("sounds/squish.wav");
116 sound_manager->preload("sounds/fall.wav");
118 dir = (start_dir == AUTO) ? LEFT : start_dir;
122 BadGuy::draw(DrawingContext& context)
126 if(state == STATE_INIT || state == STATE_INACTIVE)
128 if(state == STATE_FALLING) {
129 DrawingEffect old_effect = context.get_drawing_effect();
130 context.set_drawing_effect((DrawingEffect) (old_effect | VERTICAL_FLIP));
131 sprite->draw(context, get_pos(), layer);
132 context.set_drawing_effect(old_effect);
134 sprite->draw(context, get_pos(), layer);
139 BadGuy::update(float elapsed_time)
141 if(!Sector::current()->inside(bbox)) {
142 is_active_flag = false;
145 // get badguy name from sprite_name ignoring path and extension
146 std::string badguy = sprite_name.substr(0, sprite_name.length() - 7);
147 int path_chars = badguy.rfind("/",badguy.length());
148 badguy = badguy.substr(path_chars + 1, badguy.length() - path_chars);
149 // log warning since badguys_killed can no longer reach total_badguys
150 std::string current_level = "[" + Sector::current()->get_level()->author + " - " + Sector::current()->get_level()->name + "] ";
151 log_warning << current_level << "Counted badguy " << badguy << " starting at " << start_position << " has left the sector" <<std::endl;;
155 if ((state != STATE_INACTIVE) && is_offscreen()) {
156 if (state == STATE_ACTIVE) deactivate();
157 set_state(STATE_INACTIVE);
162 is_active_flag = true;
163 active_update(elapsed_time);
167 is_active_flag = false;
168 inactive_update(elapsed_time);
172 is_active_flag = false;
173 if(state_timer.check()) {
177 movement = physic.get_movement(elapsed_time);
180 is_active_flag = false;
181 movement = physic.get_movement(elapsed_time);
185 on_ground_flag = false;
189 BadGuy::str2dir( std::string dir_str )
191 if( dir_str == "auto" )
193 if( dir_str == "left" )
195 if( dir_str == "right" )
199 log_warning << "Badguy::str2dir: unknown direction \"" << dir_str << "\"" << std::endl;;
219 BadGuy::active_update(float elapsed_time)
221 movement = physic.get_movement(elapsed_time);
225 BadGuy::inactive_update(float )
230 BadGuy::collision_tile(uint32_t tile_attributes)
232 // Don't kill badguys that have already been killed
233 if (!is_active()) return;
235 if(tile_attributes & Tile::HURTS) {
236 if (tile_attributes & Tile::FIRE) {
237 if (is_flammable()) ignite();
239 else if (tile_attributes & Tile::ICE) {
240 if (is_freezable()) freeze();
249 BadGuy::collision(GameObject& other, const CollisionHit& hit)
251 if (!is_active()) return ABORT_MOVE;
253 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
254 if(badguy && badguy->is_active() && badguy->get_group() == COLGROUP_MOVING) {
256 /* Badguys don't let badguys squish other badguys. It's bad. */
259 if (badguy->get_bbox().p2.y < (bbox.p1.y + 16)) {
260 if(collision_squished(*badguy)) {
266 return collision_badguy(*badguy, hit);
269 Player* player = dynamic_cast<Player*> (&other);
273 if (player->get_bbox().p2.y < (bbox.p1.y + 16)) {
274 if(collision_squished(*player)) {
279 return collision_player(*player, hit);
282 Bullet* bullet = dynamic_cast<Bullet*> (&other);
284 return collision_bullet(*bullet, hit);
290 BadGuy::collision_solid(const CollisionHit& hit)
292 physic.set_velocity_x(0);
293 physic.set_velocity_y(0);
294 update_on_ground_flag(hit);
298 BadGuy::collision_player(Player& player, const CollisionHit& )
300 if(player.is_invincible()) {
312 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
318 BadGuy::collision_squished(GameObject& )
324 BadGuy::collision_bullet(Bullet& bullet, const CollisionHit& hit)
327 if(bullet.get_type() == FIRE_BONUS) {
328 // fire bullet thaws frozen badguys
333 // other bullets ricochet
334 bullet.ricochet(*this, hit);
338 else if (is_ignited()) {
339 if(bullet.get_type() == ICE_BONUS) {
340 // ice bullets extinguish ignited badguys
345 // other bullets are absorbed by ignited badguys
350 else if(bullet.get_type() == FIRE_BONUS && is_flammable()) {
351 // fire bullets ignite flammable badguys
356 else if(bullet.get_type() == ICE_BONUS && is_freezable()) {
357 // ice bullets freeze freezable badguys
363 // in all other cases, bullets ricochet
364 bullet.ricochet(*this, hit);
370 BadGuy::kill_squished(GameObject& object)
372 if (!is_active()) return;
374 sound_manager->play("sounds/squish.wav", get_pos());
375 physic.enable_gravity(true);
376 physic.set_velocity_x(0);
377 physic.set_velocity_y(0);
378 set_state(STATE_SQUISHED);
379 set_group(COLGROUP_MOVING_ONLY_STATIC);
380 Player* player = dynamic_cast<Player*>(&object);
382 player->bounce(*this);
392 if (!is_active()) return;
394 sound_manager->play("sounds/fall.wav", get_pos());
395 physic.set_velocity_y(0);
396 physic.set_acceleration_y(0);
397 physic.enable_gravity(true);
398 set_state(STATE_FALLING);
399 layer = LAYER_FALLING;
406 BadGuy::run_dead_script()
409 Sector::current()->get_level()->stats.badguys++;
414 if(dead_script != "") {
415 std::istringstream stream(dead_script);
416 Sector::current()->run_script(stream, "dead-script");
421 BadGuy::set_state(State state)
423 if(this->state == state)
426 State laststate = this->state;
430 state_timer.start(SQUISH_TIME);
433 set_group(colgroup_active);
434 //bbox.set_pos(start_position);
437 // was the badguy dead anyway?
438 if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
441 set_group(COLGROUP_DISABLED);
444 set_group(COLGROUP_DISABLED);
452 BadGuy::is_offscreen()
454 Player* player = get_nearest_player();
455 if (!player) return false;
456 Vector dist = player->get_bbox().get_middle() - get_bbox().get_middle();
457 // In SuperTux 0.1.x, Badguys were activated when Tux<->Badguy center distance was approx. <= ~668px
458 // This doesn't work for wide-screen monitors which give us a virt. res. of approx. 1066px x 600px
459 if ((fabsf(dist.x) <= X_OFFSCREEN_DISTANCE) && (fabsf(dist.y) <= Y_OFFSCREEN_DISTANCE)) {
466 BadGuy::try_activate()
468 // Don't activate if player is dying
469 Player* player = get_nearest_player();
472 if (!is_offscreen()) {
473 set_state(STATE_ACTIVE);
474 if (!is_initialized) {
476 // if starting direction was set to AUTO, this is our chance to re-orient the badguy
477 if (start_dir == AUTO) {
478 Player* player = get_nearest_player();
479 if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
487 is_initialized = true;
494 BadGuy::might_fall(int height)
496 // make sure we check for at least a 1-pixel fall
501 float y1 = bbox.p2.y + 1;
502 float y2 = bbox.p2.y + 1 + height;
510 return Sector::current()->is_free_of_statics(Rectf(x1, y1, x2, y2));
514 BadGuy::get_nearest_player()
516 return Sector::current()->get_nearest_player (this->get_bbox ());
520 BadGuy::update_on_ground_flag(const CollisionHit& hit)
523 on_ground_flag = true;
524 floor_normal = hit.slope_normal;
531 return on_ground_flag;
537 return is_active_flag;
541 BadGuy::get_floor_normal()
549 set_group(COLGROUP_MOVING_STATIC);
556 set_group(colgroup_active);
561 BadGuy::is_freezable() const
567 BadGuy::is_frozen() const
584 BadGuy::is_flammable() const
590 BadGuy::is_ignited() const
596 BadGuy::set_colgroup_active(CollisionGroup group)
598 this->colgroup_active = group;
599 if (state == STATE_ACTIVE) set_group(group);