[Supertux-Commit] r5462 - in trunk/supertux/src: . scripting

sommer at millhouse.dreamhost.com sommer at millhouse.dreamhost.com
Mon May 12 12:52:52 PDT 2008


Author: sommer
Date: 2008-05-12 12:52:52 -0700 (Mon, 12 May 2008)
New Revision: 5462

Modified:
   trunk/supertux/src/scripting/ssector.hpp
   trunk/supertux/src/scripting/wrapper.cpp
   trunk/supertux/src/sector.cpp
   trunk/supertux/src/sector.hpp
Log:
Add scripting method to set a Sector's gravity
 - using the Console, this is currently done using e.g. sector.settings.set_gravity(10)


Modified: trunk/supertux/src/scripting/ssector.hpp
===================================================================
--- trunk/supertux/src/scripting/ssector.hpp	2008-05-12 18:01:14 UTC (rev 5461)
+++ trunk/supertux/src/scripting/ssector.hpp	2008-05-12 19:52:52 UTC (rev 5462)
@@ -34,6 +34,7 @@
   virtual float get_ambient_red() = 0;
   virtual float get_ambient_green() = 0;
   virtual float get_ambient_blue() = 0;
+  virtual void set_gravity(float gravity) = 0;
 };
 
 }

Modified: trunk/supertux/src/scripting/wrapper.cpp
===================================================================
--- trunk/supertux/src/scripting/wrapper.cpp	2008-05-12 18:01:14 UTC (rev 5461)
+++ trunk/supertux/src/scripting/wrapper.cpp	2008-05-12 19:52:52 UTC (rev 5462)
@@ -2878,6 +2878,35 @@
 
 }
 
+static SQInteger SSector_set_gravity_wrapper(HSQUIRRELVM vm)
+{
+  SQUserPointer data;
+  if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) {
+    sq_throwerror(vm, _SC("'set_gravity' called without instance"));
+    return SQ_ERROR;
+  }
+  Scripting::SSector* _this = reinterpret_cast<Scripting::SSector*> (data);
+  SQFloat arg0;
+  if(SQ_FAILED(sq_getfloat(vm, 2, &arg0))) {
+    sq_throwerror(vm, _SC("Argument 1 not a float"));
+    return SQ_ERROR;
+  }
+
+  try {
+    _this->set_gravity(static_cast<float> (arg0));
+
+    return 0;
+
+  } catch(std::exception& e) {
+    sq_throwerror(vm, e.what());
+    return SQ_ERROR;
+  } catch(...) {
+    sq_throwerror(vm, _SC("Unexpected exception while executing function 'set_gravity'"));
+    return SQ_ERROR;
+  }
+
+}
+
 static SQInteger LevelTime_release_hook(SQUserPointer ptr, SQInteger )
 {
   Scripting::LevelTime* _this = reinterpret_cast<Scripting::LevelTime*> (ptr);
@@ -5166,7 +5195,13 @@
     throw SquirrelError(v, "Couldn't register function 'get_ambient_blue'");
   }
 
+  sq_pushstring(v, "set_gravity", -1);
+  sq_newclosure(v, &SSector_set_gravity_wrapper, 0);
   if(SQ_FAILED(sq_createslot(v, -3))) {
+    throw SquirrelError(v, "Couldn't register function 'set_gravity'");
+  }
+
+  if(SQ_FAILED(sq_createslot(v, -3))) {
     throw SquirrelError(v, "Couldn't register class 'SSector'");
   }
 

Modified: trunk/supertux/src/sector.cpp
===================================================================
--- trunk/supertux/src/sector.cpp	2008-05-12 18:01:14 UTC (rev 5461)
+++ trunk/supertux/src/sector.cpp	2008-05-12 19:52:52 UTC (rev 5462)
@@ -1571,3 +1571,22 @@
 {
   return ambient_light.blue;
 }
+
+void
+Sector::set_gravity(float gravity)
+{
+  log_warning << "Changing a Sector's gravitational constant might have unforseen side-effects" << std::endl;
+
+  this->gravity = gravity;
+
+  for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) {
+    GameObject* game_object = *i;
+    if(!game_object) continue;
+    if(!game_object->is_valid()) continue;
+    UsesPhysic *physics_object = dynamic_cast<UsesPhysic*>(game_object);
+    if (!physics_object) continue;
+
+    physics_object->physic.set_gravity(gravity);
+  }
+}
+

Modified: trunk/supertux/src/sector.hpp
===================================================================
--- trunk/supertux/src/sector.hpp	2008-05-12 18:01:14 UTC (rev 5461)
+++ trunk/supertux/src/sector.hpp	2008-05-12 19:52:52 UTC (rev 5462)
@@ -193,6 +193,11 @@
   float get_ambient_green();
   float get_ambient_blue();
 
+  /**
+   *  set gravity throughout sector
+   */
+  void set_gravity(float gravity);
+
 private:
   Level* level; /**< Parent level containing this sector */
   uint32_t collision_tile_attributes(const Rect& dest) const;




More information about the Supertux-Commit mailing list