Scripted Object: gravity can be turned on/off (patch by rgcjonas, bug 874)
authorLMH <lmh.0013@gmail.com>
Tue, 24 Jan 2012 20:43:54 +0000 (10:43 -1000)
committerLMH <lmh.0013@gmail.com>
Tue, 24 Jan 2012 20:43:54 +0000 (10:43 -1000)
src/object/scripted_object.cpp
src/object/scripted_object.hpp
src/scripting/scripted_object.hpp
src/scripting/wrapper.cpp

index e64c62c..e17de28 100644 (file)
@@ -143,6 +143,18 @@ ScriptedObject::is_solid()
   return solid;
 }
 
+bool
+ScriptedObject::gravity_enabled() const
+{
+       return physic.gravity_enabled();
+}
+
+void
+ScriptedObject::enable_gravity(bool f)
+{
+       physic.enable_gravity(f);
+}
+
 void
 ScriptedObject::set_action(const std::string& animation)
 {
index f3ae632..69b82a0 100644 (file)
@@ -54,6 +54,8 @@ public:
   bool is_visible();
   void set_solid(bool solid);
   bool is_solid();
+  void enable_gravity(bool gravity_enabled);
+  bool gravity_enabled() const;
 
   std::string get_name();
 
index 27da30b..966dbc5 100644 (file)
@@ -39,6 +39,9 @@ public:
   virtual float get_velocity_x() = 0;
   virtual float get_velocity_y() = 0;
 
+  virtual void enable_gravity(bool gravity_enabled) = 0;
+  virtual bool gravity_enabled() const = 0;
+
   virtual void set_visible(bool visible) = 0;
   virtual bool is_visible() = 0;
 
index 0f4ae06..4dccd32 100644 (file)
@@ -1883,6 +1883,60 @@ static SQInteger ScriptedObject_get_velocity_y_wrapper(HSQUIRRELVM vm)
 
 }
 
+static SQInteger ScriptedObject_enable_gravity_wrapper(HSQUIRRELVM vm)
+{
+  SQUserPointer data;
+  if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0)) || !data) {
+    sq_throwerror(vm, _SC("'enable_gravity' called without instance"));
+    return SQ_ERROR;
+  }
+  scripting::ScriptedObject* _this = reinterpret_cast<scripting::ScriptedObject*> (data);
+  SQBool arg0;
+  if(SQ_FAILED(sq_getbool(vm, 2, &arg0))) {
+    sq_throwerror(vm, _SC("Argument 1 not a bool"));
+    return SQ_ERROR;
+  }
+
+  try {
+    _this->enable_gravity(arg0 == SQTrue);
+
+    return 0;
+
+  } catch(std::exception& e) {
+    sq_throwerror(vm, e.what());
+    return SQ_ERROR;
+  } catch(...) {
+    sq_throwerror(vm, _SC("Unexpected exception while executing function 'enable_gravity'"));
+    return SQ_ERROR;
+  }
+
+}
+
+static SQInteger ScriptedObject_gravity_enabled_wrapper(HSQUIRRELVM vm)
+{
+  SQUserPointer data;
+  if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0)) || !data) {
+    sq_throwerror(vm, _SC("'gravity_enabled' called without instance"));
+    return SQ_ERROR;
+  }
+  scripting::ScriptedObject* _this = reinterpret_cast<scripting::ScriptedObject*> (data);
+
+  try {
+    bool return_value = _this->gravity_enabled();
+
+    sq_pushbool(vm, return_value);
+    return 1;
+
+  } catch(std::exception& e) {
+    sq_throwerror(vm, e.what());
+    return SQ_ERROR;
+  } catch(...) {
+    sq_throwerror(vm, _SC("Unexpected exception while executing function 'gravity_enabled'"));
+    return SQ_ERROR;
+  }
+
+}
+
 static SQInteger ScriptedObject_set_visible_wrapper(HSQUIRRELVM vm)
 {
   SQUserPointer data;
@@ -5145,6 +5199,20 @@ void register_supertux_wrapper(HSQUIRRELVM v)
     throw SquirrelError(v, "Couldn't register function 'get_velocity_y'");
   }
 
+  sq_pushstring(v, "enable_gravity", -1);
+  sq_newclosure(v, &ScriptedObject_enable_gravity_wrapper, 0);
+  sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, "x|tb");
+  if(SQ_FAILED(sq_createslot(v, -3))) {
+    throw SquirrelError(v, "Couldn't register function 'enable_gravity'");
+  }
+
+  sq_pushstring(v, "gravity_enabled", -1);
+  sq_newclosure(v, &ScriptedObject_gravity_enabled_wrapper, 0);
+  sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, "x|t");
+  if(SQ_FAILED(sq_createslot(v, -3))) {
+    throw SquirrelError(v, "Couldn't register function 'gravity_enabled'");
+  }
+
   sq_pushstring(v, "set_visible", -1);
   sq_newclosure(v, &ScriptedObject_set_visible_wrapper, 0);
   sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, "x|tb");