Testlevel for auto direction
[supertux.git] / src / badguy / badguy.cpp
index 676ecb7..e37fb9c 100644 (file)
 #include "log.hpp"
 #include "level.hpp"
 #include "object/bullet.hpp"
+#include "main.hpp"
 
 static const float SQUISH_TIME = 2;
 static const float X_OFFSCREEN_DISTANCE = 1600;
 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;
 
@@ -42,10 +52,15 @@ BadGuy::BadGuy(const Vector& pos, const std::string& sprite_name, int layer)
 }
 
 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");
 }
@@ -101,6 +116,21 @@ BadGuy::update(float elapsed_time)
   }
 }
 
+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()
 {
@@ -301,35 +331,48 @@ BadGuy::try_activate()
   /* Activate badguys if they're just around the screen to avoid
    * the effect of having badguys suddenly popping up from nowhere.
    */
-  if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
+  //Badguy left of screen
+  if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE && 
       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();
-  } else if (start_position.x > scroll_x &&
-      start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
+  //Badguy right of screen
+  } else if (start_position.x > scroll_x +  SCREEN_WIDTH &&
+      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
   } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
-      start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
-      ((start_position.y > scroll_y &&
-        start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) ||
-       (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
-        start_position.y < scroll_y))) {
-    dir = start_position.x < scroll_x ? RIGHT : LEFT;
-    set_state(STATE_ACTIVE);
-    activate();
+       start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
+       ((start_position.y > scroll_y + SCREEN_HEIGHT &&
+         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()  ))) {
+     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.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();
   }