Skip to content

Commit

Permalink
Add Level.has_active_sequence() to scripting API. (#2625)
Browse files Browse the repository at this point in the history
(plz hear me out on this one)

This adds Level.has_active_sequence() to the scripting API. which is basically the getter for Level.finish(). It allows you tell when the end stats screen is displayed.
  • Loading branch information
tylerandari13 authored Sep 13, 2023
1 parent d950d26 commit 78e1d70
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 deletions.
1 change: 1 addition & 0 deletions data/scripts/default.nut
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//Create Level table
Level <- {
finish=Level_finish,
has_active_sequence=Level_has_active_sequence,
spawn=Level_spawn,
set_start_point=Level_set_start_point,
set_start_pos=Level_set_start_pos,
Expand Down
29 changes: 18 additions & 11 deletions src/scripting/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,78 +25,85 @@ namespace scripting {
void
Level_finish(bool win)
{
SCRIPT_GUARD_GAMESESSION;
SCRIPT_GUARD_GAMESESSION();
game_session.finish(win);
}

bool
Level_has_active_sequence()
{
SCRIPT_GUARD_GAMESESSION(false);
return game_session.has_active_sequence();
}

void
Level_spawn(const std::string& sector, const std::string& spawnpoint)
{
SCRIPT_GUARD_GAMESESSION;
SCRIPT_GUARD_GAMESESSION();
game_session.respawn(sector, spawnpoint);
}

void
Level_set_start_point(const std::string& sector, const std::string& spawnpoint)
{
SCRIPT_GUARD_GAMESESSION;
SCRIPT_GUARD_GAMESESSION();
game_session.set_start_point(sector, spawnpoint);
}

void
Level_set_start_pos(const std::string& sector, float x, float y)
{
SCRIPT_GUARD_GAMESESSION;
SCRIPT_GUARD_GAMESESSION();
game_session.set_start_pos(sector, Vector(x, y));
}

void
Level_set_respawn_point(const std::string& sector, const std::string& spawnpoint)
{
SCRIPT_GUARD_GAMESESSION;
SCRIPT_GUARD_GAMESESSION();
game_session.set_respawn_point(sector, spawnpoint);
}

void
Level_set_respawn_pos(const std::string& sector, float x, float y)
{
SCRIPT_GUARD_GAMESESSION;
SCRIPT_GUARD_GAMESESSION();
game_session.set_respawn_pos(sector, Vector(x, y));
}

void
Level_flip_vertically()
{
SCRIPT_GUARD_GAMESESSION;
SCRIPT_GUARD_GAMESESSION();
FlipLevelTransformer flip_transformer;
flip_transformer.transform(game_session.get_current_level());
}

void
Level_toggle_pause()
{
SCRIPT_GUARD_GAMESESSION;
SCRIPT_GUARD_GAMESESSION();
game_session.toggle_pause();
}

void
Level_edit(bool edit_mode)
{
SCRIPT_GUARD_GAMESESSION;
SCRIPT_GUARD_GAMESESSION();
game_session.set_editmode(edit_mode);
}

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

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

Expand Down
8 changes: 6 additions & 2 deletions src/scripting/level.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
#include <string>

/** Macro to help easily check if there is a current GameSession and define it, if so. **/
#define SCRIPT_GUARD_GAMESESSION \
if (!GameSession::current()) return; \
#define SCRIPT_GUARD_GAMESESSION(returnvalue) \
if (!GameSession::current()) return returnvalue; \
GameSession& game_session = *GameSession::current()

#endif
Expand All @@ -51,6 +51,10 @@ class Level
* @param bool $win If ""true"", the level is marked as completed if launched from a worldmap.
*/
void Level_finish(bool win);
/**
* Gets whether an end sequence has started. (AKA when the stats at the end are visible)
*/
bool Level_has_active_sequence();

/**
* Respawns Tux in sector named ""sector"" at spawnpoint named ""spawnpoint"".${SRG_TABLENEWPARAGRAPH}
Expand Down
26 changes: 26 additions & 0 deletions src/scripting/wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12530,6 +12530,25 @@ static SQInteger Level_finish_wrapper(HSQUIRRELVM vm)

}

static SQInteger Level_has_active_sequence_wrapper(HSQUIRRELVM vm)
{

try {
bool return_value = scripting::Level_has_active_sequence();

sq_pushbool(vm, return_value);
return 1;

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

}

static SQInteger Level_spawn_wrapper(HSQUIRRELVM vm)
{
const SQChar* arg0;
Expand Down Expand Up @@ -13999,6 +14018,13 @@ void register_supertux_wrapper(HSQUIRRELVM v)
throw SquirrelError(v, "Couldn't register function 'Level_finish'");
}

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

sq_pushstring(v, "Level_spawn", -1);
sq_newclosure(v, &Level_spawn_wrapper, 0);
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".ss");
Expand Down
6 changes: 6 additions & 0 deletions src/supertux/game_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,12 @@ GameSession::get_working_directory() const
return FileSystem::dirname(m_levelfile);
}

bool
GameSession::has_active_sequence() const
{
return m_end_sequence;
}

void
GameSession::start_sequence(Player* caller, Sequence seq, const SequenceData* data)
{
Expand Down
1 change: 1 addition & 0 deletions src/supertux/game_session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class GameSession final : public Screen,
* resources for the current level/world
*/
std::string get_working_directory() const;
bool has_active_sequence() const;
int restart_level(bool after_death = false);
bool reset_button;
bool reset_checkpoint_button;
Expand Down

0 comments on commit 78e1d70

Please sign in to comment.