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), dir(LEFT), start_dir(AUTO), frozen(false), ignited(false), draw_dead_script_hint(false), state(STATE_INIT), on_ground_flag(false)
41 start_position = bbox.p1;
43 sound_manager->preload("sounds/squish.wav");
44 sound_manager->preload("sounds/fall.wav");
47 BadGuy::BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name, int layer)
48 : MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), countMe(true), dir(direction), start_dir(direction), frozen(false), ignited(false), draw_dead_script_hint(false), state(STATE_INIT), on_ground_flag(false)
50 start_position = bbox.p1;
52 sound_manager->preload("sounds/squish.wav");
53 sound_manager->preload("sounds/fall.wav");
56 BadGuy::BadGuy(const lisp::Lisp& reader, const std::string& sprite_name, int layer)
57 : 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)
59 start_position = bbox.p1;
61 std::string dir_str = "auto";
62 reader.get("direction", dir_str);
63 start_dir = str2dir( dir_str );
66 reader.get("dead-script", dead_script);
67 draw_dead_script_hint = (dead_script != "");
69 sound_manager->preload("sounds/squish.wav");
70 sound_manager->preload("sounds/fall.wav");
74 BadGuy::draw(DrawingContext& context)
78 if(state == STATE_INIT || state == STATE_INACTIVE)
80 if(state == STATE_FALLING) {
81 DrawingEffect old_effect = context.get_drawing_effect();
82 context.set_drawing_effect((DrawingEffect) (old_effect | VERTICAL_FLIP));
83 sprite->draw(context, get_pos(), layer);
84 context.set_drawing_effect(old_effect);
86 sprite->draw(context, get_pos(), layer);
87 if (draw_dead_script_hint) {
88 Vector ppos = Vector(systemRandom.randf(bbox.p1.x+8, bbox.p2.x-8), bbox.p2.y);
89 Vector pspeed = Vector(0, -100);
90 Sector::current()->add_object(new Particles(ppos, 44, 46, pspeed, Vector(0,0), 1, Color(.6f, .2f, .2f), 3, .1f, LAYER_OBJECTS+1));
96 BadGuy::update(float elapsed_time)
98 if(!Sector::current()->inside(bbox)) {
103 if (state == STATE_ACTIVE) deactivate();
104 set_state(STATE_INACTIVE);
109 active_update(elapsed_time);
113 inactive_update(elapsed_time);
117 if(state_timer.check()) {
121 movement = physic.get_movement(elapsed_time);
124 movement = physic.get_movement(elapsed_time);
128 on_ground_flag = false;
132 BadGuy::str2dir( std::string dir_str )
134 if( dir_str == "auto" )
136 if( dir_str == "left" )
138 if( dir_str == "right" )
142 log_warning << "Badguy::str2dir: unknown direction \"" << dir_str << "\"" << std::endl;;
157 BadGuy::write(lisp::Writer& )
159 log_warning << "tried to write out a generic badguy" << std::endl;
163 BadGuy::active_update(float elapsed_time)
165 movement = physic.get_movement(elapsed_time);
169 BadGuy::inactive_update(float )
174 BadGuy::collision_tile(uint32_t tile_attributes)
176 if(tile_attributes & Tile::HURTS) {
177 if (tile_attributes & Tile::FIRE) {
178 if (is_flammable()) ignite();
180 else if (tile_attributes & Tile::ICE) {
181 if (is_freezable()) freeze();
190 BadGuy::collision(GameObject& other, const CollisionHit& hit)
197 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
198 if(badguy && badguy->state == STATE_ACTIVE && badguy->get_group() == COLGROUP_MOVING) {
201 if (badguy->get_bbox().p2.y < (bbox.p1.y + 16)) {
202 if(collision_squished(*badguy)) {
207 return collision_badguy(*badguy, hit);
210 Player* player = dynamic_cast<Player*> (&other);
214 if (player->get_bbox().p2.y < (bbox.p1.y + 16)) {
215 if(collision_squished(*player)) {
220 return collision_player(*player, hit);
223 Bullet* bullet = dynamic_cast<Bullet*> (&other);
225 return collision_bullet(*bullet, hit);
239 BadGuy::collision_solid(const CollisionHit& hit)
241 physic.set_velocity_x(0);
242 physic.set_velocity_y(0);
243 update_on_ground_flag(hit);
247 BadGuy::collision_player(Player& player, const CollisionHit& )
249 if(player.is_invincible()) {
261 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
267 BadGuy::collision_squished(GameObject& )
273 BadGuy::collision_bullet(Bullet& bullet, const CollisionHit& hit)
276 if(bullet.get_type() == FIRE_BONUS) {
277 // fire bullet thaws frozen badguys
282 // other bullets ricochet
283 bullet.ricochet(*this, hit);
287 else if (is_ignited()) {
288 if(bullet.get_type() == ICE_BONUS) {
289 // ice bullets extinguish ignited badguys
294 // other bullets are absorbed by ignited badguys
299 else if(bullet.get_type() == FIRE_BONUS && is_flammable()) {
300 // fire bullets ignite flammable badguys
305 else if(bullet.get_type() == ICE_BONUS && is_freezable()) {
306 // ice bullets freeze freezable badguys
312 // in all other cases, bullets ricochet
313 bullet.ricochet(*this, hit);
319 BadGuy::kill_squished(GameObject& object)
321 sound_manager->play("sounds/squish.wav", get_pos());
322 physic.enable_gravity(true);
323 physic.set_velocity_x(0);
324 physic.set_velocity_y(0);
325 set_state(STATE_SQUISHED);
326 set_group(COLGROUP_MOVING_ONLY_STATIC);
327 Player* player = dynamic_cast<Player*>(&object);
329 if (countMe) Sector::current()->get_level()->stats.badguys++;
330 player->bounce(*this);
334 if(dead_script != "") {
335 std::istringstream stream(dead_script);
336 Sector::current()->run_script(stream, "dead-script");
343 sound_manager->play("sounds/fall.wav", get_pos());
344 if (countMe) Sector::current()->get_level()->stats.badguys++;
345 physic.set_velocity_y(0);
346 physic.enable_gravity(true);
347 set_state(STATE_FALLING);
350 if(dead_script != "") {
351 std::istringstream stream(dead_script);
352 Sector::current()->run_script(stream, "dead-script");
357 BadGuy::run_dead_script()
359 if (countMe) Sector::current()->get_level()->stats.badguys++;
362 if(dead_script != "") {
363 std::istringstream stream(dead_script);
364 Sector::current()->run_script(stream, "dead-script");
369 BadGuy::set_state(State state)
371 if(this->state == state)
374 State laststate = this->state;
378 state_timer.start(SQUISH_TIME);
381 set_group(COLGROUP_MOVING);
382 bbox.set_pos(start_position);
385 // was the badguy dead anyway?
386 if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
389 set_group(COLGROUP_DISABLED);
392 set_group(COLGROUP_DISABLED);
400 BadGuy::is_offscreen()
402 float scroll_x = Sector::current()->camera->get_translation().x;
403 float scroll_y = Sector::current()->camera->get_translation().y;
405 if(bbox.p2.x < scroll_x - X_OFFSCREEN_DISTANCE
406 || bbox.p1.x > scroll_x + X_OFFSCREEN_DISTANCE + SCREEN_WIDTH
407 || bbox.p2.y < scroll_y - Y_OFFSCREEN_DISTANCE
408 || bbox.p1.y > scroll_y + Y_OFFSCREEN_DISTANCE + SCREEN_HEIGHT)
415 BadGuy::try_activate()
417 float scroll_x = Sector::current()->camera->get_translation().x;
418 float scroll_y = Sector::current()->camera->get_translation().y;
420 /* Activate badguys if they're just around the screen to avoid
421 * the effect of having badguys suddenly popping up from nowhere.
423 //Badguy left of screen
424 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
425 start_position.x < scroll_x - bbox.get_width() &&
426 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
427 start_position.y < scroll_y + SCREEN_HEIGHT + Y_OFFSCREEN_DISTANCE) {
428 if (start_dir != AUTO) dir = start_dir; else dir = RIGHT;
429 set_state(STATE_ACTIVE);
431 //Badguy right of screen
432 } else if (start_position.x > scroll_x + SCREEN_WIDTH &&
433 start_position.x < scroll_x + SCREEN_WIDTH + X_OFFSCREEN_DISTANCE &&
434 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
435 start_position.y < scroll_y + SCREEN_HEIGHT + Y_OFFSCREEN_DISTANCE) {
436 if (start_dir != AUTO) dir = start_dir; else dir = LEFT;
437 set_state(STATE_ACTIVE);
439 //Badguy over or under screen
440 } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
441 start_position.x < scroll_x + SCREEN_WIDTH + X_OFFSCREEN_DISTANCE &&
442 ((start_position.y > scroll_y + SCREEN_HEIGHT &&
443 start_position.y < scroll_y + SCREEN_HEIGHT + Y_OFFSCREEN_DISTANCE) ||
444 (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
445 start_position.y < scroll_y - bbox.get_height() ))) {
446 if (start_dir != AUTO) dir = start_dir;
448 // if nearest player is to our right, start facing right
449 Player* player = get_nearest_player();
450 if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
456 set_state(STATE_ACTIVE);
458 } else if(state == STATE_INIT
459 && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
460 && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE + SCREEN_WIDTH
461 && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
462 && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE + SCREEN_HEIGHT ) {
463 if (start_dir != AUTO) {
466 // if nearest player is to our right, start facing right
467 Player* player = get_nearest_player();
468 if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
474 set_state(STATE_ACTIVE);
480 BadGuy::might_fall(int height)
482 // make sure we check for at least a 1-pixel fall
487 float y1 = bbox.p2.y + 1;
488 float y2 = bbox.p2.y + 1 + height;
496 return Sector::current()->is_free_of_statics(Rect(x1, y1, x2, y2));
500 BadGuy::get_nearest_player()
502 // FIXME: does not really return nearest player
504 std::vector<Player*> players = Sector::current()->get_players();
505 for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
506 Player* player = *playerIter;
514 BadGuy::update_on_ground_flag(const CollisionHit& hit)
517 on_ground_flag = true;
518 floor_normal = hit.slope_normal;
525 return on_ground_flag;
529 BadGuy::get_floor_normal()
537 set_group(COLGROUP_MOVING_STATIC);
544 set_group(COLGROUP_MOVING);
549 BadGuy::is_freezable() const
555 BadGuy::is_frozen() const
572 BadGuy::is_flammable() const
578 BadGuy::is_ignited() const