Skip to content

Commit

Permalink
Add onGameStateChanged and onIslandLeft events
Browse files Browse the repository at this point in the history
## 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
```
  • Loading branch information
Lemonymous authored and KnightMiner committed Sep 18, 2022
1 parent 57f9150 commit 0243c66
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions scripts/mod_loader/bootstrap/modApi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions scripts/mod_loader/modapi/__scripts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ local scripts = {
"localization",
"compat",
"deployment",
"gameState",
}

local rootpath = GetParentPath(...)
Expand Down
97 changes: 97 additions & 0 deletions scripts/mod_loader/modapi/gameState.lua
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 0243c66

Please sign in to comment.