Skip to content

Commit

Permalink
Add Tux.set_velocity() to scripting API. (#2580)
Browse files Browse the repository at this point in the history
* add `Player.set_velocity()` to scripting api

* Add doxygen wiki comment.

i forgot

* Add doxygen params to `player.hpp` [ci skip]
  • Loading branch information
tylerandari13 authored Aug 16, 2023
1 parent e77c5e0 commit 69ad382
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/scripting/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ Player::get_velocity_y() const
return object.get_physic().get_velocity_y();
}

void
Player::set_velocity(float x, float y)
{
SCRIPT_GUARD_VOID;
object.get_physic().set_velocity(x, y);
}

bool
Player::has_grabbed(const std::string& name) const
{
Expand Down
7 changes: 7 additions & 0 deletions src/scripting/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ class Player final
*/
float get_velocity_y() const;

/**
* Sets the velocity of the player to a programmable/variable speed.
* @param float $x The speed Tux will move on the x axis.
* @param float $y The speed Tux will move on the y axis.
*/
void set_velocity(float x, float y);

/**
* Gets the X coordinate of the player.
*/
Expand Down
42 changes: 42 additions & 0 deletions src/scripting/wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7409,6 +7409,41 @@ static SQInteger Player_get_velocity_y_wrapper(HSQUIRRELVM vm)

}

static SQInteger Player_set_velocity_wrapper(HSQUIRRELVM vm)
{
SQUserPointer data;
if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, nullptr, SQTrue)) || !data) {
sq_throwerror(vm, _SC("'set_velocity' called without instance"));
return SQ_ERROR;
}
scripting::Player* _this = reinterpret_cast<scripting::Player*> (data);

SQFloat arg0;
if(SQ_FAILED(sq_getfloat(vm, 2, &arg0))) {
sq_throwerror(vm, _SC("Argument 1 not a float"));
return SQ_ERROR;
}
SQFloat arg1;
if(SQ_FAILED(sq_getfloat(vm, 3, &arg1))) {
sq_throwerror(vm, _SC("Argument 2 not a float"));
return SQ_ERROR;
}

try {
_this->set_velocity(arg0, 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_velocity'"));
return SQ_ERROR;
}

}

static SQInteger Player_get_x_wrapper(HSQUIRRELVM vm)
{
SQUserPointer data;
Expand Down Expand Up @@ -15815,6 +15850,13 @@ void register_supertux_wrapper(HSQUIRRELVM v)
throw SquirrelError(v, "Couldn't register function 'get_velocity_y'");
}

sq_pushstring(v, "set_velocity", -1);
sq_newclosure(v, &Player_set_velocity_wrapper, 0);
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".b|nb|n");
if(SQ_FAILED(sq_createslot(v, -3))) {
throw SquirrelError(v, "Couldn't register function 'set_velocity'");
}

sq_pushstring(v, "get_x", -1);
sq_newclosure(v, &Player_get_x_wrapper, 0);
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".");
Expand Down

0 comments on commit 69ad382

Please sign in to comment.