feature/refactor-npc-handling-to-desition-handling #3
17
impostor.inc
17
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
|
||||
|
||||
14
inc/desition/desition.go_to_home.lua
Normal file
14
inc/desition/desition.go_to_home.lua
Normal 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
|
||||
})
|
||||
14
inc/desition/desition.go_to_office.lua
Normal file
14
inc/desition/desition.go_to_office.lua
Normal 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
|
||||
})
|
||||
14
inc/desition/desition.go_to_toilet.lua
Normal file
14
inc/desition/desition.go_to_toilet.lua
Normal 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
|
||||
})
|
||||
14
inc/desition/desition.go_to_walking_to_home.lua
Normal file
14
inc/desition/desition.go_to_walking_to_home.lua
Normal 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
|
||||
})
|
||||
14
inc/desition/desition.go_to_walking_to_office.lua
Normal file
14
inc/desition/desition.go_to_walking_to_office.lua
Normal 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
|
||||
})
|
||||
42
inc/desition/desition.manager.lua
Normal file
42
inc/desition/desition.manager.lua
Normal 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
|
||||
6
inc/desition/desition.play_button_mash.lua
Normal file
6
inc/desition/desition.play_button_mash.lua
Normal 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
|
||||
})
|
||||
6
inc/desition/desition.play_ddr.lua
Normal file
6
inc/desition/desition.play_ddr.lua
Normal 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
|
||||
})
|
||||
6
inc/desition/desition.play_rhythm.lua
Normal file
6
inc/desition/desition.play_rhythm.lua
Normal 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
|
||||
})
|
||||
@@ -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
|
||||
|
||||
@@ -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 = {}
|
||||
|
||||
8
inc/screen/screen.home.lua
Normal file
8
inc/screen/screen.home.lua
Normal file
@@ -0,0 +1,8 @@
|
||||
ScreenManager.register({
|
||||
id = "home",
|
||||
name = "Home",
|
||||
decisions = {
|
||||
"go_to_toilet",
|
||||
"go_to_walking_to_office",
|
||||
}
|
||||
})
|
||||
27
inc/screen/screen.manager.lua
Normal file
27
inc/screen/screen.manager.lua
Normal 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
|
||||
10
inc/screen/screen.office.lua
Normal file
10
inc/screen/screen.office.lua
Normal file
@@ -0,0 +1,10 @@
|
||||
ScreenManager.register({
|
||||
id = "office",
|
||||
name = "Office",
|
||||
decisions = {
|
||||
"play_button_mash",
|
||||
"play_rhythm",
|
||||
"play_ddr",
|
||||
"go_to_walking_to_home",
|
||||
}
|
||||
})
|
||||
7
inc/screen/screen.toilet.lua
Normal file
7
inc/screen/screen.toilet.lua
Normal file
@@ -0,0 +1,7 @@
|
||||
ScreenManager.register({
|
||||
id = "toilet",
|
||||
name = "Toilet",
|
||||
decisions = {
|
||||
"go_to_home",
|
||||
}
|
||||
})
|
||||
8
inc/screen/screen.walking_to_home.lua
Normal file
8
inc/screen/screen.walking_to_home.lua
Normal file
@@ -0,0 +1,8 @@
|
||||
ScreenManager.register({
|
||||
id = "walking_to_home",
|
||||
name = "Walking to home",
|
||||
decisions = {
|
||||
"go_to_home",
|
||||
"go_to_office",
|
||||
}
|
||||
})
|
||||
8
inc/screen/screen.walking_to_office.lua
Normal file
8
inc/screen/screen.walking_to_office.lua
Normal file
@@ -0,0 +1,8 @@
|
||||
ScreenManager.register({
|
||||
id = "walking_to_office",
|
||||
name = "Walking to office",
|
||||
decisions = {
|
||||
"go_to_home",
|
||||
"go_to_office",
|
||||
}
|
||||
})
|
||||
@@ -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)
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user