Use run_dead_script wherever possible and make kill_* operations only do stuff once
[supertux.git] / src / badguy / yeti.cpp
index f358b79..f6cc57a 100644 (file)
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //  02111-1307, USA.
+
 #include <config.h>
 
-#include <float.h>
-#include <sstream>
-#include <memory>
 #include "yeti.hpp"
+
 #include "object/camera.hpp"
 #include "yeti_stalactite.hpp"
 #include "bouncing_snowball.hpp"
 #include "game_session.hpp"
 #include "level.hpp"
+#include "lisp/writer.hpp"
+#include "object_factory.hpp"
+#include "audio/sound_manager.hpp"
+#include "sector.hpp"
+#include "object/player.hpp"
+#include "sprite/sprite.hpp"
+
+#include <math.h>
+#include <float.h>
 
 namespace {
   const float JUMP_DOWN_VX = 250; /**< horizontal speed while jumping off the dais */
@@ -53,13 +61,13 @@ namespace {
 }
 
 Yeti::Yeti(const lisp::Lisp& reader)
-       : BadGuy(reader, "images/creatures/yeti/yeti.sprite")
+  : BadGuy(reader, "images/creatures/yeti/yeti.sprite")
 {
   hit_points = INITIAL_HITPOINTS;
   countMe = false;
   sound_manager->preload("sounds/yeti_gna.wav");
   sound_manager->preload("sounds/yeti_roar.wav");
-  draw_dead_script_hint = false;
+  hud_head.reset(new Surface("images/creatures/yeti/hudlife.png"));
 }
 
 Yeti::~Yeti()
@@ -67,7 +75,7 @@ Yeti::~Yeti()
 }
 
 void
-Yeti::activate()
+Yeti::initialize()
 {
   dir = RIGHT;
   jump_down();
@@ -80,10 +88,32 @@ Yeti::draw(DrawingContext& context)
   if(safe_timer.started() && size_t(game_time*40)%2)
     return;
 
+  draw_hit_points(context);
+
   BadGuy::draw(context);
 }
 
 void
+Yeti::draw_hit_points(DrawingContext& context)
+{
+  int i;
+
+  Surface *hh = hud_head.get();
+  if (!hh)
+    return;
+
+  context.push_transform();
+  context.set_translation(Vector(0, 0));
+
+  for (i = 0; i < hit_points; ++i)
+  {
+    context.draw_surface(hh, Vector(BORDER_X + (i * hh->get_width()), BORDER_Y + 1), LAYER_FOREGROUND1);
+  }
+
+  context.pop_transform();
+}
+
+void
 Yeti::active_update(float elapsed_time)
 {
   switch(state) {
@@ -197,15 +227,10 @@ void Yeti::take_hit(Player& )
 
     state = SQUISHED;
     state_timer.start(SQUISH_TIME);
-    set_group(COLGROUP_MOVING_ONLY_STATIC);
+    set_colgroup_active(COLGROUP_MOVING_ONLY_STATIC);
     sprite->set_action("dead");
 
-    if (countMe) Sector::current()->get_level()->stats.badguys++;
-
-    if(dead_script != "") {
-      std::istringstream stream(dead_script);
-      Sector::current()->run_script(stream, "Yeti - dead-script");
-    }
+    run_dead_script();
   }
   else {
     safe_timer.start(SAFE_TIME);
@@ -216,7 +241,7 @@ void
 Yeti::kill_fall()
 {
   // shooting bullets or being invincible won't work :)
-  take_hit(*get_nearest_player()); // FIXME: debug only(?)
+  //take_hit(*get_nearest_player()); // FIXME: debug only(?)
 }
 
 void
@@ -224,8 +249,8 @@ Yeti::write(lisp::Writer& writer)
 {
   writer.start_list("yeti");
 
-  writer.write_float("x", start_position.x);
-  writer.write_float("y", start_position.y);
+  writer.write("x", start_position.x);
+  writer.write("y", start_position.y);
 
   writer.end_list("yeti");
 }
@@ -234,7 +259,7 @@ void
 Yeti::drop_stalactite()
 {
   // make a stalactite falling down and shake camera a bit
-  Sector::current()->camera->shake(.1, 0, 10);
+  Sector::current()->camera->shake(.1f, 0, 10);
 
   YetiStalactite* nearest = 0;
   float dist = FLT_MAX;
@@ -268,28 +293,28 @@ Yeti::collision_solid(const CollisionHit& hit)
     physic.set_velocity_y(0);
     switch (state) {
       case JUMP_DOWN:
-       run();
-       break;
+        run();
+        break;
       case RUN:
-       break;
+        break;
       case JUMP_UP:
-       break;
+        break;
       case BE_ANGRY:
-       // we just landed
-       if(!state_timer.started()) {
-         sprite->set_action((dir==RIGHT)?"stand-right":"stand-left");
-         stomp_count++;
-         drop_stalactite();
-
-         // go to other side after 3 jumps
-         if(stomp_count == 3) {
-           jump_down();
-         } else {
-           // jump again
-           state_timer.start(STOMP_WAIT);
-         }
-       }
-       break;
+        // we just landed
+        if(!state_timer.started()) {
+          sprite->set_action((dir==RIGHT)?"stand-right":"stand-left");
+          stomp_count++;
+          drop_stalactite();
+
+          // go to other side after 3 jumps
+          if(stomp_count == 3) {
+            jump_down();
+          } else {
+            // jump again
+            state_timer.start(STOMP_WAIT);
+          }
+        }
+        break;
       case SQUISHED:
         break;
     }