more fixes for Kugelblitz and Rain
[supertux.git] / src / badguy / kugelblitz.cpp
index 6c5824c..66354c8 100644 (file)
@@ -20,8 +20,8 @@
 #include <config.h>
 
 #include "kugelblitz.hpp"
-#include "sector.hpp"
 #include "object/tilemap.hpp"
+#include "object/camera.hpp"
 #include "tile.hpp"
 
 #define  LIFETIME 5
@@ -29,6 +29,9 @@
 #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 lisp::Lisp& reader)
     : groundhit_pos_set(false)
 {
@@ -90,7 +93,8 @@ Kugelblitz::collision_player(Player& player, const CollisionHit& )
 HitResponse
 Kugelblitz::collision_badguy(BadGuy& other , const CollisionHit& chit)
 {
-  //Let the Kugelblitz explode, too?
+  //Let the Kugelblitz explode, too? The problem with that is that
+  //two Kugelblitzes would cancel each other out on contact...
   other.kill_fall();
   return hit(chit);
 }
@@ -124,12 +128,7 @@ Kugelblitz::hit(const CollisionHit& chit)
 void
 Kugelblitz::active_update(float elapsed_time)
 {
-  if (electrify_timer.check()) {
-    Sector::current()->solids->change_all(1421,75);
-    Sector::current()->solids->change_all(1422,76);
-    explode();
-  }
-  else if (lifetime.check()) {
+  if (lifetime.check()) {
     explode();
   }
   else {
@@ -143,11 +142,13 @@ Kugelblitz::active_update(float elapsed_time)
     }
     if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 16) {
       //HIT WATER
-      Sector::current()->solids->change_all(75,1421);
-      Sector::current()->solids->change_all(76,1422);
-      physic.set_velocity_x(0);
-      physic.set_velocity_y(0);
-      electrify_timer.start(1);
+      Sector::current()->add_object(new Electrifier(75,1421,1.5));
+      Sector::current()->add_object(new Electrifier(76,1422,1.5));
+      explode();
+    }
+    if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 48) {
+      //HIT ELECTRIFIED WATER
+      explode();
     }
   }
   BadGuy::active_update(elapsed_time);  
@@ -169,4 +170,48 @@ Kugelblitz::explode()
   else remove_me();
 }
 
+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;
+    set_state(STATE_ACTIVE);
+    activate();
+  }
+}
+
 IMPLEMENT_FACTORY(Kugelblitz, "kugelblitz")