4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "audio/sound_manager.hpp"
25 #include "game_session.hpp"
29 #include "object/bullet.hpp"
30 #include "object/camera.hpp"
31 #include "object/particles.hpp"
32 #include "object/player.hpp"
33 #include "object/tilemap.hpp"
34 #include "random_generator.hpp"
36 #include "statistics.hpp"
41 static const float SQUISH_TIME = 2;
43 static const float X_OFFSCREEN_DISTANCE = 1600;
44 static const float Y_OFFSCREEN_DISTANCE = 1200;
46 BadGuy::BadGuy(const Vector& pos, const std::string& sprite_name, int layer)
47 : MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), countMe(true), is_initialized(false),
48 dir(LEFT), start_dir(AUTO), frozen(false), ignited(false),
49 state(STATE_INIT), on_ground_flag(false), colgroup_active(COLGROUP_MOVING)
51 start_position = bbox.p1;
53 sound_manager->preload("sounds/squish.wav");
54 sound_manager->preload("sounds/fall.wav");
56 dir = (start_dir == AUTO) ? LEFT : start_dir;
59 BadGuy::BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name, int layer)
60 : MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), countMe(true), is_initialized(false),
61 dir(direction), start_dir(direction), frozen(false), ignited(false),
62 state(STATE_INIT), on_ground_flag(false), colgroup_active(COLGROUP_MOVING)
64 start_position = bbox.p1;
66 sound_manager->preload("sounds/squish.wav");
67 sound_manager->preload("sounds/fall.wav");
69 dir = (start_dir == AUTO) ? LEFT : start_dir;
72 BadGuy::BadGuy(const lisp::Lisp& reader, const std::string& sprite_name, int layer)
73 : MovingSprite(reader, sprite_name, layer, COLGROUP_DISABLED), countMe(true), is_initialized(false), dir(LEFT), start_dir(AUTO), frozen(false), ignited(false), state(STATE_INIT), on_ground_flag(false), colgroup_active(COLGROUP_MOVING)
75 start_position = bbox.p1;
77 std::string dir_str = "auto";
78 reader.get("direction", dir_str);
79 start_dir = str2dir( dir_str );
82 reader.get("dead-script", dead_script);
84 sound_manager->preload("sounds/squish.wav");
85 sound_manager->preload("sounds/fall.wav");
87 dir = (start_dir == AUTO) ? LEFT : start_dir;
91 BadGuy::draw(DrawingContext& context)
95 if(state == STATE_INIT || state == STATE_INACTIVE)
97 if(state == STATE_FALLING) {
98 DrawingEffect old_effect = context.get_drawing_effect();
99 context.set_drawing_effect((DrawingEffect) (old_effect | VERTICAL_FLIP));
100 sprite->draw(context, get_pos(), layer);
101 context.set_drawing_effect(old_effect);
103 sprite->draw(context, get_pos(), layer);
108 BadGuy::update(float elapsed_time)
110 if(!Sector::current()->inside(bbox)) {
111 is_active_flag = false;
115 if ((state != STATE_INACTIVE) && is_offscreen()) {
116 if (state == STATE_ACTIVE) deactivate();
117 set_state(STATE_INACTIVE);
122 is_active_flag = true;
123 active_update(elapsed_time);
127 is_active_flag = false;
128 inactive_update(elapsed_time);
132 is_active_flag = false;
133 if(state_timer.check()) {
137 movement = physic.get_movement(elapsed_time);
140 is_active_flag = false;
141 movement = physic.get_movement(elapsed_time);
145 on_ground_flag = false;
149 BadGuy::str2dir( std::string dir_str )
151 if( dir_str == "auto" )
153 if( dir_str == "left" )
155 if( dir_str == "right" )
159 log_warning << "Badguy::str2dir: unknown direction \"" << dir_str << "\"" << std::endl;;
179 BadGuy::write(lisp::Writer& )
181 log_warning << "tried to write out a generic badguy" << std::endl;
185 BadGuy::active_update(float elapsed_time)
187 movement = physic.get_movement(elapsed_time);
191 BadGuy::inactive_update(float )
196 BadGuy::collision_tile(uint32_t tile_attributes)
198 if(tile_attributes & Tile::HURTS) {
199 if (tile_attributes & Tile::FIRE) {
200 if (is_flammable()) ignite();
202 else if (tile_attributes & Tile::ICE) {
203 if (is_freezable()) freeze();
212 BadGuy::collision(GameObject& other, const CollisionHit& hit)
214 if (!is_active()) return ABORT_MOVE;
216 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
217 if(badguy && badguy->is_active() && badguy->get_group() == COLGROUP_MOVING) {
220 if (badguy->get_bbox().p2.y < (bbox.p1.y + 16)) {
221 if(collision_squished(*badguy)) {
226 return collision_badguy(*badguy, hit);
229 Player* player = dynamic_cast<Player*> (&other);
233 if (player->get_bbox().p2.y < (bbox.p1.y + 16)) {
234 if(collision_squished(*player)) {
239 return collision_player(*player, hit);
242 Bullet* bullet = dynamic_cast<Bullet*> (&other);
244 return collision_bullet(*bullet, hit);
250 BadGuy::collision_solid(const CollisionHit& hit)
252 physic.set_velocity_x(0);
253 physic.set_velocity_y(0);
254 update_on_ground_flag(hit);
258 BadGuy::collision_player(Player& player, const CollisionHit& )
260 if(player.is_invincible()) {
272 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
278 BadGuy::collision_squished(GameObject& )
284 BadGuy::collision_bullet(Bullet& bullet, const CollisionHit& hit)
287 if(bullet.get_type() == FIRE_BONUS) {
288 // fire bullet thaws frozen badguys
293 // other bullets ricochet
294 bullet.ricochet(*this, hit);
298 else if (is_ignited()) {
299 if(bullet.get_type() == ICE_BONUS) {
300 // ice bullets extinguish ignited badguys
305 // other bullets are absorbed by ignited badguys
310 else if(bullet.get_type() == FIRE_BONUS && is_flammable()) {
311 // fire bullets ignite flammable badguys
316 else if(bullet.get_type() == ICE_BONUS && is_freezable()) {
317 // ice bullets freeze freezable badguys
323 // in all other cases, bullets ricochet
324 bullet.ricochet(*this, hit);
330 BadGuy::kill_squished(GameObject& object)
332 sound_manager->play("sounds/squish.wav", get_pos());
333 physic.enable_gravity(true);
334 physic.set_velocity_x(0);
335 physic.set_velocity_y(0);
336 set_state(STATE_SQUISHED);
337 set_group(COLGROUP_MOVING_ONLY_STATIC);
338 Player* player = dynamic_cast<Player*>(&object);
340 if (countMe) Sector::current()->get_level()->stats.badguys++;
341 player->bounce(*this);
345 if(dead_script != "") {
346 std::istringstream stream(dead_script);
347 Sector::current()->run_script(stream, "dead-script");
354 sound_manager->play("sounds/fall.wav", get_pos());
355 if (countMe) Sector::current()->get_level()->stats.badguys++;
356 physic.set_velocity_y(0);
357 physic.set_acceleration_y(0);
358 physic.enable_gravity(true);
359 set_state(STATE_FALLING);
362 if(dead_script != "") {
363 std::istringstream stream(dead_script);
364 Sector::current()->run_script(stream, "dead-script");
369 BadGuy::run_dead_script()
372 Sector::current()->get_level()->stats.badguys++;
375 if(dead_script != "") {
376 std::istringstream stream(dead_script);
377 Sector::current()->run_script(stream, "dead-script");
382 BadGuy::set_state(State state)
384 if(this->state == state)
387 State laststate = this->state;
391 state_timer.start(SQUISH_TIME);
394 set_group(colgroup_active);
395 //bbox.set_pos(start_position);
398 // was the badguy dead anyway?
399 if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
402 set_group(COLGROUP_DISABLED);
405 set_group(COLGROUP_DISABLED);
413 BadGuy::is_offscreen()
415 Player* player = get_nearest_player();
416 if (!player) return false;
417 Vector dist = player->get_bbox().get_middle() - get_bbox().get_middle();
418 if ((dist.x <= X_OFFSCREEN_DISTANCE+32) && (dist.y <= Y_OFFSCREEN_DISTANCE+32)) {
425 BadGuy::try_activate()
427 // In SuperTux 0.1.x, Badguys were activated when Tux<->Badguy center distance was approx. <= ~668px
428 // This doesn't work for wide-screen monitors which give us a virt. res. of approx. 1066px x 600px
429 Player* player = get_nearest_player();
431 Vector dist = player->get_bbox().get_middle() - get_bbox().get_middle();
432 if ((fabsf(dist.x) <= X_OFFSCREEN_DISTANCE) && (fabsf(dist.y) <= Y_OFFSCREEN_DISTANCE)) {
433 set_state(STATE_ACTIVE);
434 if (!is_initialized) {
436 // if starting direction was set to AUTO, this is our chance to re-orient the badguy
437 if (start_dir == AUTO) {
438 Player* player = get_nearest_player();
439 if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
447 is_initialized = true;
454 BadGuy::might_fall(int height)
456 // make sure we check for at least a 1-pixel fall
461 float y1 = bbox.p2.y + 1;
462 float y2 = bbox.p2.y + 1 + height;
470 return Sector::current()->is_free_of_statics(Rect(x1, y1, x2, y2));
474 BadGuy::get_nearest_player()
476 // FIXME: does not really return nearest player
478 std::vector<Player*> players = Sector::current()->get_players();
479 for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
480 Player* player = *playerIter;
481 if (player->is_dying() || player->is_dead()) continue;
489 BadGuy::update_on_ground_flag(const CollisionHit& hit)
492 on_ground_flag = true;
493 floor_normal = hit.slope_normal;
500 return on_ground_flag;
506 return is_active_flag;
510 BadGuy::get_floor_normal()
518 set_group(COLGROUP_MOVING_STATIC);
525 set_group(colgroup_active);
530 BadGuy::is_freezable() const
536 BadGuy::is_frozen() const
553 BadGuy::is_flammable() const
559 BadGuy::is_ignited() const
565 BadGuy::set_colgroup_active(CollisionGroup group)
567 this->colgroup_active = group;
568 if (state == STATE_ACTIVE) set_group(group);