save/load functionality (WIP)

This commit is contained in:
2025-12-11 20:00:43 +01:00
parent 6a39128962
commit 34340d9664
9 changed files with 644 additions and 461 deletions

View File

@@ -17,6 +17,14 @@ function ConfigurationWindow.init()
function(v) Config.physics.max_jumps = v end,
1, 5, 1, "%d"
),
UI.create_action_item(
"Save",
function() Config.save() end
),
UI.create_action_item(
"Restore Defaults",
function() Config.restore_defaults() end
),
}
end
@@ -31,23 +39,35 @@ function ConfigurationWindow.draw()
for i, control in ipairs(ConfigurationWindow.controls) do
local current_y = y_start + (i - 1) * 12
local color = Config.colors.green
local value = control.get()
local label_text = control.label
local value_text = string.format(control.format, value)
-- Calculate x position for right-aligned value
local value_x = x_value_right_align - (#value_text * char_width)
if i == ConfigurationWindow.selected_control then
color = Config.colors.item
print("<", x_start -8, current_y, color)
print(label_text, x_start, current_y, color) -- Shift label due to '<'
print(value_text, value_x, current_y, color)
print(">", x_value_right_align + 4, current_y, color) -- Print '>' after value
else
print(label_text, x_start, current_y, color)
print(value_text, value_x, current_y, color)
if control.type == "numeric_stepper" then
local value = control.get()
local label_text = control.label
local value_text = string.format(control.format, value)
-- Calculate x position for right-aligned value
local value_x = x_value_right_align - (#value_text * char_width)
if i == ConfigurationWindow.selected_control then
color = Config.colors.item
print("<", x_start -8, current_y, color)
print(label_text, x_start, current_y, color) -- Shift label due to '<'
print(value_text, value_x, current_y, color)
print(">", x_value_right_align + 4, current_y, color) -- Print '>' after value
else
print(label_text, x_start, current_y, color)
print(value_text, value_x, current_y, color)
end
elseif control.type == "action_item" then
local label_text = control.label
if i == ConfigurationWindow.selected_control then
color = Config.colors.item
print("<", x_start -8, current_y, color)
print(label_text, x_start, current_y, color)
print(">", x_start + 8 + (#label_text * char_width) + 4, current_y, color)
else
print(label_text, x_start, current_y, color)
end
end
end
@@ -56,13 +76,10 @@ end
function ConfigurationWindow.update()
if Input.menu_back() then
-- I need to find out how to switch back to the menu
-- For now, I'll assume a function GameWindow.set_state exists
GameWindow.set_state(WINDOW_MENU)
return
end
-- Navigate between controls
if Input.up() then
ConfigurationWindow.selected_control = ConfigurationWindow.selected_control - 1
if ConfigurationWindow.selected_control < 1 then
@@ -75,16 +92,21 @@ function ConfigurationWindow.update()
end
end
-- Modify control value
local control = ConfigurationWindow.controls[ConfigurationWindow.selected_control]
if control then
local current_value = control.get()
if Input.left() then
local new_value = math.max(control.min, current_value - control.step)
control.set(new_value)
elseif Input.right() then
local new_value = math.min(control.max, current_value + control.step)
control.set(new_value)
if control.type == "numeric_stepper" then
local current_value = control.get()
if btnp(2) then -- Left
local new_value = math.max(control.min, current_value - control.step)
control.set(new_value)
elseif btnp(3) then -- Right
local new_value = math.min(control.max, current_value + control.step)
control.set(new_value)
end
elseif control.type == "action_item" then
if Input.menu_confirm() then
control.action()
end
end
end
end

View File

@@ -26,6 +26,11 @@ function GameWindow.draw()
end
function GameWindow.update()
if Input.menu_back() then
Context.active_window = WINDOW_MENU
MenuWindow.refresh_menu_items()
return
end
Player.update() -- Call the encapsulated player update logic
end

View File

@@ -14,17 +14,20 @@ function MenuWindow.update()
end
end
function MenuWindow.play()
-- Reset player state and screen for a new game
Context.player.x = Config.player.start_x
Context.player.y = Config.player.start_y
Context.player.vx = 0
Context.player.vy = 0
Context.player.jumps = 0
Context.current_screen = 1
function MenuWindow.new_game()
Context.new_game() -- This function will be created in Context
GameWindow.set_state(WINDOW_GAME)
end
function MenuWindow.load_game()
Context.load_game() -- This function will be created in Context
GameWindow.set_state(WINDOW_GAME)
end
function MenuWindow.save_game()
Context.save_game() -- This function will be created in Context
end
function MenuWindow.exit()
exit()
end
@@ -34,9 +37,19 @@ function MenuWindow.configuration()
GameWindow.set_state(WINDOW_CONFIGURATION)
end
-- Initialize menu items after actions are defined
Context.menu_items = {
{label = "Play", action = MenuWindow.play},
{label = "Configuration", action = MenuWindow.configuration},
{label = "Exit", action = MenuWindow.exit}
}
function MenuWindow.refresh_menu_items()
Context.menu_items = {
{label = "New Game", action = MenuWindow.new_game},
{label = "Load Game", action = MenuWindow.load_game},
}
if Context.game_in_progress then
table.insert(Context.menu_items, {label = "Save Game", action = MenuWindow.save_game})
end
table.insert(Context.menu_items, {label = "Configuration", action = MenuWindow.configuration})
table.insert(Context.menu_items, {label = "Exit", action = MenuWindow.exit})
Context.selected_menu_item = 1 -- Reset selection after refreshing
end