4
0

Compare commits

...

3 Commits

Author SHA1 Message Date
d3fb12703c v0.13
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2025-12-11 20:40:02 +01:00
f9c539a854 resume button 2025-12-11 20:39:26 +01:00
a777241d7a context init fix 2025-12-11 20:36:11 +01:00
3 changed files with 26 additions and 15 deletions

View File

@@ -24,7 +24,8 @@ local function clone_table(t)
return copy
end
local function create_initial_context()
-- This function returns a table containing only the initial *data* for Context
local function get_initial_data()
return {
active_window = WINDOW_SPLASH,
inventory = {},
@@ -63,7 +64,6 @@ local function create_initial_context()
selected_menu_item = 1,
selected_inventory_item = 1,
game_in_progress = false, -- New flag
-- Screen data (deep copy to ensure new game resets correctly)
screens = clone_table({
{
-- Screen 1
@@ -460,22 +460,28 @@ local function create_initial_context()
}
end
Context = create_initial_context()
Context = {}
local function reset_context_to_initial_state()
local initial_state = create_initial_context()
local initial_data = get_initial_data()
-- Clear existing keys from Context
-- Clear existing data properties from Context (but not methods)
for k in pairs(Context) do
if type(Context[k]) ~= "function" then -- Only clear data, leave functions
Context[k] = nil
end
end
-- Copy all properties from initial_state to Context
for k, v in pairs(initial_state) do
-- Copy all initial data properties into Context
for k, v in pairs(initial_data) do
Context[k] = v
end
end
-- Initially populate Context with data
reset_context_to_initial_state()
-- Now define the methods for Context
function Context.new_game()
reset_context_to_initial_state()
Context.game_in_progress = true
@@ -501,7 +507,7 @@ function Context.load_game()
return
end
reset_context_to_initial_state()
reset_context_to_initial_state() -- Reset data, preserve methods
Context.player.x = mget(SAVE_GAME_PLAYER_X_ADDRESS, SAVE_GAME_BANK) / 10
Context.player.y = mget(SAVE_GAME_PLAYER_Y_ADDRESS, SAVE_GAME_BANK) / 10

View File

@@ -3,5 +3,5 @@
-- desc: Life of a programmer in the Vector
-- site: https://github.com/rastasi/mranderson
-- license: MIT License
-- version: 0.12
-- version: 0.13
-- script: lua

View File

@@ -28,6 +28,10 @@ function MenuWindow.save_game()
Context.save_game() -- This function will be created in Context
end
function MenuWindow.resume_game()
GameWindow.set_state(WINDOW_GAME)
end
function MenuWindow.exit()
exit()
end
@@ -38,17 +42,18 @@ function MenuWindow.configuration()
end
function MenuWindow.refresh_menu_items()
Context.menu_items = {
{label = "New Game", action = MenuWindow.new_game},
{label = "Load Game", action = MenuWindow.load_game},
}
Context.menu_items = {} -- Start with an empty table
if Context.game_in_progress then
table.insert(Context.menu_items, {label = "Resume Game", action = MenuWindow.resume_game})
table.insert(Context.menu_items, {label = "Save Game", action = MenuWindow.save_game})
end
table.insert(Context.menu_items, {label = "New Game", action = MenuWindow.new_game})
table.insert(Context.menu_items, {label = "Load Game", action = MenuWindow.load_game})
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