Skip to content

Commit

Permalink
Pause target timer via scripting (#2513)
Browse files Browse the repository at this point in the history
* Pause target timer via scripting

Closes #2504

* Fix stuff

* Add functions to Level table

* Pause / Start LevelTime objects

* Use get_objects_by_type
  • Loading branch information
tobbi authored Aug 1, 2023
1 parent 54c198c commit 6161ee6
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 5 deletions.
2 changes: 2 additions & 0 deletions data/scripts/default.nut
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Level <- {
set_respawn_pos=Level_set_respawn_pos,
flip_vertically=Level_flip_vertically,
toggle_pause=Level_toggle_pause,
pause_target_timer=Level_pause_target_timer,
resume_target_timer=Level_resume_target_timer,
edit=Level_edit
};

Expand Down
14 changes: 14 additions & 0 deletions src/scripting/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ Level_edit(bool edit_mode)
game_session.set_editmode(edit_mode);
}

void
Level_pause_target_timer()
{
SCRIPT_GUARD_GAMESESSION;
game_session.set_target_timer_paused(true);
}

void
Level_resume_target_timer()
{
SCRIPT_GUARD_GAMESESSION;
game_session.set_target_timer_paused(false);
}

} // namespace scripting

/* EOF */
10 changes: 10 additions & 0 deletions src/scripting/level.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ void Level_toggle_pause();
*/
void Level_edit(bool edit_mode);

/**
* Pauses the target timer.
*/
void Level_pause_target_timer();

/**
* Resumes the target timer.
*/
void Level_resume_target_timer();

#ifdef DOXYGEN_SCRIPTING
}
#endif
Expand Down
52 changes: 52 additions & 0 deletions src/scripting/wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12471,6 +12471,44 @@ static SQInteger Level_edit_wrapper(HSQUIRRELVM vm)

}

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

try {
scripting::Level_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 'Level_pause_target_timer'"));
return SQ_ERROR;
}

}

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

try {
scripting::Level_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 'Level_resume_target_timer'"));
return SQ_ERROR;
}

}

} // namespace wrapper
void create_squirrel_instance(HSQUIRRELVM v, scripting::AmbientSound* object, bool setup_releasehook)
{
Expand Down Expand Up @@ -13747,6 +13785,20 @@ void register_supertux_wrapper(HSQUIRRELVM v)
throw SquirrelError(v, "Couldn't register function 'Level_edit'");
}

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

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

// Register class AmbientSound
sq_pushstring(v, "AmbientSound", -1);
if(sq_newclass(v, SQFalse) < 0) {
Expand Down
22 changes: 17 additions & 5 deletions 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 Expand Up @@ -824,11 +826,21 @@ GameSession::start_sequence(Player* caller, Sequence seq, const SequenceData* da
}

// Stop all clocks.
for (const auto& obj : m_currentsector->get_objects())
for (LevelTime& lt : m_currentsector->get_objects_by_type<LevelTime>())
{
auto lt = dynamic_cast<LevelTime*>(obj.get());
if (lt)
lt->stop();
lt.stop();
}
}
void
GameSession::set_target_timer_paused(bool paused)
{
m_pause_target_timer = paused;
for (LevelTime& lt : m_currentsector->get_objects_by_type<LevelTime>())
{
if(paused)
lt.stop();
else
lt.start();
}
}

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);

/**
* 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 6161ee6

Please sign in to comment.