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.
22 #include "object/camera.hpp"
23 #include "object/tilemap.hpp"
25 #include "statistics.hpp"
26 #include "game_session.hpp"
29 #include "object/bullet.hpp"
31 #include "object/particles.hpp"
32 #include "random_generator.hpp"
34 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),
40 dir(LEFT), start_dir(AUTO), frozen(false), ignited(false),
41 state(STATE_INIT), on_ground_flag(false)
43 start_position = bbox.p1;
45 sound_manager->preload("sounds/squish.wav");
46 sound_manager->preload("sounds/fall.wav");
49 BadGuy::BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name, int layer)
50 : MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), countMe(true),
51 dir(direction), start_dir(direction), frozen(false), ignited(false),
52 state(STATE_INIT), on_ground_flag(false)
54 start_position = bbox.p1;
56 sound_manager->preload("sounds/squish.wav");
57 sound_manager->preload("sounds/fall.wav");
60 BadGuy::BadGuy(const lisp::Lisp& reader, const std::string& sprite_name, int layer)
61 : MovingSprite(reader, sprite_name, layer, COLGROUP_DISABLED), countMe(true), dir(LEFT), start_dir(AUTO), frozen(false), ignited(false), state(STATE_INIT), on_ground_flag(false)
63 start_position = bbox.p1;
65 std::string dir_str = "auto";
66 reader.get("direction", dir_str);
67 start_dir = str2dir( dir_str );
70 reader.get("dead-script", dead_script);
72 sound_manager->preload("sounds/squish.wav");
73 sound_manager->preload("sounds/fall.wav");
77 BadGuy::draw(DrawingContext& context)
81 if(state == STATE_INIT || state == STATE_INACTIVE)
83 if(state == STATE_FALLING) {
84 DrawingEffect old_effect = context.get_drawing_effect();
85 context.set_drawing_effect((DrawingEffect) (old_effect | VERTICAL_FLIP));
86 sprite->draw(context, get_pos(), layer);
87 context.set_drawing_effect(old_effect);
89 sprite->draw(context, get_pos(), layer);
94 BadGuy::update(float elapsed_time)
96 if(!Sector::current()->inside(bbox)) {
101 if (state == STATE_ACTIVE) deactivate();
102 set_state(STATE_INACTIVE);
107 active_update(elapsed_time);
111 inactive_update(elapsed_time);
115 if(state_timer.check()) {
119 movement = physic.get_movement(elapsed_time);
122 movement = physic.get_movement(elapsed_time);
126 on_ground_flag = false;
130 BadGuy::str2dir( std::string dir_str )
132 if( dir_str == "auto" )
134 if( dir_str == "left" )
136 if( dir_str == "right" )
140 log_warning << "Badguy::str2dir: unknown direction \"" << dir_str << "\"" << std::endl;;
155 BadGuy::write(lisp::Writer& )
157 log_warning << "tried to write out a generic badguy" << std::endl;
161 BadGuy::active_update(float elapsed_time)
163 movement = physic.get_movement(elapsed_time);
167 BadGuy::inactive_update(float )
172 BadGuy::collision_tile(uint32_t tile_attributes)
174 if(tile_attributes & Tile::HURTS) {
175 if (tile_attributes & Tile::FIRE) {
176 if (is_flammable()) ignite();
178 else if (tile_attributes & Tile::ICE) {
179 if (is_freezable()) freeze();
188 BadGuy::collision(GameObject& other, const CollisionHit& hit)
195 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
196 if(badguy && badguy->state == STATE_ACTIVE && badguy->get_group() == COLGROUP_MOVING) {
199 if (badguy->get_bbox().p2.y < (bbox.p1.y + 16)) {
200 if(collision_squished(*badguy)) {
205 return collision_badguy(*badguy, hit);
208 Player* player = dynamic_cast<Player*> (&other);
212 if (player->get_bbox().p2.y < (bbox.p1.y + 16)) {
213 if(collision_squished(*player)) {
218 return collision_player(*player, hit);
221 Bullet* bullet = dynamic_cast<Bullet*> (&other);
223 return collision_bullet(*bullet, hit);
237 BadGuy::collision_solid(const CollisionHit& hit)
239 physic.set_velocity_x(0);
240 physic.set_velocity_y(0);
241 update_on_ground_flag(hit);
245 BadGuy::collision_player(Player& player, const CollisionHit& )
247 if(player.is_invincible()) {
259 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
265 BadGuy::collision_squished(GameObject& )
271 BadGuy::collision_bullet(Bullet& bullet, const CollisionHit& hit)
274 if(bullet.get_type() == FIRE_BONUS) {
275 // fire bullet thaws frozen badguys
280 // other bullets ricochet
281 bullet.ricochet(*this, hit);
285 else if (is_ignited()) {
286 if(bullet.get_type() == ICE_BONUS) {
287 // ice bullets extinguish ignited badguys
292 // other bullets are absorbed by ignited badguys
297 else if(bullet.get_type() == FIRE_BONUS && is_flammable()) {
298 // fire bullets ignite flammable badguys
303 else if(bullet.get_type() == ICE_BONUS && is_freezable()) {
304 // ice bullets freeze freezable badguys
310 // in all other cases, bullets ricochet
311 bullet.ricochet(*this, hit);
317 BadGuy::kill_squished(GameObject& object)
319 sound_manager->play("sounds/squish.wav", get_pos());
320 physic.enable_gravity(true);
321 physic.set_velocity_x(0);
322 physic.set_velocity_y(0);
323 set_state(STATE_SQUISHED);
324 set_group(COLGROUP_MOVING_ONLY_STATIC);
325 Player* player = dynamic_cast<Player*>(&object);
327 if (countMe) Sector::current()->get_level()->stats.badguys++;
328 player->bounce(*this);
332 if(dead_script != "") {
333 std::istringstream stream(dead_script);
334 Sector::current()->run_script(stream, "dead-script");
341 sound_manager->play("sounds/fall.wav", get_pos());
342 if (countMe) Sector::current()->get_level()->stats.badguys++;
343 physic.set_velocity_y(0);
344 physic.enable_gravity(true);
345 set_state(STATE_FALLING);
348 if(dead_script != "") {
349 std::istringstream stream(dead_script);
350 Sector::current()->run_script(stream, "dead-script");
355 BadGuy::run_dead_script()
358 Sector::current()->get_level()->stats.badguys++;
361 if(dead_script != "") {
362 std::istringstream stream(dead_script);
363 Sector::current()->run_script(stream, "dead-script");
368 BadGuy::set_state(State state)
370 if(this->state == state)
373 State laststate = this->state;
377 state_timer.start(SQUISH_TIME);
380 set_group(COLGROUP_MOVING);
381 bbox.set_pos(start_position);
384 // was the badguy dead anyway?
385 if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
388 set_group(COLGROUP_DISABLED);
391 set_group(COLGROUP_DISABLED);
399 BadGuy::is_offscreen()
401 float scroll_x = Sector::current()->camera->get_translation().x;
402 float scroll_y = Sector::current()->camera->get_translation().y;
404 if(bbox.p2.x < scroll_x - X_OFFSCREEN_DISTANCE
405 || bbox.p1.x > scroll_x + X_OFFSCREEN_DISTANCE + SCREEN_WIDTH
406 || bbox.p2.y < scroll_y - Y_OFFSCREEN_DISTANCE
407 || bbox.p1.y > scroll_y + Y_OFFSCREEN_DISTANCE + SCREEN_HEIGHT)
414 BadGuy::try_activate()
416 float scroll_x = Sector::current()->camera->get_translation().x;
417 float scroll_y = Sector::current()->camera->get_translation().y;
419 /* Activate badguys if they're just around the screen to avoid
420 * the effect of having badguys suddenly popping up from nowhere.
422 //Badguy left of screen
423 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
424 start_position.x < scroll_x - bbox.get_width() &&
425 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
426 start_position.y < scroll_y + SCREEN_HEIGHT + Y_OFFSCREEN_DISTANCE) {
427 if (start_dir != AUTO) dir = start_dir; else dir = RIGHT;
428 set_state(STATE_ACTIVE);
430 //Badguy right of screen
431 } else if (start_position.x > scroll_x + SCREEN_WIDTH &&
432 start_position.x < scroll_x + SCREEN_WIDTH + X_OFFSCREEN_DISTANCE &&
433 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
434 start_position.y < scroll_y + SCREEN_HEIGHT + Y_OFFSCREEN_DISTANCE) {
435 if (start_dir != AUTO) dir = start_dir; else dir = LEFT;
436 set_state(STATE_ACTIVE);
438 //Badguy over or under screen
439 } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
440 start_position.x < scroll_x + SCREEN_WIDTH + X_OFFSCREEN_DISTANCE &&
441 ((start_position.y > scroll_y + SCREEN_HEIGHT &&
442 start_position.y < scroll_y + SCREEN_HEIGHT + Y_OFFSCREEN_DISTANCE) ||
443 (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
444 start_position.y < scroll_y - bbox.get_height() ))) {
445 if (start_dir != AUTO) dir = start_dir;
447 // if nearest player is to our right, start facing right
448 Player* player = get_nearest_player();
449 if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
455 set_state(STATE_ACTIVE);
457 } else if(state == STATE_INIT
458 && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
459 && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE + SCREEN_WIDTH
460 && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
461 && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE + SCREEN_HEIGHT ) {
462 if (start_dir != AUTO) {
465 // if nearest player is to our right, start facing right
466 Player* player = get_nearest_player();
467 if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
473 set_state(STATE_ACTIVE);
479 BadGuy::might_fall(int height)
481 // make sure we check for at least a 1-pixel fall
486 float y1 = bbox.p2.y + 1;
487 float y2 = bbox.p2.y + 1 + height;
495 return Sector::current()->is_free_of_statics(Rect(x1, y1, x2, y2));
499 BadGuy::get_nearest_player()
501 // FIXME: does not really return nearest player
503 std::vector<Player*> players = Sector::current()->get_players();
504 for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
505 Player* player = *playerIter;
513 BadGuy::update_on_ground_flag(const CollisionHit& hit)
516 on_ground_flag = true;
517 floor_normal = hit.slope_normal;
524 return on_ground_flag;
528 BadGuy::get_floor_normal()
536 set_group(COLGROUP_MOVING_STATIC);
543 set_group(COLGROUP_MOVING);
548 BadGuy::is_freezable() const
554 BadGuy::is_frozen() const
571 BadGuy::is_flammable() const
577 BadGuy::is_ignited() const