Files
impostor/inc/init/init.context.lua
Zsolt Tasnadi 03b6567c08
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Timer and Day modules
2026-03-03 19:51:22 +01:00

89 lines
2.9 KiB
Lua

local SAVE_GAME_BANK = 6
local SAVE_GAME_MAGIC_VALUE_ADDRESS = 0
local SAVE_GAME_MAGIC_VALUE = 0xCA
--- Global game context.
--- @section Context
Context = {}
--- Gets initial data for Context.
--- @within Context
--- @return result table Initial context data or nil. </br>
--- Fields: </br>
--- * current_menu_item (number) Index of the currently selected menu item.<br/>
--- * splash_timer (number) Remaining frames for the splash screen timer.<br/>
--- * popup (table) Popup window state. Contains: `show` (boolean) whether popup is visible, `content` (table) array of strings to display.<br/>
--- * game_in_progress (boolean) Whether a game is currently active.<br/>
--- * minigame_ddr (table) DDR minigame state (see Minigame.get_default_ddr).<br/>
--- * minigame_button_mash (table) Button mash minigame state (see Minigame.get_default_button_mash).<br/>
--- * minigame_rhythm (table) Rhythm minigame state (see Minigame.get_default_rhythm).<br/>
--- * meters (table) Meter values (see Meter.get_initial).<br/>
--- * stat_screen_active (boolean) Whether the stat screen overlay is currently shown.<br/>
--- * game (table) Current game progress state. Contains: `current_screen` (string) active screen ID, `current_situation` (string|nil) active situation ID.<br/>
function Context.initial_data()
return {
current_menu_item = 1,
splash_timer = Config.timing.splash_duration,
popup = {
show = false,
content = {}
},
game_in_progress = false,
stat_screen_active = false,
minigame_ddr = Minigame.get_default_ddr(),
minigame_button_mash = Minigame.get_default_button_mash(),
minigame_rhythm = Minigame.get_default_rhythm(),
meters = Meter.get_initial(),
timer = Timer.get_initial(),
game = {
current_screen = "home",
current_situation = nil,
},
day_count = 1,
}
end
--- Resets game context to initial state.
--- @within Context
function Context.reset()
local initial_data = Context.initial_data()
for k in pairs(Context) do
if type(Context[k]) ~= "function" then
Context[k] = nil
end
end
for k, v in pairs(initial_data) do
Context[k] = v
end
end
--- Starts a new game.
--- @within Context
function Context.new_game()
Context.reset()
Context.game_in_progress = true
MenuWindow.refresh_menu_items()
Screen.get_by_id(Context.game.current_screen).init()
end
--- Saves the current game state.
--- @within Context
function Context.save_game()
if not Context.game_in_progress then return end
mset(SAVE_GAME_MAGIC_VALUE, SAVE_GAME_MAGIC_VALUE_ADDRESS, SAVE_GAME_BANK)
end
--- Loads a saved game state.
--- @within Context
function Context.load_game()
if mget(SAVE_GAME_MAGIC_VALUE_ADDRESS, SAVE_GAME_BANK) ~= SAVE_GAME_MAGIC_VALUE then
Context.new_game()
return
end
Context.reset()
Context.game_in_progress = true
MenuWindow.refresh_menu_items()
Screen.get_by_id(Context.game.current_screen).init()
end