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 = 1600;
33 static const float Y_OFFSCREEN_DISTANCE = 1200;
35 BadGuy::BadGuy(const Vector& pos, const std::string& sprite_name, int layer) :
36 MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED),
39 is_initialized(false),
49 on_ground_flag(false),
51 colgroup_active(COLGROUP_MOVING)
53 start_position = bbox.p1;
55 sound_manager->preload("sounds/squish.wav");
56 sound_manager->preload("sounds/fall.wav");
58 dir = (start_dir == AUTO) ? LEFT : start_dir;
61 BadGuy::BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name, int layer) :
62 MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED),
65 is_initialized(false),
75 on_ground_flag(false),
77 colgroup_active(COLGROUP_MOVING)
79 start_position = bbox.p1;
81 sound_manager->preload("sounds/squish.wav");
82 sound_manager->preload("sounds/fall.wav");
84 dir = (start_dir == AUTO) ? LEFT : start_dir;
87 BadGuy::BadGuy(const Reader& reader, const std::string& sprite_name, int layer) :
88 MovingSprite(reader, sprite_name, layer, COLGROUP_DISABLED),
91 is_initialized(false),
101 on_ground_flag(false),
103 colgroup_active(COLGROUP_MOVING)
105 start_position = bbox.p1;
107 std::string dir_str = "auto";
108 reader.get("direction", dir_str);
109 start_dir = str2dir( dir_str );
112 reader.get("dead-script", dead_script);
114 sound_manager->preload("sounds/squish.wav");
115 sound_manager->preload("sounds/fall.wav");
117 dir = (start_dir == AUTO) ? LEFT : start_dir;
121 BadGuy::draw(DrawingContext& context)
125 if(state == STATE_INIT || state == STATE_INACTIVE)
127 if(state == STATE_FALLING) {
128 DrawingEffect old_effect = context.get_drawing_effect();
129 context.set_drawing_effect((DrawingEffect) (old_effect | VERTICAL_FLIP));
130 sprite->draw(context, get_pos(), layer);
131 context.set_drawing_effect(old_effect);
133 sprite->draw(context, get_pos(), layer);
138 BadGuy::update(float elapsed_time)
140 if(!Sector::current()->inside(bbox)) {
141 is_active_flag = false;
145 if ((state != STATE_INACTIVE) && is_offscreen()) {
146 if (state == STATE_ACTIVE) deactivate();
147 set_state(STATE_INACTIVE);
152 is_active_flag = true;
153 active_update(elapsed_time);
157 is_active_flag = false;
158 inactive_update(elapsed_time);
162 is_active_flag = false;
163 if(state_timer.check()) {
167 movement = physic.get_movement(elapsed_time);
170 is_active_flag = false;
171 movement = physic.get_movement(elapsed_time);
175 on_ground_flag = false;
179 BadGuy::str2dir( std::string dir_str )
181 if( dir_str == "auto" )
183 if( dir_str == "left" )
185 if( dir_str == "right" )
189 log_warning << "Badguy::str2dir: unknown direction \"" << dir_str << "\"" << std::endl;;
209 BadGuy::active_update(float elapsed_time)
211 movement = physic.get_movement(elapsed_time);
215 BadGuy::inactive_update(float )
220 BadGuy::collision_tile(uint32_t tile_attributes)
222 if(tile_attributes & Tile::HURTS) {
223 if (tile_attributes & Tile::FIRE) {
224 if (is_flammable()) ignite();
226 else if (tile_attributes & Tile::ICE) {
227 if (is_freezable()) freeze();
236 BadGuy::collision(GameObject& other, const CollisionHit& hit)
238 if (!is_active()) return ABORT_MOVE;
240 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
241 if(badguy && badguy->is_active() && badguy->get_group() == COLGROUP_MOVING) {
244 if (badguy->get_bbox().p2.y < (bbox.p1.y + 16)) {
245 if(collision_squished(*badguy)) {
250 return collision_badguy(*badguy, hit);
253 Player* player = dynamic_cast<Player*> (&other);
257 if (player->get_bbox().p2.y < (bbox.p1.y + 16)) {
258 if(collision_squished(*player)) {
263 return collision_player(*player, hit);
266 Bullet* bullet = dynamic_cast<Bullet*> (&other);
268 return collision_bullet(*bullet, hit);
274 BadGuy::collision_solid(const CollisionHit& hit)
276 physic.set_velocity_x(0);
277 physic.set_velocity_y(0);
278 update_on_ground_flag(hit);
282 BadGuy::collision_player(Player& player, const CollisionHit& )
284 if(player.is_invincible()) {
296 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
302 BadGuy::collision_squished(GameObject& )
308 BadGuy::collision_bullet(Bullet& bullet, const CollisionHit& hit)
311 if(bullet.get_type() == FIRE_BONUS) {
312 // fire bullet thaws frozen badguys
317 // other bullets ricochet
318 bullet.ricochet(*this, hit);
322 else if (is_ignited()) {
323 if(bullet.get_type() == ICE_BONUS) {
324 // ice bullets extinguish ignited badguys
329 // other bullets are absorbed by ignited badguys
334 else if(bullet.get_type() == FIRE_BONUS && is_flammable()) {
335 // fire bullets ignite flammable badguys
340 else if(bullet.get_type() == ICE_BONUS && is_freezable()) {
341 // ice bullets freeze freezable badguys
347 // in all other cases, bullets ricochet
348 bullet.ricochet(*this, hit);
354 BadGuy::kill_squished(GameObject& object)
356 sound_manager->play("sounds/squish.wav", get_pos());
357 physic.enable_gravity(true);
358 physic.set_velocity_x(0);
359 physic.set_velocity_y(0);
360 set_state(STATE_SQUISHED);
361 set_group(COLGROUP_MOVING_ONLY_STATIC);
362 Player* player = dynamic_cast<Player*>(&object);
364 player->bounce(*this);
374 sound_manager->play("sounds/fall.wav", get_pos());
375 physic.set_velocity_y(0);
376 physic.set_acceleration_y(0);
377 physic.enable_gravity(true);
378 set_state(STATE_FALLING);
385 BadGuy::run_dead_script()
388 Sector::current()->get_level()->stats.badguys++;
393 if(dead_script != "") {
394 std::istringstream stream(dead_script);
395 Sector::current()->run_script(stream, "dead-script");
400 BadGuy::set_state(State state)
402 if(this->state == state)
405 State laststate = this->state;
409 state_timer.start(SQUISH_TIME);
412 set_group(colgroup_active);
413 //bbox.set_pos(start_position);
416 // was the badguy dead anyway?
417 if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
420 set_group(COLGROUP_DISABLED);
423 set_group(COLGROUP_DISABLED);
431 BadGuy::is_offscreen()
433 Player* player = get_nearest_player();
434 if (!player) return false;
435 Vector dist = player->get_bbox().get_middle() - get_bbox().get_middle();
436 // In SuperTux 0.1.x, Badguys were activated when Tux<->Badguy center distance was approx. <= ~668px
437 // This doesn't work for wide-screen monitors which give us a virt. res. of approx. 1066px x 600px
438 if ((fabsf(dist.x) <= X_OFFSCREEN_DISTANCE) && (fabsf(dist.y) <= Y_OFFSCREEN_DISTANCE)) {
445 BadGuy::try_activate()
447 // Don't activate if player is dying
448 Player* player = get_nearest_player();
451 if (!is_offscreen()) {
452 set_state(STATE_ACTIVE);
453 if (!is_initialized) {
455 // if starting direction was set to AUTO, this is our chance to re-orient the badguy
456 if (start_dir == AUTO) {
457 Player* player = get_nearest_player();
458 if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
466 is_initialized = true;
473 BadGuy::might_fall(int height)
475 // make sure we check for at least a 1-pixel fall
480 float y1 = bbox.p2.y + 1;
481 float y2 = bbox.p2.y + 1 + height;
489 return Sector::current()->is_free_of_statics(Rectf(x1, y1, x2, y2));
493 BadGuy::get_nearest_player()
495 // FIXME: does not really return nearest player
497 std::vector<Player*> players = Sector::current()->get_players();
498 for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
499 Player* player = *playerIter;
500 if (player->is_dying() || player->is_dead()) continue;
508 BadGuy::update_on_ground_flag(const CollisionHit& hit)
511 on_ground_flag = true;
512 floor_normal = hit.slope_normal;
519 return on_ground_flag;
525 return is_active_flag;
529 BadGuy::get_floor_normal()
537 set_group(COLGROUP_MOVING_STATIC);
544 set_group(colgroup_active);
549 BadGuy::is_freezable() const
555 BadGuy::is_frozen() const
572 BadGuy::is_flammable() const
578 BadGuy::is_ignited() const
584 BadGuy::set_colgroup_active(CollisionGroup group)
586 this->colgroup_active = group;
587 if (state == STATE_ACTIVE) set_group(group);