-Apply door fix to hatch as well (evil code duplication here...)
[supertux.git] / src / badguy / nolok_01.cpp
index b8499c5..7d66504 100644 (file)
@@ -6,17 +6,17 @@
 
 #define WALK_TIME 2.5
 #define SHOOT_TIME 0.4
-#define JUMP_TIME 0.3
+#define JUMP_TIME 0.5
+#define INITIAL_HITPOINTS 3
 
 static const float WALKSPEED = 90;
 
-//TODO: Create sprite, give multiple hitpoints, limit max number of snowballs
-//      Can only be killed when jumping, no idea why
+//TODO: Create sprite, limit max number of snowballs
 //      Stop actions when pause button is hit (probably a general problem of timers)
-Nolok_01::Nolok_01(LispReader& reader)
+Nolok_01::Nolok_01(const lisp::Lisp& reader)
 {
-  reader.read_float("x", start_position.x);
-  reader.read_float("y", start_position.y);
+  reader.get("x", start_position.x);
+  reader.get("y", start_position.y);
   bbox.set_size(31.8, 63.8);
   sprite = sprite_manager->create("dummyguy");
 }
@@ -30,19 +30,20 @@ Nolok_01::Nolok_01(float pos_x, float pos_y)
 }
 
 void
-Nolok_01::write(LispWriter& writer)
+Nolok_01::write(lisp::Writer& writer)
 {
-  writer.start_list("nolok01");
+  writer.start_list("nolok_01");
 
-  writer.write_float("x", get_pos().x);
-  writer.write_float("y", get_pos().y);
+  writer.write_float("x", start_position.x);
+  writer.write_float("y", start_position.y);
 
-  writer.end_list("nolok01");
+  writer.end_list("nolok_01");
 }
 
 void
 Nolok_01::activate()
 {
+  hitpoints = INITIAL_HITPOINTS;
   physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
   sprite->set_action(dir == LEFT ? "left" : "right");
   action = WALKING;
@@ -52,36 +53,50 @@ Nolok_01::activate()
 void
 Nolok_01::active_action(float elapsed_time)
 {
-   movement = physic.get_movement(elapsed_time);
    if (action_timer.check()) {
-     if (action == WALKING) {
-        physic.set_velocity_y(700);
-        action = JUMPING;
-        action_timer.start(JUMP_TIME);
-     }
-     else if (action == JUMPING) {
+     switch (action) {       
+       case WALKING:
+        {
+         sprite->set_action("jump");
+         physic.set_velocity_y(700);
+         action = JUMPING;
+         action_timer.start(JUMP_TIME);
+         break;
+        }
+       case JUMPING:
+       {
         sprite->set_action("throw");
         action = SHOOTING;
         action_timer.start(SHOOT_TIME);
-     }
-     else if (action == SHOOTING) {
+        break;
+       }
+       case SHOOTING:
+       {
         Sector::current()->add_object(new BouncingSnowball(get_pos().x - 64, get_pos().y, LEFT));
         Sector::current()->add_object(new BouncingSnowball(get_pos().x + 64, get_pos().y, RIGHT));
         physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
         sprite->set_action(dir == LEFT ? "left" : "right");
         action = WALKING;
         action_timer.start(WALK_TIME);
+        break;
+       }
      }
    }
+   movement = physic.get_movement(elapsed_time);
 }
 
 bool
 Nolok_01::collision_squished(Player& player)
 {
-  sprite->set_action("dead"); 
-  kill_squished(player);
-  Sector::current()->add_object(new Door((int)get_pos().x+32, 512, "sector1", "main2"));
-  return true;
+  bool result = false;
+  player.bounce(*this);
+  if (hitpoints <= 0) {
+    sprite->set_action("dead"); 
+    kill_squished(player);
+    Sector::current()->add_object(new Door((int)get_pos().x+32, 512, "sector1", "main2"));
+    result = true;
+  }
+  return result;
 }
 
 HitResponse
@@ -98,3 +113,4 @@ Nolok_01::collision_solid(GameObject& , const CollisionHit& hit)
   return CONTINUE;
 }
 
+IMPLEMENT_FACTORY(Nolok_01, "nolok_01")