From: Christoph Sommer Date: Tue, 3 Apr 2007 19:41:49 +0000 (+0000) Subject: Expose Objects on Worldmap in squirrel table "worldmap" X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=c182fc1bf9675e7732a089f9b2ec5e856ef136a2;p=supertux.git Expose Objects on Worldmap in squirrel table "worldmap" SVN-Revision: 4959 --- diff --git a/src/worldmap/worldmap.cpp b/src/worldmap/worldmap.cpp index 9441b43f4..2a6b2552a 100644 --- a/src/worldmap/worldmap.cpp +++ b/src/worldmap/worldmap.cpp @@ -154,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; @@ -171,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() @@ -179,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; @@ -190,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 @@ -212,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 (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 (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) { @@ -512,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 { diff --git a/src/worldmap/worldmap.hpp b/src/worldmap/worldmap.hpp index c0f7dc61e..e988a6935 100644 --- a/src/worldmap/worldmap.hpp +++ b/src/worldmap/worldmap.hpp @@ -123,6 +123,9 @@ public: ~WorldMap(); void add_object(GameObject* object); + + void try_expose(GameObject* object); + void try_unexpose(GameObject* object); static WorldMap* current() { return current_; }