- implemented reset points
authorIngo Ruhnke <grumbel@gmx.de>
Mon, 19 Apr 2004 22:47:36 +0000 (22:47 +0000)
committerIngo Ruhnke <grumbel@gmx.de>
Mon, 19 Apr 2004 22:47:36 +0000 (22:47 +0000)
SVN-Revision: 582

src/gameloop.cpp
src/level.cpp
src/level.h

index cfdefc1..00c26bd 100644 (file)
@@ -63,6 +63,13 @@ GameSession::restart_level()
   fps_timer.init(true);
   frame_timer.init(true);
 
+  float old_x_pos = -1;
+
+  if (world)
+    { // Tux has lost a life, so we try to respawn him at the nearest reset point
+      old_x_pos = world->get_tux()->base.x;
+    }
+  
   delete world;
 
   if (st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
@@ -77,6 +84,25 @@ GameSession::restart_level()
     {
       world = new World(subset, levelnb);
     }
+
+  // Set Tux to the nearest reset point
+  if (old_x_pos != -1)
+    {
+      ResetPoint best_reset_point = { -1, -1 };
+      for(std::vector<ResetPoint>::iterator i = get_level()->reset_points.begin();
+          i != get_level()->reset_points.end(); ++i)
+        {
+          if (i->x < old_x_pos && best_reset_point.x < i->x)
+            best_reset_point = *i;
+        }
+      
+      if (best_reset_point.x != -1)
+        {
+          world->get_tux()->base.x = best_reset_point.x;
+          world->get_tux()->base.y = best_reset_point.y;
+        }
+    }
+
     
   if (st_gl_mode != ST_GL_DEMO_GAME)
     {
index e21be87..4811a7b 100644 (file)
@@ -315,7 +315,29 @@ Level::load(const std::string& filename)
 
       reader.read_int_vector("foreground-tm",  &fg_tm);
 
-      {
+      { // Read ResetPoints
+        lisp_object_t* cur = 0;
+        if (reader.read_lisp("reset-points",  &cur))
+          {
+            while (!lisp_nil_p(cur))
+              {
+                lisp_object_t* data = lisp_car(cur);
+
+                ResetPoint pos;
+
+                LispReader reader(lisp_cdr(data));
+                if (reader.read_int("x", &pos.x)
+                    && reader.read_int("y", &pos.y))
+                  {
+                    reset_points.push_back(pos);
+                  }
+
+                cur = lisp_cdr(cur);
+              }
+          }
+      }
+
+      { // Read BadGuys
         lisp_object_t* cur = 0;
         if (reader.read_lisp("objects",  &cur))
           {
@@ -557,6 +579,7 @@ Level::cleanup()
       fg_tiles[i].clear();
     }
 
+  reset_points.clear();
   name.clear();
   author.clear();
   theme.clear();
index f093683..4a22948 100644 (file)
@@ -50,6 +50,12 @@ enum TileMapType {
  TM_FG
  };
 
+struct ResetPoint
+{
+  int x;
+  int y;
+};
+
 class Level 
 {
  public:
@@ -74,6 +80,9 @@ class Level
   float gravity;
 
   std::vector<BadGuyData> badguy_data;
+
+  /** A collection of points to which Tux can be reset after a lost live */
+  std::vector<ResetPoint> reset_points;
  public:
   Level();
   Level(const std::string& subset, int level);