More robust way of calculating the falling layer
[supertux.git] / src / supertux / sector.cpp
index 2a7dc37..b5c7580 100644 (file)
@@ -108,6 +108,8 @@ Sector::Sector(Level* parent) :
     throw scripting::SquirrelError(global_vm, "Couldn't get sector table");
   sq_addref(global_vm, &sector_table);
   sq_pop(global_vm, 1);
+
+  foremost_layer = calculate_foremost_layer();
 }
 
 Sector::~Sector()
@@ -650,6 +652,28 @@ Sector::get_active_region()
     camera->get_translation() + Vector(1600, 1200) + Vector(SCREEN_WIDTH,SCREEN_HEIGHT));
 }
 
+int
+Sector::calculate_foremost_layer()
+{
+  int layer = 0;
+  for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i)
+  {
+    TileMap* tm = dynamic_cast<TileMap*>(i->get());
+    if (!tm) continue;
+    if(tm->get_layer() > foremost_layer)
+    {
+      foremost_layer = tm->get_layer();
+    }
+  }
+  return layer;
+}
+
+int
+Sector::get_foremost_layer()
+{
+  return foremost_layer;
+}
+
 void
 Sector::update(float elapsed_time)
 {