+bool
+Sector::before_object_add(GameObject* object)
+{
+ Bullet* bullet = dynamic_cast<Bullet*> (object);
+ if(bullet)
+ bullets.push_back(bullet);
+
+ MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
+ if(movingobject) {
+ moving_objects.push_back(movingobject);
+#ifdef USE_GRID
+ grid->add_object(movingobject);
+#endif
+ }
+
+ TileMap* tilemap = dynamic_cast<TileMap*> (object);
+ if(tilemap && tilemap->is_solid()) {
+ if(solids == 0) {
+ solids = tilemap;
+ } else {
+ log_warning << "Another solid tilemaps added. Ignoring" << std::endl;
+ }
+ }
+
+ Camera* camera = dynamic_cast<Camera*> (object);
+ if(camera) {
+ if(this->camera != 0) {
+ log_warning << "Multiple cameras added. Ignoring" << std::endl;
+ return false;
+ }
+ this->camera = camera;
+ }
+
+ Player* player = dynamic_cast<Player*> (object);
+ if(player) {
+ if(this->player != 0) {
+ log_warning << "Multiple players added. Ignoring" << std::endl;
+ return false;
+ }
+ this->player = player;
+ }
+
+ if(_current == this) {
+ try_expose(object);
+ }
+
+ return true;
+}
+
+void
+Sector::try_expose(GameObject* object)
+{
+ ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
+ if(interface != NULL) {
+ HSQUIRRELVM vm = script_manager->get_vm();
+ sq_pushobject(vm, sector_table);
+ interface->expose(vm, -1);
+ sq_pop(vm, 1);
+ }
+}
+
+void
+Sector::before_object_remove(GameObject* object)
+{
+ if(_current == this)
+ try_unexpose(object);
+}
+
+void
+Sector::try_unexpose(GameObject* object)
+{
+ ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
+ if(interface != NULL) {
+ HSQUIRRELVM vm = script_manager->get_vm();
+ int oldtop = sq_gettop(vm);
+ sq_pushobject(vm, sector_table);
+ try {
+ interface->unexpose(vm, -1);
+ } catch(std::exception& e) {
+ log_warning << "Couldn't unregister object: " << e.what() << std::endl;
+ }
+ sq_settop(vm, oldtop);
+ }
+}
+