From 0243c661744f49f6d270175f46b9e3116c83544a Mon Sep 17 00:00:00 2001 From: Lemonymous <10490534+Lemonymous@users.noreply.github.com> Date: Wed, 7 Sep 2022 21:57:18 +0200 Subject: [PATCH] Add onGameStateChanged and onIslandLeft events ## Events - modApi.events.onGameStateChanged:dispatch(newGameState, oldGameState) - modApi.events.onIslandLeft:dispatch(island) Game States: ``` Main_Menu Hangar End_Game_Screen Map Island Mission Mission_Test ``` --- scripts/mod_loader/bootstrap/modApi.lua | 2 + scripts/mod_loader/modapi/__scripts.lua | 1 + scripts/mod_loader/modapi/gameState.lua | 97 +++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 scripts/mod_loader/modapi/gameState.lua diff --git a/scripts/mod_loader/bootstrap/modApi.lua b/scripts/mod_loader/bootstrap/modApi.lua index e40176b9..38dbcf30 100644 --- a/scripts/mod_loader/bootstrap/modApi.lua +++ b/scripts/mod_loader/bootstrap/modApi.lua @@ -101,6 +101,8 @@ t.onWindowHidden = Event() t.onMissionChanged = Event() t.onMissionDismissed = Event() +t.onIslandLeft = Event() +t.onGameStateChanged = Event() t.onGameVictory = Event() t.onSquadEnteredGame = Event() t.onSquadExitedGame = Event() diff --git a/scripts/mod_loader/modapi/__scripts.lua b/scripts/mod_loader/modapi/__scripts.lua index bce214cf..499bf4ef 100644 --- a/scripts/mod_loader/modapi/__scripts.lua +++ b/scripts/mod_loader/modapi/__scripts.lua @@ -27,6 +27,7 @@ local scripts = { "localization", "compat", "deployment", + "gameState", } local rootpath = GetParentPath(...) diff --git a/scripts/mod_loader/modapi/gameState.lua b/scripts/mod_loader/modapi/gameState.lua new file mode 100644 index 00000000..9005974e --- /dev/null +++ b/scripts/mod_loader/modapi/gameState.lua @@ -0,0 +1,97 @@ + +GAME_STATE = { + MAIN_MENU = "Main_Menu", + HANGAR = "Hangar", + END_GAME_SCREEN = "End_Game_Screen", + MAP = "Map", + ISLAND = "Island", + MISSION = "Mission", + MISSION_TEST = "Mission_Test", +} + +local currentGameState = GAME_STATE.MAIN_MENU + +function modApi:getGameState() + return currentGameState +end + +-- Check current sector each frame. If an increment is detected, +-- it means we have left an island. When leaving the 4th island, +-- sector does not increase, so no event is dispatched. +-- Event onGameStateChanged can be used to detect when the island +-- has been fully left, when it is reflected in the save data. +modApi.events.onFrameDrawStart:subscribe(function() + if not Game or not GAME then + return + end + + local sector = Game:GetSector() + + if GAME.currentSector == nil then + GAME.currentSector = sector + elseif GAME.currentSector < sector then + GAME.currentSector = sector + + modApi.events.onIslandLeft:dispatch(GAME.Island) + end +end) + +local function setGameState(gameState) + local oldGameState = currentGameState + + if currentGameState ~= gameState then + currentGameState = gameState + + if GAME then + if currentGameState == GAME_STATE.ISLAND or currentGameState == GAME_STATE.MAP then + -- fallbackGameState is either map or island. + -- When exiting a mission, or entering the game outside of a mission, + -- we will fall back to either of the two. + GAME.fallbackGameState = currentGameState + end + GAME.currentGameState = currentGameState + end + + modApi.events.onGameStateChanged:dispatch(currentGameState, oldGameState) + end +end + +modApi.events.onMainMenuEntered:subscribe(function() + setGameState(GAME_STATE.MAIN_MENU) +end) + +modApi.events.onHangarEntered:subscribe(function() + setGameState(GAME_STATE.HANGAR) +end) + +modApi.events.onGameExited:subscribe(function() + if not sdlext.isMainMenu() then + setGameState(GAME_STATE.END_GAME_SCREEN) + end +end) + +modApi.events.onGameEntered:subscribe(function() + if GAME.currentGameState == nil or GAME.currentGameState == GAME_STATE.MISSION_TEST then + setGameState(GAME.fallbackGameState or GAME_STATE.MAP) + else + setGameState(GAME.currentGameState) + end +end) + +modApi.events.onPreIslandSelection:subscribe(function() + setGameState(GAME_STATE.ISLAND) +end) + +modApi.events.onIslandLeft:subscribe(function() + setGameState(GAME_STATE.MAP) +end) + +modApi.events.onMissionChanged:subscribe(function(mission, oldMission) + if mission == Mission_Test then + setGameState(GAME_STATE.MISSION_TEST) + elseif mission then + setGameState(GAME_STATE.MISSION) + elseif sdlext.isGame() then + setGameState(GAME.fallbackGameState) + end +end)