From 7a0bff0e1654117767b86aea6fe47d6b2f60cdfb Mon Sep 17 00:00:00 2001 From: Christoph Sommer Date: Mon, 12 May 2008 19:52:52 +0000 Subject: [PATCH] Add scripting method to set a Sector's gravity - using the Console, this is currently done using e.g. sector.settings.set_gravity(10) SVN-Revision: 5462 --- src/scripting/ssector.hpp | 1 + src/scripting/wrapper.cpp | 35 +++++++++++++++++++++++++++++++++++ src/sector.cpp | 19 +++++++++++++++++++ src/sector.hpp | 5 +++++ 4 files changed, 60 insertions(+) diff --git a/src/scripting/ssector.hpp b/src/scripting/ssector.hpp index 82fc54215..08dda196c 100644 --- a/src/scripting/ssector.hpp +++ b/src/scripting/ssector.hpp @@ -34,6 +34,7 @@ public: 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; }; } diff --git a/src/scripting/wrapper.cpp b/src/scripting/wrapper.cpp index 7a5ab3082..d3c8cf1af 100644 --- a/src/scripting/wrapper.cpp +++ b/src/scripting/wrapper.cpp @@ -2878,6 +2878,35 @@ static SQInteger SSector_get_ambient_blue_wrapper(HSQUIRRELVM vm) } +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 (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 (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 (ptr); @@ -5166,6 +5195,12 @@ void register_supertux_wrapper(HSQUIRRELVM v) 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'"); } diff --git a/src/sector.cpp b/src/sector.cpp index eb3ead64e..688bba31c 100644 --- a/src/sector.cpp +++ b/src/sector.cpp @@ -1571,3 +1571,22 @@ Sector::get_ambient_blue() { 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(game_object); + if (!physics_object) continue; + + physics_object->physic.set_gravity(gravity); + } +} + diff --git a/src/sector.hpp b/src/sector.hpp index 5033959c1..e3b22e00e 100644 --- a/src/sector.hpp +++ b/src/sector.hpp @@ -193,6 +193,11 @@ public: 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; -- 2.11.0