New Badguy "Will-O-Wisp": eerie light that tracks Tux and warps him to another spawnpoint
[supertux.git] / src / badguy / kugelblitz.cpp
index bab4d32..eb4fc2e 100644 (file)
 #include <config.h>
 
 #include "kugelblitz.hpp"
+#include "object/tilemap.hpp"
+#include "object/camera.hpp"
+#include "tile.hpp"
 
-static const float JUMPSPEED=600;
-static const float Kugelblitz_MID_TOLERANCE=4;
-static const float Kugelblitz_LOW_TOLERANCE=2;
+#define  LIFETIME 5
+#define  MOVETIME 0.75
+#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)
 {
   reader.get("x", start_position.x);
   start_position.y = 0; //place above visible area
-  bbox.set_size(63.8, 127.8);
-  sprite = sprite_manager->create("kugelblitz");
+  bbox.set_size(63.8, 63.8);
+  sprite = sprite_manager->create("images/creatures/kugelblitz/kugelblitz.sprite");
   sprite->set_action("falling");
   physic.enable_gravity(false);
 }
@@ -42,7 +49,6 @@ Kugelblitz::write(lisp::Writer& writer)
   writer.start_list("kugelblitz");
 
   writer.write_float("x", start_position.x);
-  writer.write_float("y", start_position.y);
 
   writer.end_list("kugelblitz");
 }
@@ -51,6 +57,9 @@ void
 Kugelblitz::activate()
 {
   physic.set_velocity_y(-300);
+  physic.set_velocity_x(-20); //fall a little to the left
+  direction = 1;
+  dying = false;
 }
 
 HitResponse
@@ -60,8 +69,31 @@ Kugelblitz::collision_solid(GameObject& , const CollisionHit& chit)
 }
 
 HitResponse
+Kugelblitz::collision_player(Player& player, const CollisionHit& )
+{
+  if(player.is_invincible()) {
+    explode();
+    return ABORT_MOVE;
+  }
+  // hit from above?
+  if(player.get_movement().y - get_movement().y > 0 && player.get_bbox().p2.y <
+      (get_bbox().p1.y + get_bbox().p2.y) / 2) {
+    // if it's not is it possible to squish us, then this will hurt
+    if(!collision_squished(player))
+      player.kill(Player::SHRINK);
+      explode();
+    return FORCE_MOVE;
+  }
+  player.kill(Player::SHRINK);
+  explode();
+  return FORCE_MOVE;
+}
+
+HitResponse
 Kugelblitz::collision_badguy(BadGuy& other , const CollisionHit& chit)
 {
+  //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);
 }
@@ -76,10 +108,14 @@ Kugelblitz::hit(const CollisionHit& chit)
       pos_groundhit = get_pos();
       groundhit_pos_set = true;
     }
-    bbox.set_size(63.8, 63.8);
     sprite->set_action("flying");
     physic.set_velocity_y(0);
-    physic.set_velocity_x(100);
+    //Set random initial speed and direction
+    if ((rand() % 2) == 1) direction = 1; else direction = -1;
+    int speed = (BASE_SPEED + (rand() % RAND_SPEED)) * direction;
+    physic.set_velocity_x(speed);
+    movement_timer.start(MOVETIME);
+    lifetime.start(LIFETIME);
 
   } else if(chit.normal.y < .5) { // bumped on roof
     physic.set_velocity_y(0);
@@ -91,7 +127,90 @@ Kugelblitz::hit(const CollisionHit& chit)
 void
 Kugelblitz::active_update(float elapsed_time)
 {
-  BadGuy::active_update(elapsed_time);
+  if (lifetime.check()) {
+    explode();
+  }
+  else {
+    if (groundhit_pos_set) {
+      if (movement_timer.check()) {
+        if (direction == 1) direction = -1; else direction = 1;
+        int speed = (BASE_SPEED + (rand() % RAND_SPEED)) * direction;
+        physic.set_velocity_x(speed);
+        movement_timer.start(MOVETIME);
+      }
+    }
+    if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 16) {
+      //HIT WATER
+      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);  
+}
+
+void
+Kugelblitz::kill_fall()
+{
+}
+
+void
+Kugelblitz::explode()
+{
+  if (!dying) {
+    sprite->set_action("pop");
+    lifetime.start(0.2);
+    dying = true;
+  }
+  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")