From 78a8025c991520e0089327437d80e64ae9927e0e Mon Sep 17 00:00:00 2001 From: Semphriss <66701383+Semphriss@users.noreply.github.com> Date: Fri, 14 Aug 2020 17:38:20 -0400 Subject: [PATCH] Added Squirrel function to edit the action (animation) of a platform's sprite (#1506) Co-authored-by: Semphris --- src/object/platform.cpp | 6 +++++ src/object/platform.hpp | 3 +++ src/scripting/platform.cpp | 7 ++++++ src/scripting/platform.hpp | 3 +++ src/scripting/wrapper.cpp | 47 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+) diff --git a/src/object/platform.cpp b/src/object/platform.cpp index 7f30dbdbf52..516a513a0d5 100644 --- a/src/object/platform.cpp +++ b/src/object/platform.cpp @@ -149,6 +149,12 @@ Platform::stop_moving() get_walker()->stop_moving(); } +void +Platform::set_action(const std::string& action, int repeat) +{ + MovingSprite::set_action(action, repeat); +} + void Platform::move_to(const Vector& pos) { diff --git a/src/object/platform.hpp b/src/object/platform.hpp index 227d4cdc2ae..3fee0008e22 100644 --- a/src/object/platform.hpp +++ b/src/object/platform.hpp @@ -60,6 +60,9 @@ class Platform : public MovingSprite, /** Stop platform at next node */ void stop_moving(); + /** Updates the platform to the given action */ + void set_action(const std::string& action, int repeat); + /** @} */ private: diff --git a/src/scripting/platform.cpp b/src/scripting/platform.cpp index a528f939e4b..9173e483cde 100644 --- a/src/scripting/platform.cpp +++ b/src/scripting/platform.cpp @@ -40,6 +40,13 @@ Platform::stop_moving() object.stop_moving(); } +void +Platform::set_action(const std::string& action, int repeat) +{ + SCRIPT_GUARD_VOID; + object.set_action(action, repeat); +} + } // namespace scripting /* EOF */ diff --git a/src/scripting/platform.hpp b/src/scripting/platform.hpp index e2566e716f6..4e8f2e067bd 100644 --- a/src/scripting/platform.hpp +++ b/src/scripting/platform.hpp @@ -47,6 +47,9 @@ class Platform final /** Stop platform at next node */ void stop_moving(); + + /** Updates the platform to the given action */ + void set_action(const std::string& action, int repeat); }; } // namespace scripting diff --git a/src/scripting/wrapper.cpp b/src/scripting/wrapper.cpp index 1c68ec6a06b..118f454a4e4 100644 --- a/src/scripting/wrapper.cpp +++ b/src/scripting/wrapper.cpp @@ -1866,6 +1866,46 @@ static SQInteger Platform_stop_moving_wrapper(HSQUIRRELVM vm) } +static SQInteger Platform_set_action_wrapper(HSQUIRRELVM vm) +{ + SQUserPointer data; + if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, nullptr)) || !data) { + sq_throwerror(vm, _SC("'set_action' called without instance")); + return SQ_ERROR; + } + auto _this = reinterpret_cast (data); + + if (_this == nullptr) { + return SQ_ERROR; + } + + const SQChar* arg0; + if(SQ_FAILED(sq_getstring(vm, 2, &arg0))) { + sq_throwerror(vm, _SC("Argument 1 not a string")); + return SQ_ERROR; + } + + SQInteger arg1; + if(SQ_FAILED(sq_getinteger(vm, 3, &arg1))) { + sq_throwerror(vm, _SC("Argument 2 not a integer")); + return SQ_ERROR; + } + + try { + _this->set_action(static_cast (arg0), static_cast (arg1)); + + 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_action'")); + return SQ_ERROR; + } + +} + static SQInteger Player_release_hook(SQUserPointer ptr, SQInteger ) { auto _this = reinterpret_cast (ptr); @@ -8059,6 +8099,13 @@ void register_supertux_wrapper(HSQUIRRELVM v) if(SQ_FAILED(sq_createslot(v, -3))) { throw SquirrelError(v, "Couldn't register function 'stop_moving'"); } + + sq_pushstring(v, "set_action", -1); + sq_newclosure(v, &Platform_set_action_wrapper, 0); + sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, "x|tsi"); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'set_action'"); + } if(SQ_FAILED(sq_createslot(v, -3))) { throw SquirrelError(v, "Couldn't register class 'Platform'");