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
5 changed files with 100 additions and 10 deletions
Showing only changes of commit 99b178262f - Show all commits

View File

@@ -43,16 +43,38 @@ local function get_initial_data()
}, },
menu_items = {}, menu_items = {},
selected_menu_item = 1, selected_menu_item = 1,
selected_desition_index = 1, -- New desition index
game_in_progress = false, -- New flag game_in_progress = false, -- New flag
screens = clone_table({ screens = clone_table({
{ {
name = "Screen 1", name = "Screen 1",
decisions = {
Util.create_decision(
"play_button_mash",
"Play Button Mash",
function() Desitions.start_minigame_mash() end
),
}
}, },
{ {
name = "Screen 2", name = "Screen 2",
decisions = {
Util.create_decision(
"play_rhythm",
"Play Rhythm Game",
function() Desitions.start_minigame_rhythm() end
),
}
}, },
{ {
name = "Screen 3", name = "Screen 3",
decisions = {
Util.create_decision(
"play_ddr",
"Play DDR (Random)",
function() Desitions.start_minigame_ddr(nil) end
),
}
} }
}) })
} }

View File

@@ -11,9 +11,9 @@ local MinigameDDRWindow = {}
local Util = {} local Util = {}
local Context = {} local Context = {}
local Desitions = {}
local UI = {} local UI = {}
local Print = {} local Print = {}
local Input = {} local Input = {}
local Player = {} local Player = {}
local Audio = {} local Audio = {}
local Desitions = {}

View File

@@ -1,4 +1,3 @@
function Desitions.start_minigame_mash() function Desitions.start_minigame_mash()
MinigameButtonMashWindow.start(WINDOW_GAME) MinigameButtonMashWindow.start(WINDOW_GAME)
end end

View File

@@ -80,10 +80,43 @@ function UI.create_numeric_stepper(label, value_getter, value_setter, min, max,
} }
end end
function UI.create_desition_item(label, desition) function UI.create_action_item(label, action)
return { return {
label = label, label = label,
desition = desition, action = action,
type = "desition_item" type = "action_item"
} }
end end
function UI.draw_desition_selector(desitions, selected_desition_index)
local bar_height = 16
local bar_y = Config.screen.height - bar_height
rect(0, bar_y, Config.screen.width, bar_height, Config.colors.dark_grey)
if #desitions > 0 then
local selected_desition = desitions[selected_desition_index]
local desition_label = selected_desition.label
local text_width = #desition_label * 4 -- Assuming 4 pixels per char
local text_x = (Config.screen.width - text_width) / 2
local text_y = bar_y + 4
-- Draw left arrow
Print.text("<", text_x - 10, text_y, Config.colors.green)
-- Draw selected desition label
Print.text(desition_label, text_x, text_y, Config.colors.item) -- Highlight color
-- Draw right arrow
Print.text(">", text_x + text_width + 6, text_y, Config.colors.green)
end
end
function UI.update_desition_selector(desitions, selected_desition_index)
if Input.left() then
Audio.sfx_beep()
selected_desition_index = Util.safeindex(desitions, selected_desition_index - 1)
elseif Input.right() then
Audio.sfx_beep()
selected_desition_index = Util.safeindex(desitions, selected_desition_index + 1)
end
return selected_desition_index
end

View File

@@ -2,6 +2,10 @@ function GameWindow.draw()
local currentScreenData = Context.screens[Context.current_screen] local currentScreenData = Context.screens[Context.current_screen]
UI.draw_top_bar(currentScreenData.name) UI.draw_top_bar(currentScreenData.name)
if currentScreenData and currentScreenData.decisions and #currentScreenData.decisions > 0 then
UI.draw_desition_selector(currentScreenData.decisions, Context.selected_desition_index)
end
end end
function GameWindow.update() function GameWindow.update()
@@ -10,11 +14,43 @@ function GameWindow.update()
MenuWindow.refresh_menu_items() MenuWindow.refresh_menu_items()
return return
end end
if Input.select() then
if Context.current_screen == #Context.screens then -- Handle screen changing using up/down
Context.current_screen = 1 if Input.up() then
else Context.current_screen = Context.current_screen - 1
if Context.current_screen < 1 then
Context.current_screen = #Context.screens
end
Context.selected_desition_index = 1 -- Reset selected decision on screen change
elseif Input.down() then
Context.current_screen = Context.current_screen + 1 Context.current_screen = Context.current_screen + 1
if Context.current_screen > #Context.screens then
Context.current_screen = 1
end
Context.selected_desition_index = 1 -- Reset selected decision on screen change
end
local currentScreenData = Context.screens[Context.current_screen]
if currentScreenData and currentScreenData.decisions and #currentScreenData.decisions > 0 then
-- Update selected decision using left/right inputs
local new_selected_desition_index = UI.update_desition_selector(
currentScreenData.decisions,
Context.selected_desition_index
)
-- Only update Context if the selection actually changed to avoid unnecessary re-assignment
if new_selected_desition_index ~= Context.selected_desition_index then
Context.selected_desition_index = new_selected_desition_index
end
-- Execute selected decision on Input.select()
if Input.select() then
local selected_desition = currentScreenData.decisions[Context.selected_desition_index]
if selected_desition and selected_desition.handler then
Audio.sfx_select() -- Play sound for selection
selected_desition.handler()
end
end end
end end
end end