+bool
+Sector::before_object_add(GameObject* object)
+{
+ Bullet* bullet = dynamic_cast<Bullet*> (object);
+ if(bullet != NULL) {
+ bullets.push_back(bullet);
+ }
+
+ MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
+ if(movingobject != NULL) {
+ moving_objects.push_back(movingobject);
+ }
+
+ Portable* portable = dynamic_cast<Portable*> (object);
+ if(portable != NULL) {
+ portables.push_back(portable);
+ }
+
+ TileMap* tilemap = dynamic_cast<TileMap*> (object);
+ if(tilemap != NULL && tilemap->is_solid()) {
+ solid_tilemaps.push_back(tilemap);
+ }
+
+ Camera* camera = dynamic_cast<Camera*> (object);
+ if(camera != NULL) {
+ 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 != NULL) {
+ if(this->player != 0) {
+ log_warning << "Multiple players added. Ignoring" << std::endl;
+ return false;
+ }
+ this->player = player;
+ }
+
+ UsesPhysic *physic_object = dynamic_cast<UsesPhysic *>(object);
+ if(physic_object)
+ {
+ physic_object->physic.set_gravity(gravity);
+ }
+
+
+ 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 = Scripting::global_vm;
+ sq_pushobject(vm, sector_table);
+ interface->expose(vm, -1);
+ sq_pop(vm, 1);
+ }
+}
+
+void
+Sector::try_expose_me()
+{
+ HSQUIRRELVM vm = Scripting::global_vm;
+ sq_pushobject(vm, sector_table);
+ Scripting::SSector* interface = static_cast<Scripting::SSector*> (this);
+ expose_object(vm, -1, interface, "settings", false);
+ sq_pop(vm, 1);
+}
+
+void
+Sector::before_object_remove(GameObject* object)
+{
+ Portable* portable = dynamic_cast<Portable*> (object);
+ if(portable != NULL) {
+ portables.erase(std::find(portables.begin(), portables.end(), portable));
+ }
+ Bullet* bullet = dynamic_cast<Bullet*> (object);
+ if(bullet != NULL) {
+ bullets.erase(std::find(bullets.begin(), bullets.end(), bullet));
+ }
+ MovingObject* moving_object = dynamic_cast<MovingObject*> (object);
+ if(moving_object != NULL) {
+ moving_objects.erase(
+ std::find(moving_objects.begin(), moving_objects.end(), moving_object));
+ }
+
+ if(_current == this)
+ try_unexpose(object);
+}
+
+void
+Sector::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, 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);
+ }
+}
+
+void
+Sector::try_unexpose_me()
+{
+ HSQUIRRELVM vm = Scripting::global_vm;
+ SQInteger oldtop = sq_gettop(vm);
+ sq_pushobject(vm, sector_table);
+ try {
+ Scripting::unexpose_object(vm, -1, "settings");
+ } catch(std::exception& e) {
+ log_warning << "Couldn't unregister object: " << e.what() << std::endl;
+ }
+ sq_settop(vm, oldtop);
+}