Skip to content

Commit

Permalink
Pause target timer via scripting
Browse files Browse the repository at this point in the history
Closes #2504
  • Loading branch information
tobbi committed Jul 6, 2023
1 parent 925e67b commit 1225211
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 33 deletions.
24 changes: 24 additions & 0 deletions src/scripting/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,30 @@ bool check_cutscene()
return session->get_current_level().m_is_in_cutscene;
}

void pause_target_timer()
{
auto session = GameSession::current();
if (session == nullptr)
{
log_info << "No game session" << std::endl;
return;
}

return session->set_target_timer_paused(true);
}

void resume_target_timer()
{
auto session = GameSession::current();
if (session == nullptr)
{
log_info << "No game session" << std::endl;
return;
}

return session->set_target_timer_paused(false);
}

void wait(HSQUIRRELVM vm, float seconds)
{
if(GameSession::current()->get_current_level().m_skip_cutscene)
Expand Down
11 changes: 11 additions & 0 deletions src/scripting/functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ void end_cutscene();
*/
bool check_cutscene();

/**
* Pauses the target timer
*/
void pause_target_timer();

/**
* Resumes the target timer
*/
void resume_target_timer();


/**
* Suspends the script execution for a specified number of seconds.
* @param float $seconds
Expand Down
96 changes: 64 additions & 32 deletions src/scripting/wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,7 @@ static SQInteger Clouds_set_amount_wrapper(HSQUIRRELVM vm)

static SQInteger ConveyorBelt_release_hook(SQUserPointer ptr, SQInteger )
{
auto _this = reinterpret_cast<scripting::ConveyorBelt*> (ptr);
scripting::ConveyorBelt* _this = reinterpret_cast<scripting::ConveyorBelt*> (ptr);
delete _this;
return 0;
}
Expand All @@ -1263,11 +1263,7 @@ static SQInteger ConveyorBelt_start_wrapper(HSQUIRRELVM vm)
sq_throwerror(vm, _SC("'start' called without instance"));
return SQ_ERROR;
}
auto _this = reinterpret_cast<scripting::ConveyorBelt*> (data);

if (_this == nullptr) {
return SQ_ERROR;
}
scripting::ConveyorBelt* _this = reinterpret_cast<scripting::ConveyorBelt*> (data);


try {
Expand All @@ -1292,11 +1288,7 @@ static SQInteger ConveyorBelt_stop_wrapper(HSQUIRRELVM vm)
sq_throwerror(vm, _SC("'stop' called without instance"));
return SQ_ERROR;
}
auto _this = reinterpret_cast<scripting::ConveyorBelt*> (data);

if (_this == nullptr) {
return SQ_ERROR;
}
scripting::ConveyorBelt* _this = reinterpret_cast<scripting::ConveyorBelt*> (data);


try {
Expand All @@ -1321,11 +1313,7 @@ static SQInteger ConveyorBelt_move_left_wrapper(HSQUIRRELVM vm)
sq_throwerror(vm, _SC("'move_left' called without instance"));
return SQ_ERROR;
}
auto _this = reinterpret_cast<scripting::ConveyorBelt*> (data);

if (_this == nullptr) {
return SQ_ERROR;
}
scripting::ConveyorBelt* _this = reinterpret_cast<scripting::ConveyorBelt*> (data);


try {
Expand All @@ -1350,11 +1338,7 @@ static SQInteger ConveyorBelt_move_right_wrapper(HSQUIRRELVM vm)
sq_throwerror(vm, _SC("'move_right' called without instance"));
return SQ_ERROR;
}
auto _this = reinterpret_cast<scripting::ConveyorBelt*> (data);

if (_this == nullptr) {
return SQ_ERROR;
}
scripting::ConveyorBelt* _this = reinterpret_cast<scripting::ConveyorBelt*> (data);


try {
Expand All @@ -1379,11 +1363,7 @@ static SQInteger ConveyorBelt_set_speed_wrapper(HSQUIRRELVM vm)
sq_throwerror(vm, _SC("'set_speed' called without instance"));
return SQ_ERROR;
}
auto _this = reinterpret_cast<scripting::ConveyorBelt*> (data);

if (_this == nullptr) {
return SQ_ERROR;
}
scripting::ConveyorBelt* _this = reinterpret_cast<scripting::ConveyorBelt*> (data);

SQFloat arg0;
if(SQ_FAILED(sq_getfloat(vm, 2, &arg0))) {
Expand All @@ -1392,7 +1372,7 @@ static SQInteger ConveyorBelt_set_speed_wrapper(HSQUIRRELVM vm)
}

try {
_this->set_speed(static_cast<float> (arg0));
_this->set_speed(arg0);

return 0;

Expand Down Expand Up @@ -11339,6 +11319,44 @@ static SQInteger check_cutscene_wrapper(HSQUIRRELVM vm)

}

static SQInteger pause_target_timer_wrapper(HSQUIRRELVM vm)
{
(void) vm;

try {
scripting::pause_target_timer();

return 0;

} catch(std::exception& e) {
sq_throwerror(vm, e.what());
return SQ_ERROR;
} catch(...) {
sq_throwerror(vm, _SC("Unexpected exception while executing function 'pause_target_timer'"));
return SQ_ERROR;
}

}

static SQInteger resume_target_timer_wrapper(HSQUIRRELVM vm)
{
(void) vm;

try {
scripting::resume_target_timer();

return 0;

} catch(std::exception& e) {
sq_throwerror(vm, e.what());
return SQ_ERROR;
} catch(...) {
sq_throwerror(vm, _SC("Unexpected exception while executing function 'resume_target_timer'"));
return SQ_ERROR;
}

}

static SQInteger wait_wrapper(HSQUIRRELVM vm)
{
HSQUIRRELVM arg0 = vm;
Expand Down Expand Up @@ -13334,6 +13352,20 @@ void register_supertux_wrapper(HSQUIRRELVM v)
throw SquirrelError(v, "Couldn't register function 'check_cutscene'");
}

sq_pushstring(v, "pause_target_timer", -1);
sq_newclosure(v, &pause_target_timer_wrapper, 0);
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".");
if(SQ_FAILED(sq_createslot(v, -3))) {
throw SquirrelError(v, "Couldn't register function 'pause_target_timer'");
}

sq_pushstring(v, "resume_target_timer", -1);
sq_newclosure(v, &resume_target_timer_wrapper, 0);
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".");
if(SQ_FAILED(sq_createslot(v, -3))) {
throw SquirrelError(v, "Couldn't register function 'resume_target_timer'");
}

sq_pushstring(v, "wait", -1);
sq_newclosure(v, &wait_wrapper, 0);
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".b|n");
Expand Down Expand Up @@ -14051,35 +14083,35 @@ void register_supertux_wrapper(HSQUIRRELVM v)
}
sq_pushstring(v, "start", -1);
sq_newclosure(v, &ConveyorBelt_start_wrapper, 0);
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, "x|t");
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".");
if(SQ_FAILED(sq_createslot(v, -3))) {
throw SquirrelError(v, "Couldn't register function 'start'");
}

sq_pushstring(v, "stop", -1);
sq_newclosure(v, &ConveyorBelt_stop_wrapper, 0);
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, "x|t");
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".");
if(SQ_FAILED(sq_createslot(v, -3))) {
throw SquirrelError(v, "Couldn't register function 'stop'");
}

sq_pushstring(v, "move_left", -1);
sq_newclosure(v, &ConveyorBelt_move_left_wrapper, 0);
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, "x|t");
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".");
if(SQ_FAILED(sq_createslot(v, -3))) {
throw SquirrelError(v, "Couldn't register function 'move_left'");
}

sq_pushstring(v, "move_right", -1);
sq_newclosure(v, &ConveyorBelt_move_right_wrapper, 0);
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, "x|t");
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".");
if(SQ_FAILED(sq_createslot(v, -3))) {
throw SquirrelError(v, "Couldn't register function 'move_right'");
}

sq_pushstring(v, "set_speed", -1);
sq_newclosure(v, &ConveyorBelt_set_speed_wrapper, 0);
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, "x|tn");
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".b|n");
if(SQ_FAILED(sq_createslot(v, -3))) {
throw SquirrelError(v, "Couldn't register function 'set_speed'");
}
Expand Down
4 changes: 3 additions & 1 deletion src/supertux/game_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ GameSession::GameSession(const std::string& levelfile_, Savegame& savegame, Stat
m_max_ice_bullets_at_start(),
m_active(false),
m_end_seq_started(false),
m_pause_target_timer(false),
m_current_cutscene_text(),
m_endsequence_timer()
{
Expand Down Expand Up @@ -110,6 +111,7 @@ GameSession::reset_level()

clear_respawn_points();
m_activated_checkpoint = nullptr;
m_pause_target_timer = false;
}

int
Expand Down Expand Up @@ -535,7 +537,7 @@ GameSession::update(float dt_sec, const Controller& controller)
assert(m_currentsector != nullptr);
// Update the world
if (!m_end_sequence || !m_end_sequence->is_running()) {
if (!m_level->m_is_in_cutscene)
if (!m_level->m_is_in_cutscene && !m_pause_target_timer)
{
m_play_time += dt_sec;
m_level->m_stats.finish(m_play_time);
Expand Down
2 changes: 2 additions & 0 deletions src/supertux/game_session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class GameSession final : public Screen,
Level& get_current_level() const { return *m_level; }

void start_sequence(Player* caller, Sequence seq, const SequenceData* data = nullptr);
void set_target_timer_paused(bool paused) { m_pause_target_timer = paused; }

/**
* returns the "working directory" usually this is the directory where the
Expand Down Expand Up @@ -187,6 +188,7 @@ class GameSession final : public Screen,
bool m_active; /** Game active? **/

bool m_end_seq_started;
bool m_pause_target_timer;

std::unique_ptr<GameObject> m_current_cutscene_text;

Expand Down

0 comments on commit 1225211

Please sign in to comment.