Skip to content

Commit

Permalink
rename unload_level and add pre_spawn_level
Browse files Browse the repository at this point in the history
  • Loading branch information
Dregu committed Oct 11, 2023
1 parent 26e3165 commit eeeeabf
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 25 deletions.
14 changes: 14 additions & 0 deletions src/game_api/script/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
class JournalPage;
struct AABB;

auto g_level_loaded = false;

void pre_load_level_files()
{
LuaBackend::for_each_backend(
Expand Down Expand Up @@ -74,6 +76,7 @@ bool pre_load_screen()
}
bool pre_unload_level()
{
g_level_loaded = false;
bool block{false};
LuaBackend::for_each_backend(
[&](LuaBackend::LockedBackend backend)
Expand Down Expand Up @@ -207,6 +210,17 @@ void post_tile_code_spawn(std::string_view tile_code, float x, float y, int laye

Entity* pre_entity_spawn(std::uint32_t entity_type, float x, float y, int layer, Entity* overlay, int spawn_type_flags)
{
if (!g_level_loaded)
{
g_level_loaded = true;
LuaBackend::for_each_backend(
[=](LuaBackend::LockedBackend backend)
{
backend->pre_spawn();
return true;
});
}

Entity* spawned_ent{nullptr};
LuaBackend::for_each_backend(
[=, &spawned_ent](LuaBackend::LockedBackend backend)
Expand Down
30 changes: 26 additions & 4 deletions src/game_api/script/lua_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ bool LuaBackend::pre_unload_level()
if (is_callback_cleared(id))
continue;

if (callback.screen == ON::PRE_UNLOAD_LEVEL)
if (callback.screen == ON::PRE_LEVEL_DESTRUCTION)
{
set_current_callback(-1, id, CallbackType::Normal);
auto return_value = handle_function<bool>(this, callback.func).value_or(false);
Expand All @@ -903,7 +903,7 @@ bool LuaBackend::pre_unload_layer(LAYER layer)
if (is_callback_cleared(id))
continue;

if (callback.screen == ON::PRE_UNLOAD_LAYER)
if (callback.screen == ON::PRE_LAYER_DESTRUCTION)
{
set_current_callback(-1, id, CallbackType::Normal);
auto return_value = handle_function<bool>(this, callback.func, layer).value_or(false);
Expand Down Expand Up @@ -1047,7 +1047,7 @@ void LuaBackend::post_unload_level()
if (is_callback_cleared(id))
continue;

if (callback.screen == ON::POST_UNLOAD_LEVEL)
if (callback.screen == ON::POST_LEVEL_DESTRUCTION)
{
set_current_callback(-1, id, CallbackType::Normal);
handle_function<void>(this, callback.func);
Expand All @@ -1068,7 +1068,7 @@ void LuaBackend::post_unload_layer(LAYER layer)
if (is_callback_cleared(id))
continue;

if (callback.screen == ON::POST_UNLOAD_LAYER)
if (callback.screen == ON::POST_LAYER_DESTRUCTION)
{
set_current_callback(-1, id, CallbackType::Normal);
handle_function<void>(this, callback.func, layer);
Expand Down Expand Up @@ -1209,6 +1209,28 @@ void LuaBackend::post_entity_spawn(Entity* entity, int spawn_type_flags)
}
}

void LuaBackend::pre_spawn()
{
if (!get_enabled())
return;

auto now = get_frame_count();

for (auto& [id, callback] : callbacks)
{
if (is_callback_cleared(id))
continue;

if (callback.screen == ON::PRE_LEVEL_SPAWN)
{
set_current_callback(-1, id, CallbackType::Normal);
handle_function<void>(this, callback.func);
clear_current_callback();
callback.lastRan = now;
}
}
}

bool LuaBackend::pre_entity_instagib(Entity* victim)
{
bool skip{false};
Expand Down
10 changes: 6 additions & 4 deletions src/game_api/script/lua_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,11 @@ enum class ON
PRE_UPDATE,
POST_UPDATE,
USER_DATA,
PRE_UNLOAD_LEVEL,
POST_UNLOAD_LEVEL,
PRE_UNLOAD_LAYER,
POST_UNLOAD_LAYER,
PRE_LEVEL_DESTRUCTION,
POST_LEVEL_DESTRUCTION,
PRE_LAYER_DESTRUCTION,
POST_LAYER_DESTRUCTION,
PRE_LEVEL_SPAWN,
};

struct IntOption
Expand Down Expand Up @@ -389,6 +390,7 @@ class LuaBackend

Entity* pre_entity_spawn(std::uint32_t entity_type, float x, float y, int layer, Entity* overlay, int spawn_type_flags);
void post_entity_spawn(Entity* entity, int spawn_type_flags);
void pre_spawn();

bool pre_entity_instagib(Entity* victim);

Expand Down
39 changes: 22 additions & 17 deletions src/game_api/script/lua_vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2255,14 +2255,17 @@ end
ON::POST_UPDATE,
"USER_DATA",
ON::USER_DATA,
"PRE_UNLOAD_LEVEL",
ON::PRE_UNLOAD_LEVEL,
"POST_UNLOAD_LEVEL",
ON::POST_UNLOAD_LEVEL,
"PRE_UNLOAD_LAYER",
ON::PRE_UNLOAD_LAYER,
"POST_UNLOAD_LAYER",
ON::POST_UNLOAD_LAYER);
"PRE_LEVEL_DESTRUCTION",
ON::PRE_LEVEL_DESTRUCTION,
"POST_LEVEL_DESTRUCTION",
ON::POST_LEVEL_DESTRUCTION,
"PRE_LAYER_DESTRUCTION",
ON::PRE_LAYER_DESTRUCTION,
"POST_LAYER_DESTRUCTION",
ON::POST_LAYER_DESTRUCTION,
"PRE_LEVEL_SPAWN",
ON::PRE_LEVEL_SPAWN);

/* ON
// LOGO
// Runs when entering the the mossmouth logo screen.
Expand Down Expand Up @@ -2339,7 +2342,7 @@ end
// Params: PreLoadLevelFilesContext load_level_ctx
// Runs right before level files would be loaded
// PRE_LEVEL_GENERATION
// Runs before any level generation, no entities should exist at this point
// Runs before any level generation, no entities should exist at this point. Does not work in all level-like screens, see PRE_LEVEL_SPAWN.
// POST_ROOM_GENERATION
// Params: PostRoomGenerationContext room_gen_ctx
// Runs right after all rooms are generated before entities are spawned
Expand Down Expand Up @@ -2474,16 +2477,18 @@ end
// USER_DATA
// Params: Entity ent
// Runs on all changes to Entity.user_data, including after loading saved user_data in the next level and transition. Also runs the first time user_data is set back to nil, but nil won't be saved to bother you on future levels.
// PRE_UNLOAD_LEVEL
// Runs right before the current level is unloaded and any entities destroyed
// POST_UNLOAD_LEVEL
// Runs right after the current level has been unloaded and all entities destroyed
// PRE_UNLOAD_LAYER
// PRE_LEVEL_DESTRUCTION
// Runs right before the current level is unloaded and any entities destroyed. Runs in pretty much all screens, even ones without entities. The screen has already changed at this point, meaning the screen being destoyed is in state.screen_last.
// POST_LEVEL_DESTRUCTION
// Runs right after the current level has been unloaded and all entities destroyed. Runs in pretty much all screens, even ones without entities. The screen has already changed at this point, meaning the screen being destoyed is in state.screen_last.
// PRE_LAYER_DESTRUCTION
// Params: LAYER layer
// Runs right before a layer is unloaded and any entities there destroyed
// POST_UNLOAD_LAYER
// Runs right before a layer is unloaded and any entities there destroyed. Runs in pretty much all screens, even ones without entities. The screen has already changed at this point, meaning the screen being destoyed is in state.screen_last.
// POST_LAYER_DESTRUCTION
// Params: LAYER layer
// Runs right after a layer has been unloaded and any entities there destroyed
// Runs right after a layer has been unloaded and any entities there destroyed. Runs in pretty much all screens, even ones without entities. The screen has already changed at this point, meaning the screen being destoyed is in state.screen_last.
// PRE_LEVEL_SPAWN
// Runs right before the first entity in any screen is spawned. Doesn't run if the screen doesn't spawn entities. You should probably prefer PRE_LEVEL_GENERATION, but that doesn't support all level-like screens.
*/

lua.create_named_table(
Expand Down

0 comments on commit eeeeabf

Please sign in to comment.