Expose Objects on Worldmap in squirrel table "worldmap"
[supertux.git] / src / worldmap / worldmap.cpp
index c3f71d1..2a6b255 100644 (file)
@@ -36,6 +36,7 @@
 #include "shrinkfade.hpp"
 #include "video/surface.hpp"
 #include "video/drawing_context.hpp"
+#include "sprite/sprite.hpp"
 #include "sprite/sprite_manager.hpp"
 #include "audio/sound_manager.hpp"
 #include "lisp/parser.hpp"
@@ -153,8 +154,6 @@ WorldMap::WorldMap(const std::string& filename, const std::string& force_spawnpo
   worldmap_menu->add_hl();
   worldmap_menu->add_entry(MNID_QUITWORLDMAP, _("Quit World"));
 
-  load(filename);
-
   // create a new squirrel table for the worldmap
   using namespace Scripting;
 
@@ -170,6 +169,9 @@ WorldMap::WorldMap(const std::string& filename, const std::string& force_spawnpo
 
   sq_addref(global_vm, &worldmap_table);
   sq_pop(global_vm, 1);
+  
+  // load worldmap objects
+  load(filename);
 }
 
 WorldMap::~WorldMap()
@@ -178,6 +180,18 @@ WorldMap::~WorldMap()
 
   save_state();
 
+  for(GameObjects::iterator i = game_objects.begin();
+      i != game_objects.end(); ++i) {
+    GameObject* object = *i;
+    try_unexpose(object);
+    object->unref();
+  }
+
+  for(SpawnPoints::iterator i = spawn_points.begin();
+      i != spawn_points.end(); ++i) {
+    delete *i;
+  }
+
   for(ScriptList::iterator i = scripts.begin();
       i != scripts.end(); ++i) {
     HSQOBJECT& object = *i;
@@ -189,17 +203,6 @@ WorldMap::~WorldMap()
 
   if(current_ == this)
     current_ = NULL;
-
-  for(GameObjects::iterator i = game_objects.begin();
-      i != game_objects.end(); ++i) {
-    GameObject* object = *i;
-    object->unref();
-  }
-
-  for(SpawnPoints::iterator i = spawn_points.begin();
-      i != spawn_points.end(); ++i) {
-    delete *i;
-  }
 }
 
 void
@@ -211,10 +214,40 @@ WorldMap::add_object(GameObject* object)
   }
 
   object->ref();
+  try_expose(object);
   game_objects.push_back(object);
 }
 
 void
+WorldMap::try_expose(GameObject* object)
+{
+  ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
+  if(interface != NULL) {
+    HSQUIRRELVM vm = Scripting::global_vm;
+    sq_pushobject(vm, worldmap_table);
+    interface->expose(vm, -1);
+    sq_pop(vm, 1);
+  }
+}
+
+void
+WorldMap::try_unexpose(GameObject* object)
+{
+  ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
+  if(interface != NULL) {
+    HSQUIRRELVM vm = Scripting::global_vm;
+    SQInteger oldtop = sq_gettop(vm);
+    sq_pushobject(vm, worldmap_table);
+    try {
+      interface->unexpose(vm, -1);
+    } catch(std::exception& e) {
+      log_warning << "Couldn't unregister object: " << e.what() << std::endl;
+    }
+    sq_settop(vm, oldtop);
+  }
+}
+
+void
 WorldMap::move_to_spawnpoint(const std::string& spawnpoint)
 {
   for(SpawnPoints::iterator i = spawn_points.begin(); i != spawn_points.end(); ++i) {
@@ -247,7 +280,7 @@ WorldMap::load(const std::string& filename)
 
   try {
     lisp::Parser parser;
-    std::auto_ptr<lisp::Lisp> root (parser.parse(map_filename));
+    const lisp::Lisp* root = parser.parse(map_filename);
 
     const lisp::Lisp* lisp = root->get_lisp("supertux-level");
     if(!lisp)
@@ -323,7 +356,7 @@ WorldMap::get_level_title(LevelTile& level)
 
   try {
     lisp::Parser parser;
-    std::auto_ptr<lisp::Lisp> root (parser.parse(levels_path + level.get_name()));
+    const lisp::Lisp* root = parser.parse(levels_path + level.get_name());
 
     const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
     if(!level_lisp)
@@ -511,6 +544,7 @@ WorldMap::update(float delta)
         i != game_objects.end(); ) {
       GameObject* object = *i;
       if(!object->is_valid()) {
+        try_unexpose(object);
         object->unref();
         i = game_objects.erase(i);
       } else {
@@ -528,10 +562,15 @@ WorldMap::update(float delta)
     if (camera_offset.y < 0)
       camera_offset.y = 0;
 
-    if (camera_offset.x > solids->get_width()*32 - SCREEN_WIDTH)
-      camera_offset.x = solids->get_width()*32 - SCREEN_WIDTH;
-    if (camera_offset.y > solids->get_height()*32 - SCREEN_HEIGHT)
-      camera_offset.y = solids->get_height()*32 - SCREEN_HEIGHT;
+    if (camera_offset.x > (int)solids->get_width()*32 - SCREEN_WIDTH)
+      camera_offset.x = (int)solids->get_width()*32 - SCREEN_WIDTH;
+    if (camera_offset.y > (int)solids->get_height()*32 - SCREEN_HEIGHT)
+      camera_offset.y = (int)solids->get_height()*32 - SCREEN_HEIGHT;
+
+    if (int(solids->get_width()*32) < SCREEN_WIDTH)
+      camera_offset.x = solids->get_width()*16.0 - SCREEN_WIDTH/2.0;
+    if (int(solids->get_height()*32) < SCREEN_HEIGHT)
+      camera_offset.y = solids->get_height()*16.0 - SCREEN_HEIGHT/2.0;
 
     // handle input
     bool enter_level = false;
@@ -663,6 +702,10 @@ WorldMap::at_teleporter(const Vector& pos)
 void
 WorldMap::draw(DrawingContext& context)
 {
+  if (int(solids->get_width()*32) < SCREEN_WIDTH || int(solids->get_height()*32) < SCREEN_HEIGHT)
+    context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
+      Color(0.0f, 0.0f, 0.0f, 1.0f), LAYER_BACKGROUND0);
+
   context.set_ambient_color( ambient_light );
   context.push_transform();
   context.set_translation(camera_offset);
@@ -694,9 +737,9 @@ WorldMap::draw_status(DrawingContext& context)
           get_level_title(*level);
 
         context.draw_text(white_text, level->title,
-            Vector(SCREEN_WIDTH/2,
-              SCREEN_HEIGHT - white_text->get_height() - 30),
-            CENTER_ALLIGN, LAYER_FOREGROUND1);
+                          Vector(SCREEN_WIDTH/2,
+                                 SCREEN_HEIGHT - white_text->get_height() - 30),
+                          ALIGN_CENTER, LAYER_FOREGROUND1);
 
         // if level is solved, draw level picture behind stats
         /*
@@ -726,7 +769,7 @@ WorldMap::draw_status(DrawingContext& context)
           context.draw_text(gold_text, special_tile->map_message,
               Vector(SCREEN_WIDTH/2,
                 SCREEN_HEIGHT - white_text->get_height() - 60),
-              CENTER_ALLIGN, LAYER_FOREGROUND1);
+              ALIGN_CENTER, LAYER_FOREGROUND1);
         break;
       }
     }
@@ -735,7 +778,7 @@ WorldMap::draw_status(DrawingContext& context)
     Teleporter* teleporter = at_teleporter(tux->get_tile_pos());
     if (teleporter && (teleporter->message != "")) {
       Vector pos = Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 30);
-      context.draw_text(white_text, teleporter->message, pos, CENTER_ALLIGN, LAYER_FOREGROUND1);
+      context.draw_text(white_text, teleporter->message, pos, ALIGN_CENTER, LAYER_FOREGROUND1);
     }
 
   }
@@ -744,7 +787,7 @@ WorldMap::draw_status(DrawingContext& context)
   if(passive_message_timer.started())
     context.draw_text(gold_text, passive_message,
             Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 60),
-            CENTER_ALLIGN, LAYER_FOREGROUND1);
+            ALIGN_CENTER, LAYER_FOREGROUND1);
 
   context.pop_transform();
 }
@@ -956,12 +999,12 @@ WorldMap::save_state()
 
     // push world into worlds table
     sq_createslot(vm, -3);
-  } catch(std::exception& e) {
+  } catch(std::exception& ) {
     sq_settop(vm, oldtop);
   }
 
   sq_settop(vm, oldtop);
-  
+
   if(World::current() != NULL)
     World::current()->save_state();
 }
@@ -989,7 +1032,7 @@ WorldMap::load_state()
     // get table for our world
     sq_pushstring(vm, map_filename.c_str(), map_filename.length());
     if(SQ_FAILED(sq_get(vm, -2)))
-      throw Scripting::SquirrelError(vm, "Couldn't get state.world.mapfilename");
+      throw Scripting::SquirrelError(vm, "Couldn't get state.worlds.mapfilename");
 
     // load tux
     sq_pushstring(vm, "tux", -1);