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"
28 static const float SQUISH_TIME = 2;
30 static const float X_OFFSCREEN_DISTANCE = 1600;
31 static const float Y_OFFSCREEN_DISTANCE = 1200;
33 BadGuy::BadGuy(const Vector& pos, const std::string& sprite_name, int layer) :
34 MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED),
36 is_initialized(false),
46 on_ground_flag(false),
48 colgroup_active(COLGROUP_MOVING)
50 start_position = bbox.p1;
52 sound_manager->preload("sounds/squish.wav");
53 sound_manager->preload("sounds/fall.wav");
55 dir = (start_dir == AUTO) ? LEFT : start_dir;
58 BadGuy::BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name, int layer) :
59 MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED),
61 is_initialized(false),
71 on_ground_flag(false),
73 colgroup_active(COLGROUP_MOVING)
75 start_position = bbox.p1;
77 sound_manager->preload("sounds/squish.wav");
78 sound_manager->preload("sounds/fall.wav");
80 dir = (start_dir == AUTO) ? LEFT : start_dir;
83 BadGuy::BadGuy(const Reader& reader, const std::string& sprite_name, int layer) :
84 MovingSprite(reader, sprite_name, layer, COLGROUP_DISABLED),
86 is_initialized(false),
96 on_ground_flag(false),
98 colgroup_active(COLGROUP_MOVING)
100 start_position = bbox.p1;
102 std::string dir_str = "auto";
103 reader.get("direction", dir_str);
104 start_dir = str2dir( dir_str );
107 reader.get("dead-script", dead_script);
109 sound_manager->preload("sounds/squish.wav");
110 sound_manager->preload("sounds/fall.wav");
112 dir = (start_dir == AUTO) ? LEFT : start_dir;
116 BadGuy::draw(DrawingContext& context)
120 if(state == STATE_INIT || state == STATE_INACTIVE)
122 if(state == STATE_FALLING) {
123 DrawingEffect old_effect = context.get_drawing_effect();
124 context.set_drawing_effect((DrawingEffect) (old_effect | VERTICAL_FLIP));
125 sprite->draw(context, get_pos(), layer);
126 context.set_drawing_effect(old_effect);
128 sprite->draw(context, get_pos(), layer);
133 BadGuy::update(float elapsed_time)
135 if(!Sector::current()->inside(bbox)) {
136 is_active_flag = false;
140 if ((state != STATE_INACTIVE) && is_offscreen()) {
141 if (state == STATE_ACTIVE) deactivate();
142 set_state(STATE_INACTIVE);
147 is_active_flag = true;
148 active_update(elapsed_time);
152 is_active_flag = false;
153 inactive_update(elapsed_time);
157 is_active_flag = false;
158 if(state_timer.check()) {
162 movement = physic.get_movement(elapsed_time);
165 is_active_flag = false;
166 movement = physic.get_movement(elapsed_time);
170 on_ground_flag = false;
174 BadGuy::str2dir( std::string dir_str )
176 if( dir_str == "auto" )
178 if( dir_str == "left" )
180 if( dir_str == "right" )
184 log_warning << "Badguy::str2dir: unknown direction \"" << dir_str << "\"" << std::endl;;
204 BadGuy::active_update(float elapsed_time)
206 movement = physic.get_movement(elapsed_time);
210 BadGuy::inactive_update(float )
215 BadGuy::collision_tile(uint32_t tile_attributes)
217 if(tile_attributes & Tile::HURTS) {
218 if (tile_attributes & Tile::FIRE) {
219 if (is_flammable()) ignite();
221 else if (tile_attributes & Tile::ICE) {
222 if (is_freezable()) freeze();
231 BadGuy::collision(GameObject& other, const CollisionHit& hit)
233 if (!is_active()) return ABORT_MOVE;
235 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
236 if(badguy && badguy->is_active() && badguy->get_group() == COLGROUP_MOVING) {
239 if (badguy->get_bbox().p2.y < (bbox.p1.y + 16)) {
240 if(collision_squished(*badguy)) {
245 return collision_badguy(*badguy, hit);
248 Player* player = dynamic_cast<Player*> (&other);
252 if (player->get_bbox().p2.y < (bbox.p1.y + 16)) {
253 if(collision_squished(*player)) {
258 return collision_player(*player, hit);
261 Bullet* bullet = dynamic_cast<Bullet*> (&other);
263 return collision_bullet(*bullet, hit);
269 BadGuy::collision_solid(const CollisionHit& hit)
271 physic.set_velocity_x(0);
272 physic.set_velocity_y(0);
273 update_on_ground_flag(hit);
277 BadGuy::collision_player(Player& player, const CollisionHit& )
279 if(player.is_invincible()) {
291 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
297 BadGuy::collision_squished(GameObject& )
303 BadGuy::collision_bullet(Bullet& bullet, const CollisionHit& hit)
306 if(bullet.get_type() == FIRE_BONUS) {
307 // fire bullet thaws frozen badguys
312 // other bullets ricochet
313 bullet.ricochet(*this, hit);
317 else if (is_ignited()) {
318 if(bullet.get_type() == ICE_BONUS) {
319 // ice bullets extinguish ignited badguys
324 // other bullets are absorbed by ignited badguys
329 else if(bullet.get_type() == FIRE_BONUS && is_flammable()) {
330 // fire bullets ignite flammable badguys
335 else if(bullet.get_type() == ICE_BONUS && is_freezable()) {
336 // ice bullets freeze freezable badguys
342 // in all other cases, bullets ricochet
343 bullet.ricochet(*this, hit);
349 BadGuy::kill_squished(GameObject& object)
351 sound_manager->play("sounds/squish.wav", get_pos());
352 physic.enable_gravity(true);
353 physic.set_velocity_x(0);
354 physic.set_velocity_y(0);
355 set_state(STATE_SQUISHED);
356 set_group(COLGROUP_MOVING_ONLY_STATIC);
357 Player* player = dynamic_cast<Player*>(&object);
359 player->bounce(*this);
369 sound_manager->play("sounds/fall.wav", get_pos());
370 physic.set_velocity_y(0);
371 physic.set_acceleration_y(0);
372 physic.enable_gravity(true);
373 set_state(STATE_FALLING);
380 BadGuy::run_dead_script()
383 Sector::current()->get_level()->stats.badguys++;
388 if(dead_script != "") {
389 std::istringstream stream(dead_script);
390 Sector::current()->run_script(stream, "dead-script");
395 BadGuy::set_state(State state)
397 if(this->state == state)
400 State laststate = this->state;
404 state_timer.start(SQUISH_TIME);
407 set_group(colgroup_active);
408 //bbox.set_pos(start_position);
411 // was the badguy dead anyway?
412 if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
415 set_group(COLGROUP_DISABLED);
418 set_group(COLGROUP_DISABLED);
426 BadGuy::is_offscreen()
428 Player* player = get_nearest_player();
429 if (!player) return false;
430 Vector dist = player->get_bbox().get_middle() - get_bbox().get_middle();
431 if ((dist.x <= X_OFFSCREEN_DISTANCE+32) && (dist.y <= Y_OFFSCREEN_DISTANCE+32)) {
438 BadGuy::try_activate()
440 // In SuperTux 0.1.x, Badguys were activated when Tux<->Badguy center distance was approx. <= ~668px
441 // This doesn't work for wide-screen monitors which give us a virt. res. of approx. 1066px x 600px
442 Player* player = get_nearest_player();
444 Vector dist = player->get_bbox().get_middle() - get_bbox().get_middle();
445 if ((fabsf(dist.x) <= X_OFFSCREEN_DISTANCE) && (fabsf(dist.y) <= Y_OFFSCREEN_DISTANCE)) {
446 set_state(STATE_ACTIVE);
447 if (!is_initialized) {
449 // if starting direction was set to AUTO, this is our chance to re-orient the badguy
450 if (start_dir == AUTO) {
451 Player* player = get_nearest_player();
452 if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
460 is_initialized = true;
467 BadGuy::might_fall(int height)
469 // make sure we check for at least a 1-pixel fall
474 float y1 = bbox.p2.y + 1;
475 float y2 = bbox.p2.y + 1 + height;
483 return Sector::current()->is_free_of_statics(Rect(x1, y1, x2, y2));
487 BadGuy::get_nearest_player()
489 // FIXME: does not really return nearest player
491 std::vector<Player*> players = Sector::current()->get_players();
492 for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
493 Player* player = *playerIter;
494 if (player->is_dying() || player->is_dead()) continue;
502 BadGuy::update_on_ground_flag(const CollisionHit& hit)
505 on_ground_flag = true;
506 floor_normal = hit.slope_normal;
513 return on_ground_flag;
519 return is_active_flag;
523 BadGuy::get_floor_normal()
531 set_group(COLGROUP_MOVING_STATIC);
538 set_group(colgroup_active);
543 BadGuy::is_freezable() const
549 BadGuy::is_frozen() const
566 BadGuy::is_flammable() const
572 BadGuy::is_ignited() const
578 BadGuy::set_colgroup_active(CollisionGroup group)
580 this->colgroup_active = group;
581 if (state == STATE_ACTIVE) set_group(group);