refact
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2026-02-22 18:15:24 +01:00
parent d9febf16e0
commit 83e2000198
10 changed files with 166 additions and 104 deletions

View File

@@ -4,6 +4,9 @@ local SAVE_GAME_MAGIC_VALUE = 0xCA
local SAVE_GAME_CURRENT_SCREEN_ADDRESS = 6
-- Define a consistent order for screens for save/load operations
local SCREEN_ID_ORDER = {"home", "toilet", "walking_to_office", "office", "walking_to_home"}
--- Gets initial data for Context.
-- @return table Initial context data.
local function get_initial_data()
@@ -16,15 +19,13 @@ local function get_initial_data()
seems ordinary: work,
meetings, coffee, and
endless notifications.
But beneath the surface
— within him, or around
But beneath him, or around
him — something is
constantly building, and
it soon becomes clear
that there is more going
on than meets the eye.]]
},
current_screen = 1,
splash_timer = Config.timing.splash_duration,
popup = {
show = false,
@@ -41,17 +42,15 @@ on than meets the eye.]]
},
menu_items = {},
selected_menu_item = 1,
selected_decision_index = 1,
game_in_progress = false,
screens = {},
minigame_ddr = Minigame.get_default_ddr(),
minigame_button_mash = Minigame.get_default_button_mash(),
minigame_rhythm = Minigame.get_default_rhythm(),
meters = Meter.get_initial(),
--- Active sprites.
sprites = {},
--- Current situation ID.
current_situation = nil,
game = {
current_screen = "home",
current_situation = nil,
}
}
end
@@ -70,29 +69,15 @@ local function reset_context_to_initial_state()
for k, v in pairs(initial_data) do
Context[k] = v
end
Context.screens = {}
Context.screen_indices_by_id = {}
local screen_order = {"home", "toilet", "walking_to_office", "office", "walking_to_home"}
for i, screen_id in ipairs(screen_order) do
local screen_data = Screen.get_by_id(screen_id)
if screen_data then
table.insert(Context.screens, screen_data)
Context.screen_indices_by_id[screen_id] = i
else
PopupWindow.show({"Error: Screen '" .. screen_id .. "' not registered!"})
end
end
end
reset_context_to_initial_state()
--- Starts a new game.
function Context.new_game()
reset_context_to_initial_state()
Context.game_in_progress = true
MenuWindow.refresh_menu_items()
Context.screens[Context.current_screen].init()
Screen.get_by_id(Context.game.current_screen).init()
end
--- Saves the current game state.
@@ -100,7 +85,20 @@ 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)
mset(Context.current_screen, SAVE_GAME_CURRENT_SCREEN_ADDRESS, SAVE_GAME_BANK)
local screen_index_to_save
for i, screen_id in ipairs(SCREEN_ID_ORDER) do
if screen_id == Context.game.current_screen then
screen_index_to_save = i
break
end
end
if screen_index_to_save then
mset(screen_index_to_save, SAVE_GAME_CURRENT_SCREEN_ADDRESS, SAVE_GAME_BANK)
else
trace("Error: Current screen ID '" .. Context.game.current_screen .. "' not found in SCREEN_ID_ORDER for saving.")
end
end
--- Loads a saved game state.
@@ -111,9 +109,17 @@ function Context.load_game()
end
reset_context_to_initial_state()
Context.current_screen = mget(SAVE_GAME_CURRENT_SCREEN_ADDRESS, SAVE_GAME_BANK)
local loaded_screen_index = mget(SAVE_GAME_CURRENT_SCREEN_ADDRESS, SAVE_GAME_BANK)
if loaded_screen_index and SCREEN_ID_ORDER[loaded_screen_index] then
Context.game.current_screen = SCREEN_ID_ORDER[loaded_screen_index]
else
trace("Error: Invalid screen index loaded: " .. tostring(loaded_screen_index) .. ". Defaulting to new game state.")
Context.new_game()
return
end
Context.game_in_progress = true
MenuWindow.refresh_menu_items()
Context.screens[Context.current_screen].init()
Screen.get_by_id(Context.game.current_screen).init()
end