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.
21 #include "object/camera.hpp"
22 #include "object/tilemap.hpp"
24 #include "statistics.hpp"
25 #include "game_session.hpp"
28 #include "object/bullet.hpp"
30 #include "object/particles.hpp"
31 #include "random_generator.hpp"
33 static const float SQUISH_TIME = 2;
35 static const float X_OFFSCREEN_DISTANCE = 1600;
36 static const float Y_OFFSCREEN_DISTANCE = 1200;
38 BadGuy::BadGuy(const Vector& pos, const std::string& sprite_name, int layer)
39 : MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), countMe(true), is_initialized(false),
40 dir(LEFT), start_dir(AUTO), frozen(false), ignited(false),
41 state(STATE_INIT), on_ground_flag(false), colgroup_active(COLGROUP_MOVING)
43 start_position = bbox.p1;
45 sound_manager->preload("sounds/squish.wav");
46 sound_manager->preload("sounds/fall.wav");
48 dir = (start_dir == AUTO) ? LEFT : start_dir;
51 BadGuy::BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name, int layer)
52 : MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), countMe(true), is_initialized(false),
53 dir(direction), start_dir(direction), frozen(false), ignited(false),
54 state(STATE_INIT), on_ground_flag(false), colgroup_active(COLGROUP_MOVING)
56 start_position = bbox.p1;
58 sound_manager->preload("sounds/squish.wav");
59 sound_manager->preload("sounds/fall.wav");
61 dir = (start_dir == AUTO) ? LEFT : start_dir;
64 BadGuy::BadGuy(const lisp::Lisp& reader, const std::string& sprite_name, int layer)
65 : 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)
67 start_position = bbox.p1;
69 std::string dir_str = "auto";
70 reader.get("direction", dir_str);
71 start_dir = str2dir( dir_str );
74 reader.get("dead-script", dead_script);
76 sound_manager->preload("sounds/squish.wav");
77 sound_manager->preload("sounds/fall.wav");
79 dir = (start_dir == AUTO) ? LEFT : start_dir;
83 BadGuy::draw(DrawingContext& context)
87 if(state == STATE_INIT || state == STATE_INACTIVE)
89 if(state == STATE_FALLING) {
90 DrawingEffect old_effect = context.get_drawing_effect();
91 context.set_drawing_effect((DrawingEffect) (old_effect | VERTICAL_FLIP));
92 sprite->draw(context, get_pos(), layer);
93 context.set_drawing_effect(old_effect);
95 sprite->draw(context, get_pos(), layer);
100 BadGuy::update(float elapsed_time)
102 if(!Sector::current()->inside(bbox)) {
103 is_active_flag = false;
107 if ((state != STATE_INACTIVE) && is_offscreen()) {
108 if (state == STATE_ACTIVE) deactivate();
109 set_state(STATE_INACTIVE);
114 is_active_flag = true;
115 active_update(elapsed_time);
119 is_active_flag = false;
120 inactive_update(elapsed_time);
124 is_active_flag = false;
125 if(state_timer.check()) {
129 movement = physic.get_movement(elapsed_time);
132 is_active_flag = false;
133 movement = physic.get_movement(elapsed_time);
137 on_ground_flag = false;
141 BadGuy::str2dir( std::string dir_str )
143 if( dir_str == "auto" )
145 if( dir_str == "left" )
147 if( dir_str == "right" )
151 log_warning << "Badguy::str2dir: unknown direction \"" << dir_str << "\"" << std::endl;;
171 BadGuy::write(lisp::Writer& )
173 log_warning << "tried to write out a generic badguy" << std::endl;
177 BadGuy::active_update(float elapsed_time)
179 movement = physic.get_movement(elapsed_time);
183 BadGuy::inactive_update(float )
188 BadGuy::collision_tile(uint32_t tile_attributes)
190 if(tile_attributes & Tile::HURTS) {
191 if (tile_attributes & Tile::FIRE) {
192 if (is_flammable()) ignite();
194 else if (tile_attributes & Tile::ICE) {
195 if (is_freezable()) freeze();
204 BadGuy::collision(GameObject& other, const CollisionHit& hit)
206 if (!is_active()) return ABORT_MOVE;
208 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
209 if(badguy && badguy->is_active() && badguy->get_group() == COLGROUP_MOVING) {
212 if (badguy->get_bbox().p2.y < (bbox.p1.y + 16)) {
213 if(collision_squished(*badguy)) {
218 return collision_badguy(*badguy, hit);
221 Player* player = dynamic_cast<Player*> (&other);
225 if (player->get_bbox().p2.y < (bbox.p1.y + 16)) {
226 if(collision_squished(*player)) {
231 return collision_player(*player, hit);
234 Bullet* bullet = dynamic_cast<Bullet*> (&other);
236 return collision_bullet(*bullet, hit);
242 BadGuy::collision_solid(const CollisionHit& hit)
244 physic.set_velocity_x(0);
245 physic.set_velocity_y(0);
246 update_on_ground_flag(hit);
250 BadGuy::collision_player(Player& player, const CollisionHit& )
252 if(player.is_invincible()) {
264 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
270 BadGuy::collision_squished(GameObject& )
276 BadGuy::collision_bullet(Bullet& bullet, const CollisionHit& hit)
279 if(bullet.get_type() == FIRE_BONUS) {
280 // fire bullet thaws frozen badguys
285 // other bullets ricochet
286 bullet.ricochet(*this, hit);
290 else if (is_ignited()) {
291 if(bullet.get_type() == ICE_BONUS) {
292 // ice bullets extinguish ignited badguys
297 // other bullets are absorbed by ignited badguys
302 else if(bullet.get_type() == FIRE_BONUS && is_flammable()) {
303 // fire bullets ignite flammable badguys
308 else if(bullet.get_type() == ICE_BONUS && is_freezable()) {
309 // ice bullets freeze freezable badguys
315 // in all other cases, bullets ricochet
316 bullet.ricochet(*this, hit);
322 BadGuy::kill_squished(GameObject& object)
324 sound_manager->play("sounds/squish.wav", get_pos());
325 physic.enable_gravity(true);
326 physic.set_velocity_x(0);
327 physic.set_velocity_y(0);
328 set_state(STATE_SQUISHED);
329 set_group(COLGROUP_MOVING_ONLY_STATIC);
330 Player* player = dynamic_cast<Player*>(&object);
332 if (countMe) Sector::current()->get_level()->stats.badguys++;
333 player->bounce(*this);
337 if(dead_script != "") {
338 std::istringstream stream(dead_script);
339 Sector::current()->run_script(stream, "dead-script");
346 sound_manager->play("sounds/fall.wav", get_pos());
347 if (countMe) Sector::current()->get_level()->stats.badguys++;
348 physic.set_velocity_y(0);
349 physic.enable_gravity(true);
350 set_state(STATE_FALLING);
353 if(dead_script != "") {
354 std::istringstream stream(dead_script);
355 Sector::current()->run_script(stream, "dead-script");
360 BadGuy::run_dead_script()
363 Sector::current()->get_level()->stats.badguys++;
366 if(dead_script != "") {
367 std::istringstream stream(dead_script);
368 Sector::current()->run_script(stream, "dead-script");
373 BadGuy::set_state(State state)
375 if(this->state == state)
378 State laststate = this->state;
382 state_timer.start(SQUISH_TIME);
385 set_group(colgroup_active);
386 //bbox.set_pos(start_position);
389 // was the badguy dead anyway?
390 if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
393 set_group(COLGROUP_DISABLED);
396 set_group(COLGROUP_DISABLED);
404 BadGuy::is_offscreen()
406 Player* player = get_nearest_player();
407 if (!player) return false;
408 Vector dist = player->get_bbox().get_middle() - get_bbox().get_middle();
409 if ((dist.x <= X_OFFSCREEN_DISTANCE+32) && (dist.y <= Y_OFFSCREEN_DISTANCE+32)) {
416 BadGuy::try_activate()
418 // In SuperTux 0.1.x, Badguys were activated when Tux<->Badguy center distance was approx. <= ~668px
419 // This doesn't work for wide-screen monitors which give us a virt. res. of approx. 1066px x 600px
420 Player* player = get_nearest_player();
422 Vector dist = player->get_bbox().get_middle() - get_bbox().get_middle();
423 if ((fabsf(dist.x) <= X_OFFSCREEN_DISTANCE) && (fabsf(dist.y) <= Y_OFFSCREEN_DISTANCE)) {
424 set_state(STATE_ACTIVE);
425 if (!is_initialized) {
427 // if starting direction was set to AUTO, this is our chance to re-orient the badguy
428 if (start_dir == AUTO) {
429 Player* player = get_nearest_player();
430 if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
438 is_initialized = true;
445 BadGuy::might_fall(int height)
447 // make sure we check for at least a 1-pixel fall
452 float y1 = bbox.p2.y + 1;
453 float y2 = bbox.p2.y + 1 + height;
461 return Sector::current()->is_free_of_statics(Rect(x1, y1, x2, y2));
465 BadGuy::get_nearest_player()
467 // FIXME: does not really return nearest player
469 std::vector<Player*> players = Sector::current()->get_players();
470 for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
471 Player* player = *playerIter;
472 if (player->is_dying() || player->is_dead()) continue;
480 BadGuy::update_on_ground_flag(const CollisionHit& hit)
483 on_ground_flag = true;
484 floor_normal = hit.slope_normal;
491 return on_ground_flag;
497 return is_active_flag;
501 BadGuy::get_floor_normal()
509 set_group(COLGROUP_MOVING_STATIC);
516 set_group(colgroup_active);
521 BadGuy::is_freezable() const
527 BadGuy::is_frozen() const
544 BadGuy::is_flammable() const
550 BadGuy::is_ignited() const
556 BadGuy::set_colgroup_active(CollisionGroup group)
558 this->colgroup_active = group;
559 if (state == STATE_ACTIVE) set_group(group);