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"
21 #include "audio/sound_manager.hpp"
22 #include "object/bullet.hpp"
23 #include "object/player.hpp"
24 #include "supertux/level.hpp"
25 #include "supertux/sector.hpp"
26 #include "supertux/tile.hpp"
27 #include "util/reader.hpp"
29 static const float SQUISH_TIME = 2;
31 static const float X_OFFSCREEN_DISTANCE = 1600;
32 static const float Y_OFFSCREEN_DISTANCE = 1200;
34 BadGuy::BadGuy(const Vector& pos, const std::string& sprite_name, int layer) :
35 MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED),
38 is_initialized(false),
48 on_ground_flag(false),
50 colgroup_active(COLGROUP_MOVING)
52 start_position = bbox.p1;
54 sound_manager->preload("sounds/squish.wav");
55 sound_manager->preload("sounds/fall.wav");
57 dir = (start_dir == AUTO) ? LEFT : start_dir;
60 BadGuy::BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name, int layer) :
61 MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED),
64 is_initialized(false),
74 on_ground_flag(false),
76 colgroup_active(COLGROUP_MOVING)
78 start_position = bbox.p1;
80 sound_manager->preload("sounds/squish.wav");
81 sound_manager->preload("sounds/fall.wav");
83 dir = (start_dir == AUTO) ? LEFT : start_dir;
86 BadGuy::BadGuy(const Reader& reader, const std::string& sprite_name, int layer) :
87 MovingSprite(reader, sprite_name, layer, COLGROUP_DISABLED),
90 is_initialized(false),
100 on_ground_flag(false),
102 colgroup_active(COLGROUP_MOVING)
104 start_position = bbox.p1;
106 std::string dir_str = "auto";
107 reader.get("direction", dir_str);
108 start_dir = str2dir( dir_str );
111 reader.get("dead-script", dead_script);
113 sound_manager->preload("sounds/squish.wav");
114 sound_manager->preload("sounds/fall.wav");
116 dir = (start_dir == AUTO) ? LEFT : start_dir;
120 BadGuy::draw(DrawingContext& context)
124 if(state == STATE_INIT || state == STATE_INACTIVE)
126 if(state == STATE_FALLING) {
127 DrawingEffect old_effect = context.get_drawing_effect();
128 context.set_drawing_effect((DrawingEffect) (old_effect | VERTICAL_FLIP));
129 sprite->draw(context, get_pos(), layer);
130 context.set_drawing_effect(old_effect);
132 sprite->draw(context, get_pos(), layer);
137 BadGuy::update(float elapsed_time)
139 if(!Sector::current()->inside(bbox)) {
140 is_active_flag = false;
144 if ((state != STATE_INACTIVE) && is_offscreen()) {
145 if (state == STATE_ACTIVE) deactivate();
146 set_state(STATE_INACTIVE);
151 is_active_flag = true;
152 active_update(elapsed_time);
156 is_active_flag = false;
157 inactive_update(elapsed_time);
161 is_active_flag = false;
162 if(state_timer.check()) {
166 movement = physic.get_movement(elapsed_time);
169 is_active_flag = false;
170 movement = physic.get_movement(elapsed_time);
174 on_ground_flag = false;
178 BadGuy::str2dir( std::string dir_str )
180 if( dir_str == "auto" )
182 if( dir_str == "left" )
184 if( dir_str == "right" )
188 log_warning << "Badguy::str2dir: unknown direction \"" << dir_str << "\"" << std::endl;;
208 BadGuy::active_update(float elapsed_time)
210 movement = physic.get_movement(elapsed_time);
214 BadGuy::inactive_update(float )
219 BadGuy::collision_tile(uint32_t tile_attributes)
221 if(tile_attributes & Tile::HURTS) {
222 if (tile_attributes & Tile::FIRE) {
223 if (is_flammable()) ignite();
225 else if (tile_attributes & Tile::ICE) {
226 if (is_freezable()) freeze();
235 BadGuy::collision(GameObject& other, const CollisionHit& hit)
237 if (!is_active()) return ABORT_MOVE;
239 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
240 if(badguy && badguy->is_active() && badguy->get_group() == COLGROUP_MOVING) {
243 if (badguy->get_bbox().p2.y < (bbox.p1.y + 16)) {
244 if(collision_squished(*badguy)) {
249 return collision_badguy(*badguy, hit);
252 Player* player = dynamic_cast<Player*> (&other);
256 if (player->get_bbox().p2.y < (bbox.p1.y + 16)) {
257 if(collision_squished(*player)) {
262 return collision_player(*player, hit);
265 Bullet* bullet = dynamic_cast<Bullet*> (&other);
267 return collision_bullet(*bullet, hit);
273 BadGuy::collision_solid(const CollisionHit& hit)
275 physic.set_velocity_x(0);
276 physic.set_velocity_y(0);
277 update_on_ground_flag(hit);
281 BadGuy::collision_player(Player& player, const CollisionHit& )
283 if(player.is_invincible()) {
295 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
301 BadGuy::collision_squished(GameObject& )
307 BadGuy::collision_bullet(Bullet& bullet, const CollisionHit& hit)
310 if(bullet.get_type() == FIRE_BONUS) {
311 // fire bullet thaws frozen badguys
316 // other bullets ricochet
317 bullet.ricochet(*this, hit);
321 else if (is_ignited()) {
322 if(bullet.get_type() == ICE_BONUS) {
323 // ice bullets extinguish ignited badguys
328 // other bullets are absorbed by ignited badguys
333 else if(bullet.get_type() == FIRE_BONUS && is_flammable()) {
334 // fire bullets ignite flammable badguys
339 else if(bullet.get_type() == ICE_BONUS && is_freezable()) {
340 // ice bullets freeze freezable badguys
346 // in all other cases, bullets ricochet
347 bullet.ricochet(*this, hit);
353 BadGuy::kill_squished(GameObject& object)
355 sound_manager->play("sounds/squish.wav", get_pos());
356 physic.enable_gravity(true);
357 physic.set_velocity_x(0);
358 physic.set_velocity_y(0);
359 set_state(STATE_SQUISHED);
360 set_group(COLGROUP_MOVING_ONLY_STATIC);
361 Player* player = dynamic_cast<Player*>(&object);
363 player->bounce(*this);
373 sound_manager->play("sounds/fall.wav", get_pos());
374 physic.set_velocity_y(0);
375 physic.set_acceleration_y(0);
376 physic.enable_gravity(true);
377 set_state(STATE_FALLING);
384 BadGuy::run_dead_script()
387 Sector::current()->get_level()->stats.badguys++;
392 if(dead_script != "") {
393 std::istringstream stream(dead_script);
394 Sector::current()->run_script(stream, "dead-script");
399 BadGuy::set_state(State state)
401 if(this->state == state)
404 State laststate = this->state;
408 state_timer.start(SQUISH_TIME);
411 set_group(colgroup_active);
412 //bbox.set_pos(start_position);
415 // was the badguy dead anyway?
416 if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
419 set_group(COLGROUP_DISABLED);
422 set_group(COLGROUP_DISABLED);
430 BadGuy::is_offscreen()
432 Player* player = get_nearest_player();
433 if (!player) return false;
434 Vector dist = player->get_bbox().get_middle() - get_bbox().get_middle();
435 if ((dist.x <= X_OFFSCREEN_DISTANCE+32) && (dist.y <= Y_OFFSCREEN_DISTANCE+32)) {
442 BadGuy::try_activate()
444 // In SuperTux 0.1.x, Badguys were activated when Tux<->Badguy center distance was approx. <= ~668px
445 // This doesn't work for wide-screen monitors which give us a virt. res. of approx. 1066px x 600px
446 Player* player = get_nearest_player();
448 Vector dist = player->get_bbox().get_middle() - get_bbox().get_middle();
449 if ((fabsf(dist.x) <= X_OFFSCREEN_DISTANCE) && (fabsf(dist.y) <= Y_OFFSCREEN_DISTANCE)) {
450 set_state(STATE_ACTIVE);
451 if (!is_initialized) {
453 // if starting direction was set to AUTO, this is our chance to re-orient the badguy
454 if (start_dir == AUTO) {
455 Player* player = get_nearest_player();
456 if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
464 is_initialized = true;
471 BadGuy::might_fall(int height)
473 // make sure we check for at least a 1-pixel fall
478 float y1 = bbox.p2.y + 1;
479 float y2 = bbox.p2.y + 1 + height;
487 return Sector::current()->is_free_of_statics(Rect(x1, y1, x2, y2));
491 BadGuy::get_nearest_player()
493 // FIXME: does not really return nearest player
495 std::vector<Player*> players = Sector::current()->get_players();
496 for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
497 Player* player = *playerIter;
498 if (player->is_dying() || player->is_dead()) continue;
506 BadGuy::update_on_ground_flag(const CollisionHit& hit)
509 on_ground_flag = true;
510 floor_normal = hit.slope_normal;
517 return on_ground_flag;
523 return is_active_flag;
527 BadGuy::get_floor_normal()
535 set_group(COLGROUP_MOVING_STATIC);
542 set_group(colgroup_active);
547 BadGuy::is_freezable() const
553 BadGuy::is_frozen() const
570 BadGuy::is_flammable() const
576 BadGuy::is_ignited() const
582 BadGuy::set_colgroup_active(CollisionGroup group)
584 this->colgroup_active = group;
585 if (state == STATE_ACTIVE) set_group(group);