Skip to content

Commit

Permalink
Added Squirrel function to edit the action (animation) of a platform'…
Browse files Browse the repository at this point in the history
…s sprite (#1506)

Co-authored-by: Semphris <[email protected]>
  • Loading branch information
Semphriss and Semphris authored Aug 14, 2020
1 parent 17576a9 commit 78a8025
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/object/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
3 changes: 3 additions & 0 deletions src/object/platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions src/scripting/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
3 changes: 3 additions & 0 deletions src/scripting/platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions src/scripting/wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<scripting::Platform*> (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<const char*> (arg0), static_cast<int> (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<scripting::Player*> (ptr);
Expand Down Expand Up @@ -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'");
Expand Down

0 comments on commit 78a8025

Please sign in to comment.