feature/refactor-npc-handling-to-desition-handling #3

Merged
mr.zero merged 10 commits from feature/refactor-npc-handling-to-desition-handling into master 2026-02-17 20:58:47 +00:00
22 changed files with 256 additions and 141 deletions
Showing only changes of commit 0357eb415e - Show all commits

View File

@@ -3,11 +3,24 @@ init/init.modules.lua
init/init.config.lua init/init.config.lua
system/system.util.lua system/system.util.lua
init/init.windows.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 init/init.context.lua
data/data.songs.lua data/data.songs.lua
system/system.print.lua system/system.print.lua
system/system.desitions.lua
system/system.desition_manager.lua
system/system.input.lua system/system.input.lua
system/system.audio.lua system/system.audio.lua
system/system.ui.lua system/system.ui.lua

View File

@@ -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
})

View File

@@ -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
})

View File

@@ -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
})

View File

@@ -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
})

View File

@@ -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
})

View File

@@ -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

View File

@@ -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
})

View File

@@ -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
})

View File

@@ -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
})

View File

@@ -45,52 +45,12 @@ local function get_initial_data()
selected_menu_item = 1, selected_menu_item = 1,
selected_desition_index = 1, -- New desition index selected_desition_index = 1, -- New desition index
game_in_progress = false, -- New flag game_in_progress = false, -- New flag
screens = clone_table({ screens = {} -- Initialize as empty, populated on reset
{
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",
}
}
})
} }
end end
Context = {}
local function reset_context_to_initial_state() local function reset_context_to_initial_state()
local initial_data = get_initial_data() 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 for k, v in pairs(initial_data) do
Context[k] = v Context[k] = v
end 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 end
-- Initially populate Context with data -- Initially populate Context with data

View File

@@ -10,9 +10,8 @@ local MinigameRhythmWindow = {}
local MinigameDDRWindow = {} local MinigameDDRWindow = {}
local Util = {} local Util = {}
local Context = {}
local Desitions = {}
local DesitionManager = {} local DesitionManager = {}
local ScreenManager = {} -- New declaration
local UI = {} local UI = {}
local Print = {} local Print = {}
local Input = {} local Input = {}

View File

@@ -0,0 +1,8 @@
ScreenManager.register({
id = "home",
name = "Home",
decisions = {
"go_to_toilet",
"go_to_walking_to_office",
}
})

View File

@@ -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

View File

@@ -0,0 +1,10 @@
ScreenManager.register({
id = "office",
name = "Office",
decisions = {
"play_button_mash",
"play_rhythm",
"play_ddr",
"go_to_walking_to_home",
}
})

View File

@@ -0,0 +1,7 @@
ScreenManager.register({
id = "toilet",
name = "Toilet",
decisions = {
"go_to_home",
}
})

View File

@@ -0,0 +1,8 @@
ScreenManager.register({
id = "walking_to_home",
name = "Walking to home",
decisions = {
"go_to_home",
"go_to_office",
}
})

View File

@@ -0,0 +1,8 @@
ScreenManager.register({
id = "walking_to_office",
name = "Walking to office",
decisions = {
"go_to_home",
"go_to_office",
}
})

View File

@@ -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)

View File

@@ -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

View File

@@ -1,13 +1,5 @@
Util = {} Util = {}
function Util.create_decision(id, label, handler)
return {
id = id,
label = label,
handler = handler
}
end
function Util.safeindex(array, index) function Util.safeindex(array, index)
return ((index - 1 + #array) % #array) + 1 return ((index - 1 + #array) % #array) + 1
end end

View File

@@ -4,11 +4,17 @@ function GameWindow.draw()
UI.draw_top_bar(currentScreenData.name) UI.draw_top_bar(currentScreenData.name)
if currentScreenData and currentScreenData.decisions and #currentScreenData.decisions > 0 then if currentScreenData and currentScreenData.decisions and #currentScreenData.decisions > 0 then
local display_decisions = {} local available_desitions = {}
for _, desition_id in ipairs(currentScreenData.decisions) do 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 end
UI.draw_desition_selector(display_decisions, Context.selected_desition_index)
end end
end end
@@ -37,14 +43,20 @@ function GameWindow.update()
local currentScreenData = Context.screens[Context.current_screen] local currentScreenData = Context.screens[Context.current_screen]
if currentScreenData and currentScreenData.decisions and #currentScreenData.decisions > 0 then if currentScreenData and currentScreenData.decisions and #currentScreenData.decisions > 0 then
local display_decisions = {} local available_desitions = {}
for _, desition_id in ipairs(currentScreenData.decisions) do 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 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 -- Update selected decision using left/right inputs
local new_selected_desition_index = UI.update_desition_selector( local new_selected_desition_index = UI.update_desition_selector(
display_decisions, available_desitions,
Context.selected_desition_index Context.selected_desition_index
) )
@@ -55,10 +67,10 @@ function GameWindow.update()
-- Execute selected decision on Input.select() -- Execute selected decision on Input.select()
if Input.select() then if Input.select() then
local selected_desition = display_decisions[Context.selected_desition_index] local selected_desition = available_desitions[Context.selected_desition_index]
if selected_desition and selected_desition.handler then if selected_desition and selected_desition.handle then -- Call handle directly
Audio.sfx_select() -- Play sound for selection Audio.sfx_select() -- Play sound for selection
selected_desition.handler() selected_desition.handle()
end end
end end
end end
@@ -67,4 +79,4 @@ end
function GameWindow.set_state(new_state) function GameWindow.set_state(new_state)
Context.active_window = new_state Context.active_window = new_state
-- Add any state-specific initialization/cleanup here later if needed -- Add any state-specific initialization/cleanup here later if needed
end end