From 0357eb415e8efc8fb28bf312bbab92b938471173 Mon Sep 17 00:00:00 2001 From: Zsolt Tasnadi Date: Tue, 17 Feb 2026 21:37:53 +0100 Subject: [PATCH] desition and screen magement refactor --- impostor.inc | 17 ++++- inc/desition/desition.go_to_home.lua | 14 +++++ inc/desition/desition.go_to_office.lua | 14 +++++ inc/desition/desition.go_to_toilet.lua | 14 +++++ .../desition.go_to_walking_to_home.lua | 14 +++++ .../desition.go_to_walking_to_office.lua | 14 +++++ inc/desition/desition.manager.lua | 42 +++++++++++++ inc/desition/desition.play_button_mash.lua | 6 ++ inc/desition/desition.play_ddr.lua | 6 ++ inc/desition/desition.play_rhythm.lua | 6 ++ inc/init/init.context.lua | 62 ++++++------------- inc/init/init.modules.lua | 3 +- inc/screen/screen.home.lua | 8 +++ inc/screen/screen.manager.lua | 27 ++++++++ inc/screen/screen.office.lua | 10 +++ inc/screen/screen.toilet.lua | 7 +++ inc/screen/screen.walking_to_home.lua | 8 +++ inc/screen/screen.walking_to_office.lua | 8 +++ inc/system/system.desition_manager.lua | 42 ------------- inc/system/system.desitions.lua | 33 ---------- inc/system/system.util.lua | 10 +-- inc/window/window.game.lua | 32 +++++++--- 22 files changed, 256 insertions(+), 141 deletions(-) create mode 100644 inc/desition/desition.go_to_home.lua create mode 100644 inc/desition/desition.go_to_office.lua create mode 100644 inc/desition/desition.go_to_toilet.lua create mode 100644 inc/desition/desition.go_to_walking_to_home.lua create mode 100644 inc/desition/desition.go_to_walking_to_office.lua create mode 100644 inc/desition/desition.manager.lua create mode 100644 inc/desition/desition.play_button_mash.lua create mode 100644 inc/desition/desition.play_ddr.lua create mode 100644 inc/desition/desition.play_rhythm.lua create mode 100644 inc/screen/screen.home.lua create mode 100644 inc/screen/screen.manager.lua create mode 100644 inc/screen/screen.office.lua create mode 100644 inc/screen/screen.toilet.lua create mode 100644 inc/screen/screen.walking_to_home.lua create mode 100644 inc/screen/screen.walking_to_office.lua delete mode 100644 inc/system/system.desition_manager.lua delete mode 100644 inc/system/system.desitions.lua diff --git a/impostor.inc b/impostor.inc index 044b83d..d69c758 100644 --- a/impostor.inc +++ b/impostor.inc @@ -3,11 +3,24 @@ init/init.modules.lua init/init.config.lua system/system.util.lua init/init.windows.lua +desition/desition.manager.lua +desition/desition.go_to_home.lua +desition/desition.go_to_toilet.lua +desition/desition.go_to_walking_to_office.lua +desition/desition.go_to_office.lua +desition/desition.go_to_walking_to_home.lua +desition/desition.play_button_mash.lua +desition/desition.play_rhythm.lua +desition/desition.play_ddr.lua +screen/screen.manager.lua +screen/screen.home.lua +screen/screen.toilet.lua +screen/screen.walking_to_office.lua +screen/screen.office.lua +screen/screen.walking_to_home.lua init/init.context.lua data/data.songs.lua system/system.print.lua -system/system.desitions.lua -system/system.desition_manager.lua system/system.input.lua system/system.audio.lua system/system.ui.lua diff --git a/inc/desition/desition.go_to_home.lua b/inc/desition/desition.go_to_home.lua new file mode 100644 index 0000000..3bcdd99 --- /dev/null +++ b/inc/desition/desition.go_to_home.lua @@ -0,0 +1,14 @@ +DesitionManager.register({ + id = "go_to_home", + label = "Go to Home", + handle = function() + local screen_index = Context.screen_indices_by_id["home"] + if screen_index then + Context.current_screen = screen_index + Context.selected_desition_index = 1 + else + PopupWindow.show({"Error: Screen 'home' not found or not indexed!"}) + end + end, + condition = function() return true end +}) diff --git a/inc/desition/desition.go_to_office.lua b/inc/desition/desition.go_to_office.lua new file mode 100644 index 0000000..ff0c32f --- /dev/null +++ b/inc/desition/desition.go_to_office.lua @@ -0,0 +1,14 @@ +DesitionManager.register({ + id = "go_to_office", + label = "Go to Office", + handle = function() + local screen_index = Context.screen_indices_by_id["office"] + if screen_index then + Context.current_screen = screen_index + Context.selected_desition_index = 1 + else + PopupWindow.show({"Error: Screen 'office' not found or not indexed!"}) + end + end, + condition = function() return true end +}) diff --git a/inc/desition/desition.go_to_toilet.lua b/inc/desition/desition.go_to_toilet.lua new file mode 100644 index 0000000..e3d5d52 --- /dev/null +++ b/inc/desition/desition.go_to_toilet.lua @@ -0,0 +1,14 @@ +DesitionManager.register({ + id = "go_to_toilet", + label = "Go to Toilet", + handle = function() + local screen_index = Context.screen_indices_by_id["toilet"] + if screen_index then + Context.current_screen = screen_index + Context.selected_desition_index = 1 + else + PopupWindow.show({"Error: Screen 'toilet' not found or not indexed!"}) + end + end, + condition = function() return true end +}) diff --git a/inc/desition/desition.go_to_walking_to_home.lua b/inc/desition/desition.go_to_walking_to_home.lua new file mode 100644 index 0000000..4bfe117 --- /dev/null +++ b/inc/desition/desition.go_to_walking_to_home.lua @@ -0,0 +1,14 @@ +DesitionManager.register({ + id = "go_to_walking_to_home", + label = "Go to Walking to home", + handle = function() + local screen_index = Context.screen_indices_by_id["walking_to_home"] + if screen_index then + Context.current_screen = screen_index + Context.selected_desition_index = 1 + else + PopupWindow.show({"Error: Screen 'walking_to_home' not found or not indexed!"}) + end + end, + condition = function() return true end +}) diff --git a/inc/desition/desition.go_to_walking_to_office.lua b/inc/desition/desition.go_to_walking_to_office.lua new file mode 100644 index 0000000..f424b0a --- /dev/null +++ b/inc/desition/desition.go_to_walking_to_office.lua @@ -0,0 +1,14 @@ +DesitionManager.register({ + id = "go_to_walking_to_office", + label = "Go to Walking to office", + handle = function() + local screen_index = Context.screen_indices_by_id["walking_to_office"] + if screen_index then + Context.current_screen = screen_index + Context.selected_desition_index = 1 + else + PopupWindow.show({"Error: Screen 'walking_to_office' not found or not indexed!"}) + end + end, + condition = function() return true end +}) diff --git a/inc/desition/desition.manager.lua b/inc/desition/desition.manager.lua new file mode 100644 index 0000000..1c43b7f --- /dev/null +++ b/inc/desition/desition.manager.lua @@ -0,0 +1,42 @@ +DesitionManager = {} + +local _desitions = {} -- Private table to store all desitions + +-- Registers a decision object with the manager +-- desition_object: A table containing id, label, handle(), and condition() +function DesitionManager.register(desition_object) + if not desition_object or not desition_object.id then + PopupWindow.show({"Error: Invalid desition object registered (missing id)!"}) + return + end + if not desition_object.label then + PopupWindow.show({"Error: Invalid desition object registered (missing label)!"}) + return + end + + -- Ensure handle() and condition() methods exist with defaults if missing + if not desition_object.condition then + desition_object.condition = function() return true end + end + if not desition_object.handle then + desition_object.handle = function() end + end + + if _desitions[desition_object.id] then + -- Optional: warning if overwriting an existing desition + -- trace("Warning: Overwriting desition with id: " .. desition_object.id) + end + _desitions[desition_object.id] = desition_object +end + +-- Retrieves a desition by its id +-- id: unique string identifier of the desition +-- Returns the desition object, or nil if not found +function DesitionManager.get(id) + return _desitions[id] +end + +-- Optional: a way to get all registered desitions, if needed (e.g., for debug) +function DesitionManager.get_all() + return _desitions +end diff --git a/inc/desition/desition.play_button_mash.lua b/inc/desition/desition.play_button_mash.lua new file mode 100644 index 0000000..754e292 --- /dev/null +++ b/inc/desition/desition.play_button_mash.lua @@ -0,0 +1,6 @@ +DesitionManager.register({ + id = "play_button_mash", + label = "Play Button Mash", + handle = function() MinigameButtonMashWindow.start(WINDOW_GAME) end, + condition = function() return true end +}) diff --git a/inc/desition/desition.play_ddr.lua b/inc/desition/desition.play_ddr.lua new file mode 100644 index 0000000..f03be37 --- /dev/null +++ b/inc/desition/desition.play_ddr.lua @@ -0,0 +1,6 @@ +DesitionManager.register({ + id = "play_ddr", + label = "Play DDR (Random)", + handle = function() MinigameDDRWindow.start(WINDOW_GAME, nil) end, + condition = function() return true end +}) diff --git a/inc/desition/desition.play_rhythm.lua b/inc/desition/desition.play_rhythm.lua new file mode 100644 index 0000000..82688ec --- /dev/null +++ b/inc/desition/desition.play_rhythm.lua @@ -0,0 +1,6 @@ +DesitionManager.register({ + id = "play_rhythm", + label = "Play Rhythm Game", + handle = function() MinigameRhythmWindow.start(WINDOW_GAME) end, + condition = function() return true end +}) diff --git a/inc/init/init.context.lua b/inc/init/init.context.lua index 61f5afb..6f3a949 100644 --- a/inc/init/init.context.lua +++ b/inc/init/init.context.lua @@ -45,52 +45,12 @@ local function get_initial_data() selected_menu_item = 1, selected_desition_index = 1, -- New desition index game_in_progress = false, -- New flag - screens = clone_table({ - { - id = "home", - name = "Home", - decisions = { - "go_to_toilet", - "go_to_walking_to_office", - } - }, - { - id = "toilet", - name = "Toilet", - decisions = { - "go_to_home", - } - }, - { - id = "walking_to_office", - name = "Walking to office", - decisions = { - "go_to_home", - "go_to_office", - } - }, - { - id = "office", - name = "Office", - decisions = { - "play_button_mash", - "play_rhythm", - "play_ddr", - "go_to_walking_to_home", - } - }, - { - id = "walking_to_home", - name = "Walking to home", - decisions = { - "go_to_home", - "go_to_office", - } - } - }) + screens = {} -- Initialize as empty, populated on reset } end +Context = {} + local function reset_context_to_initial_state() local initial_data = get_initial_data() @@ -105,6 +65,22 @@ local function reset_context_to_initial_state() for k, v in pairs(initial_data) do Context[k] = v end + + -- Populate Context.screens from ScreenManager, ensuring indexed array + Context.screens = {} + Context.screen_indices_by_id = {} -- Renamed for clarity, stores index + -- The screen order needs to be explicit to ensure consistent numerical indices + local screen_order = {"home", "toilet", "walking_to_office", "office", "walking_to_home"} + for i, screen_id in ipairs(screen_order) do + local screen_data = ScreenManager.get_by_id(screen_id) + if screen_data then + table.insert(Context.screens, screen_data) + Context.screen_indices_by_id[screen_id] = i -- Store index + else + -- Handle error if a screen is not registered + PopupWindow.show({"Error: Screen '" .. screen_id .. "' not registered!"}) + end + end end -- Initially populate Context with data diff --git a/inc/init/init.modules.lua b/inc/init/init.modules.lua index 23df024..e98cb49 100644 --- a/inc/init/init.modules.lua +++ b/inc/init/init.modules.lua @@ -10,9 +10,8 @@ local MinigameRhythmWindow = {} local MinigameDDRWindow = {} local Util = {} -local Context = {} -local Desitions = {} local DesitionManager = {} +local ScreenManager = {} -- New declaration local UI = {} local Print = {} local Input = {} diff --git a/inc/screen/screen.home.lua b/inc/screen/screen.home.lua new file mode 100644 index 0000000..efb059c --- /dev/null +++ b/inc/screen/screen.home.lua @@ -0,0 +1,8 @@ +ScreenManager.register({ + id = "home", + name = "Home", + decisions = { + "go_to_toilet", + "go_to_walking_to_office", + } +}) diff --git a/inc/screen/screen.manager.lua b/inc/screen/screen.manager.lua new file mode 100644 index 0000000..8a2a08f --- /dev/null +++ b/inc/screen/screen.manager.lua @@ -0,0 +1,27 @@ +ScreenManager = {} + +local _screens = {} -- Internal list to hold screen data + +-- Public property to access the registered screens as an indexed array +function ScreenManager.get_screens_array() + local screens_array = {} + for _, screen_data in pairs(_screens) do + table.insert(screens_array, screen_data) + end + return screens_array +end + +-- Registers a screen with the manager +-- screen_data: A table containing id, name, and decisions for the screen +function ScreenManager.register(screen_data) + if _screens[screen_data.id] then + -- Optional: warning if overwriting an existing screen + -- trace("Warning: Overwriting screen with id: " .. screen_data.id) + end + _screens[screen_data.id] = screen_data +end + +-- Retrieves a screen by its id (if needed directly) +function ScreenManager.get_by_id(screen_id) + return _screens[screen_id] +end diff --git a/inc/screen/screen.office.lua b/inc/screen/screen.office.lua new file mode 100644 index 0000000..7a619b0 --- /dev/null +++ b/inc/screen/screen.office.lua @@ -0,0 +1,10 @@ +ScreenManager.register({ + id = "office", + name = "Office", + decisions = { + "play_button_mash", + "play_rhythm", + "play_ddr", + "go_to_walking_to_home", + } +}) diff --git a/inc/screen/screen.toilet.lua b/inc/screen/screen.toilet.lua new file mode 100644 index 0000000..ebe9543 --- /dev/null +++ b/inc/screen/screen.toilet.lua @@ -0,0 +1,7 @@ +ScreenManager.register({ + id = "toilet", + name = "Toilet", + decisions = { + "go_to_home", + } +}) diff --git a/inc/screen/screen.walking_to_home.lua b/inc/screen/screen.walking_to_home.lua new file mode 100644 index 0000000..a12739f --- /dev/null +++ b/inc/screen/screen.walking_to_home.lua @@ -0,0 +1,8 @@ +ScreenManager.register({ + id = "walking_to_home", + name = "Walking to home", + decisions = { + "go_to_home", + "go_to_office", + } +}) diff --git a/inc/screen/screen.walking_to_office.lua b/inc/screen/screen.walking_to_office.lua new file mode 100644 index 0000000..95a89df --- /dev/null +++ b/inc/screen/screen.walking_to_office.lua @@ -0,0 +1,8 @@ +ScreenManager.register({ + id = "walking_to_office", + name = "Walking to office", + decisions = { + "go_to_home", + "go_to_office", + } +}) diff --git a/inc/system/system.desition_manager.lua b/inc/system/system.desition_manager.lua deleted file mode 100644 index aae6183..0000000 --- a/inc/system/system.desition_manager.lua +++ /dev/null @@ -1,42 +0,0 @@ -DesitionManager = {} - -local _desitions = {} -- Private table to store all desitions - --- Registers a new desition with the manager --- id: unique string identifier for the desition --- label: string label to display for the desition --- handler: function to execute when the desition is selected -function DesitionManager.register(id, label, handler) - if _desitions[id] then - -- Optional: warning if overwriting an existing desition - -- trace("Warning: Overwriting desition with id: " .. id) - end - _desitions[id] = Util.create_decision(id, label, handler) -end - --- Retrieves a desition by its id --- id: unique string identifier of the desition --- Returns the desition object, or nil if not found -function DesitionManager.get(id) - return _desitions[id] -end - --- Optional: a way to get all registered desitions, if needed (e.g., for debug) -function DesitionManager.get_all() - return _desitions -end - --- Register all game desitions --- Home Screen Decisions -DesitionManager.register("go_to_toilet", "Go to Toilet", Desitions.go_to_toilet) -DesitionManager.register("go_to_walking_to_office", "Go to Walking to office", Desitions.go_to_walking_to_office) - --- Minigame functions -DesitionManager.register("play_button_mash", "Play Button Mash", Desitions.start_minigame_mash) -DesitionManager.register("play_rhythm", "Play Rhythm Game", Desitions.start_minigame_rhythm) -DesitionManager.register("play_ddr", "Play DDR (Random)", Desitions.start_minigame_ddr) - --- Navigation functions -DesitionManager.register("go_to_home", "Go to Home", Desitions.go_to_home) -DesitionManager.register("go_to_office", "Go to Office", Desitions.go_to_office) -DesitionManager.register("go_to_walking_to_home", "Go to Walking to home", Desitions.go_to_walking_to_home) diff --git a/inc/system/system.desitions.lua b/inc/system/system.desitions.lua deleted file mode 100644 index c5c5f6a..0000000 --- a/inc/system/system.desitions.lua +++ /dev/null @@ -1,33 +0,0 @@ -Desitions = {} - -function Desitions.go_to_screen(screen_id) - for i, screen_data in ipairs(Context.screens) do - if screen_data.id == screen_id then - Context.current_screen = i - Context.selected_desition_index = 1 -- Reset selected decision on new screen - return - end - end - -- Handle error: screen_id not found, perhaps show a debug message or a popup - PopupWindow.show({"Error: Screen '" .. screen_id .. "' not found!"}) -end - --- Specific navigation helpers -function Desitions.go_to_home() Desitions.go_to_screen("home") end -function Desitions.go_to_toilet() Desitions.go_to_screen("toilet") end -function Desitions.go_to_walking_to_office() Desitions.go_to_screen("walking_to_office") end -function Desitions.go_to_office() Desitions.go_to_screen("office") end -function Desitions.go_to_walking_to_home() Desitions.go_to_screen("walking_to_home") end - --- Minigame functions -function Desitions.start_minigame_mash() - MinigameButtonMashWindow.start(WINDOW_GAME) -end - -function Desitions.start_minigame_rhythm() - MinigameRhythmWindow.start(WINDOW_GAME) -end - -function Desitions.start_minigame_ddr(song_key) - MinigameDDRWindow.start(WINDOW_GAME, song_key) -end \ No newline at end of file diff --git a/inc/system/system.util.lua b/inc/system/system.util.lua index a1d0e19..0bbcf5e 100644 --- a/inc/system/system.util.lua +++ b/inc/system/system.util.lua @@ -1,13 +1,5 @@ Util = {} -function Util.create_decision(id, label, handler) - return { - id = id, - label = label, - handler = handler - } -end - function Util.safeindex(array, index) return ((index - 1 + #array) % #array) + 1 -end +end \ No newline at end of file diff --git a/inc/window/window.game.lua b/inc/window/window.game.lua index f0826b5..4bede42 100644 --- a/inc/window/window.game.lua +++ b/inc/window/window.game.lua @@ -4,11 +4,17 @@ function GameWindow.draw() UI.draw_top_bar(currentScreenData.name) if currentScreenData and currentScreenData.decisions and #currentScreenData.decisions > 0 then - local display_decisions = {} + local available_desitions = {} for _, desition_id in ipairs(currentScreenData.decisions) do - table.insert(display_decisions, DesitionManager.get(desition_id)) + local desition_obj = DesitionManager.get(desition_id) + if desition_obj and desition_obj.condition() then -- Check condition directly + table.insert(available_desitions, desition_obj) + end + end + -- If no available desitions, display nothing or a message + if #available_desitions > 0 then + UI.draw_desition_selector(available_desitions, Context.selected_desition_index) end - UI.draw_desition_selector(display_decisions, Context.selected_desition_index) end end @@ -37,14 +43,20 @@ function GameWindow.update() local currentScreenData = Context.screens[Context.current_screen] if currentScreenData and currentScreenData.decisions and #currentScreenData.decisions > 0 then - local display_decisions = {} + local available_desitions = {} for _, desition_id in ipairs(currentScreenData.decisions) do - table.insert(display_decisions, DesitionManager.get(desition_id)) + local desition_obj = DesitionManager.get(desition_id) + if desition_obj and desition_obj.condition() then -- Check condition directly + table.insert(available_desitions, desition_obj) + end end + -- If no available desitions, we can't update or execute + if #available_desitions == 0 then return end + -- Update selected decision using left/right inputs local new_selected_desition_index = UI.update_desition_selector( - display_decisions, + available_desitions, Context.selected_desition_index ) @@ -55,10 +67,10 @@ function GameWindow.update() -- Execute selected decision on Input.select() if Input.select() then - local selected_desition = display_decisions[Context.selected_desition_index] - if selected_desition and selected_desition.handler then + local selected_desition = available_desitions[Context.selected_desition_index] + if selected_desition and selected_desition.handle then -- Call handle directly Audio.sfx_select() -- Play sound for selection - selected_desition.handler() + selected_desition.handle() end end end @@ -67,4 +79,4 @@ end function GameWindow.set_state(new_state) Context.active_window = new_state -- Add any state-specific initialization/cleanup here later if needed -end \ No newline at end of file +end