static const float Y_OFFSCREEN_DISTANCE = 1200;
BadGuy::BadGuy(const Vector& pos, const std::string& sprite_name, int layer)
- : MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), countMe(true), dir(LEFT), state(STATE_INIT)
+ : MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), countMe(true), dir(LEFT), start_dir(AUTO), state(STATE_INIT)
+{
+ start_position = bbox.p1;
+
+ sound_manager->preload("sounds/squish.wav");
+ sound_manager->preload("sounds/fall.wav");
+}
+
+BadGuy::BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name, int layer)
+ : MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), countMe(true), dir(direction), start_dir(direction), state(STATE_INIT)
{
start_position = bbox.p1;
}
BadGuy::BadGuy(const lisp::Lisp& reader, const std::string& sprite_name, int layer)
- : MovingSprite(reader, sprite_name, layer, COLGROUP_DISABLED), countMe(true), dir(LEFT), state(STATE_INIT)
+ : MovingSprite(reader, sprite_name, layer, COLGROUP_DISABLED), countMe(true), dir(LEFT), start_dir(AUTO), state(STATE_INIT)
{
start_position = bbox.p1;
+ std::string dir_str = "auto";
+ reader.get("direction", dir_str);
+ start_dir = str2dir( dir_str );
+ dir = start_dir;
+
sound_manager->preload("sounds/squish.wav");
sound_manager->preload("sounds/fall.wav");
}
}
}
+Direction
+BadGuy::str2dir( std::string dir_str )
+{
+ if( dir_str == "auto" )
+ return AUTO;
+ if( dir_str == "left" )
+ return LEFT;
+ if( dir_str == "right" )
+ return RIGHT;
+
+ //default to "auto"
+ log_warning << "Badguy::str2dir: unknown direction \"" << dir_str << "\"" << std::endl;;
+ return AUTO;
+}
+
void
BadGuy::activate()
{
start_position.x < scroll_x - bbox.get_width() &&
start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
- dir = RIGHT;
+ if (start_dir != AUTO) dir = start_dir; else dir = RIGHT;
set_state(STATE_ACTIVE);
activate();
//Badguy right of screen
start_position.x < scroll_x + SCREEN_WIDTH + X_OFFSCREEN_DISTANCE &&
start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
- dir = LEFT;
+ if (start_dir != AUTO) dir = start_dir; else dir = LEFT;
set_state(STATE_ACTIVE);
activate();
//Badguy over or under screen
start_position.y < scroll_y + SCREEN_HEIGHT + Y_OFFSCREEN_DISTANCE) ||
(start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
start_position.y < scroll_y - bbox.get_height() ))) {
- dir = start_position.x < scroll_x ? RIGHT : LEFT;
+ if (start_dir != AUTO) dir = start_dir; else dir = start_position.x < scroll_x ? RIGHT : LEFT;
set_state(STATE_ACTIVE);
activate();
} else if(state == STATE_INIT
&& start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
&& start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
&& start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
- dir = LEFT;
+ if (start_dir != AUTO) {
+ dir = start_dir;
+ } else {
+ // if nearest player is to our right, start facing right
+ Player* player = get_nearest_player();
+ if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
+ dir = RIGHT;
+ } else {
+ dir = LEFT;
+ }
+ }
set_state(STATE_ACTIVE);
activate();
}