Add current level to another debug message
[supertux.git] / src / badguy / kugelblitz.cpp
index 2e304bb..cfc900f 100644 (file)
 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 #include "badguy/kugelblitz.hpp"
+
+#include <math.h>
+
 #include "math/random_generator.hpp"
 #include "object/camera.hpp"
 #include "object/player.hpp"
 #include "sprite/sprite.hpp"
+#include "sprite/sprite_manager.hpp"
 #include "supertux/object_factory.hpp"
 #include "supertux/sector.hpp"
 #include "util/reader.hpp"
 #define  BASE_SPEED 200
 #define  RAND_SPEED 150
 
-static const float X_OFFSCREEN_DISTANCE = 1600;
-static const float Y_OFFSCREEN_DISTANCE = 1200;
-
 Kugelblitz::Kugelblitz(const Reader& reader) :
-  BadGuy(reader, "images/creatures/kugelblitz/kugelblitz.sprite"), 
+  BadGuy(reader, "images/creatures/kugelblitz/kugelblitz.sprite"),
   pos_groundhit(),
   groundhit_pos_set(false),
   dying(),
   movement_timer(),
   lifetime(),
   direction(),
-  state()
+  light(0.0f,0.0f,0.0f),
+  lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light.sprite"))
 {
   reader.get("x", start_position.x);
   sprite->set_action("falling");
   physic.enable_gravity(false);
   countMe = false;
+
+  lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
+  lightsprite->set_color(Color(0.2f, 0.1f, 0.0f));
 }
 
 void
@@ -93,10 +98,10 @@ Kugelblitz::collision_badguy(BadGuy& other , const CollisionHit& chit)
 }
 
 HitResponse
-Kugelblitz::hit(const CollisionHit& hit)
+Kugelblitz::hit(const CollisionHit& hit_)
 {
   // hit floor?
-  if(hit.bottom) {
+  if(hit_.bottom) {
     if (!groundhit_pos_set)
     {
       pos_groundhit = get_pos();
@@ -105,13 +110,13 @@ Kugelblitz::hit(const CollisionHit& hit)
     sprite->set_action("flying");
     physic.set_velocity_y(0);
     //Set random initial speed and direction
-    direction = systemRandom.rand(2)? 1: -1;
-    int speed = (BASE_SPEED + (systemRandom.rand(RAND_SPEED))) * direction;
+    direction = gameRandom.rand(2)? 1: -1;
+    int speed = (BASE_SPEED + (gameRandom.rand(RAND_SPEED))) * direction;
     physic.set_velocity_x(speed);
     movement_timer.start(MOVETIME);
     lifetime.start(LIFETIME);
 
-  } else if(hit.top) { // bumped on roof
+  } else if(hit_.top) { // bumped on roof
     physic.set_velocity_y(0);
   }
 
@@ -128,7 +133,7 @@ Kugelblitz::active_update(float elapsed_time)
     if (groundhit_pos_set) {
       if (movement_timer.check()) {
         if (direction == 1) direction = -1; else direction = 1;
-        int speed = (BASE_SPEED + (systemRandom.rand(RAND_SPEED))) * direction;
+        int speed = (BASE_SPEED + (gameRandom.rand(RAND_SPEED))) * direction;
         physic.set_velocity_x(speed);
         movement_timer.start(MOVETIME);
       }
@@ -150,6 +155,22 @@ Kugelblitz::active_update(float elapsed_time)
 }
 
 void
+Kugelblitz::draw(DrawingContext& context)
+{
+  sprite->draw(context, get_pos(), layer);
+
+  //Only draw light in dark areas
+  context.get_light( get_bbox().get_middle(), &light );
+  if (light.red + light.green < 2.0){
+    context.push_target();
+    context.set_target(DrawingContext::LIGHTMAP);
+    sprite->draw(context, get_pos(), layer);
+    lightsprite->draw(context, get_bbox().get_middle(), 0);
+    context.pop_target();
+  }
+}
+
+void
 Kugelblitz::kill_fall()
 {
 }
@@ -168,47 +189,32 @@ Kugelblitz::explode()
 void
 Kugelblitz::try_activate()
 {
-  //FIXME: Don't activate Kugelblitz before it's on-screen
-  float scroll_x = Sector::current()->camera->get_translation().x;
-  float scroll_y = Sector::current()->camera->get_translation().y;
-
-  /* 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 &&
-      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;
-    set_state(STATE_ACTIVE);
-    activate();
-  } else if (start_position.x > scroll_x &&
-             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;
-    set_state(STATE_ACTIVE);
-    activate();
-  } 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();
-  } 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;
+  // Much smaller offscreen distances to pop out of nowhere and surprise Tux
+  float X_OFFSCREEN_DISTANCE = 400;
+  float Y_OFFSCREEN_DISTANCE = 600;
+
+  Player* player_ = get_nearest_player();
+  if (!player_) return;
+  Vector dist = player_->get_bbox().get_middle() - get_bbox().get_middle();
+  if ((fabsf(dist.x) <= X_OFFSCREEN_DISTANCE) && (fabsf(dist.y) <= Y_OFFSCREEN_DISTANCE)) {
     set_state(STATE_ACTIVE);
+    if (!is_initialized) {
+
+      // if starting direction was set to AUTO, this is our chance to re-orient the badguy
+      if (start_dir == AUTO) {
+        Player* player__ = get_nearest_player();
+        if (player__ && (player__->get_bbox().p1.x > get_bbox().p2.x)) {
+          dir = RIGHT;
+        } else {
+          dir = LEFT;
+        }
+      }
+
+      initialize();
+      is_initialized = true;
+    }
     activate();
   }
 }
 
-IMPLEMENT_FACTORY(Kugelblitz, "kugelblitz");
-
 /* EOF */