Compare commits
3 Commits
34340d9664
...
d3fb12703c
| Author | SHA1 | Date | |
|---|---|---|---|
| d3fb12703c | |||
| f9c539a854 | |||
| a777241d7a |
@@ -24,7 +24,8 @@ local function clone_table(t)
|
|||||||
return copy
|
return copy
|
||||||
end
|
end
|
||||||
|
|
||||||
local function create_initial_context()
|
-- This function returns a table containing only the initial *data* for Context
|
||||||
|
local function get_initial_data()
|
||||||
return {
|
return {
|
||||||
active_window = WINDOW_SPLASH,
|
active_window = WINDOW_SPLASH,
|
||||||
inventory = {},
|
inventory = {},
|
||||||
@@ -63,7 +64,6 @@ local function create_initial_context()
|
|||||||
selected_menu_item = 1,
|
selected_menu_item = 1,
|
||||||
selected_inventory_item = 1,
|
selected_inventory_item = 1,
|
||||||
game_in_progress = false, -- New flag
|
game_in_progress = false, -- New flag
|
||||||
-- Screen data (deep copy to ensure new game resets correctly)
|
|
||||||
screens = clone_table({
|
screens = clone_table({
|
||||||
{
|
{
|
||||||
-- Screen 1
|
-- Screen 1
|
||||||
@@ -460,22 +460,28 @@ local function create_initial_context()
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
Context = create_initial_context()
|
Context = {}
|
||||||
|
|
||||||
local function reset_context_to_initial_state()
|
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
|
for k in pairs(Context) do
|
||||||
Context[k] = nil
|
if type(Context[k]) ~= "function" then -- Only clear data, leave functions
|
||||||
|
Context[k] = nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Copy all properties from initial_state to Context
|
-- Copy all initial data properties into Context
|
||||||
for k, v in pairs(initial_state) do
|
for k, v in pairs(initial_data) do
|
||||||
Context[k] = v
|
Context[k] = v
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Initially populate Context with data
|
||||||
|
reset_context_to_initial_state()
|
||||||
|
|
||||||
|
-- Now define the methods for Context
|
||||||
function Context.new_game()
|
function Context.new_game()
|
||||||
reset_context_to_initial_state()
|
reset_context_to_initial_state()
|
||||||
Context.game_in_progress = true
|
Context.game_in_progress = true
|
||||||
@@ -501,7 +507,7 @@ function Context.load_game()
|
|||||||
return
|
return
|
||||||
end
|
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.x = mget(SAVE_GAME_PLAYER_X_ADDRESS, SAVE_GAME_BANK) / 10
|
||||||
Context.player.y = mget(SAVE_GAME_PLAYER_Y_ADDRESS, SAVE_GAME_BANK) / 10
|
Context.player.y = mget(SAVE_GAME_PLAYER_Y_ADDRESS, SAVE_GAME_BANK) / 10
|
||||||
@@ -512,4 +518,4 @@ function Context.load_game()
|
|||||||
|
|
||||||
Context.game_in_progress = true
|
Context.game_in_progress = true
|
||||||
MenuWindow.refresh_menu_items()
|
MenuWindow.refresh_menu_items()
|
||||||
end
|
end
|
||||||
@@ -3,5 +3,5 @@
|
|||||||
-- desc: Life of a programmer in the Vector
|
-- desc: Life of a programmer in the Vector
|
||||||
-- site: https://github.com/rastasi/mranderson
|
-- site: https://github.com/rastasi/mranderson
|
||||||
-- license: MIT License
|
-- license: MIT License
|
||||||
-- version: 0.12
|
-- version: 0.13
|
||||||
-- script: lua
|
-- script: lua
|
||||||
|
|||||||
@@ -28,6 +28,10 @@ function MenuWindow.save_game()
|
|||||||
Context.save_game() -- This function will be created in Context
|
Context.save_game() -- This function will be created in Context
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function MenuWindow.resume_game()
|
||||||
|
GameWindow.set_state(WINDOW_GAME)
|
||||||
|
end
|
||||||
|
|
||||||
function MenuWindow.exit()
|
function MenuWindow.exit()
|
||||||
exit()
|
exit()
|
||||||
end
|
end
|
||||||
@@ -38,17 +42,18 @@ function MenuWindow.configuration()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function MenuWindow.refresh_menu_items()
|
function MenuWindow.refresh_menu_items()
|
||||||
Context.menu_items = {
|
Context.menu_items = {} -- Start with an empty table
|
||||||
{label = "New Game", action = MenuWindow.new_game},
|
|
||||||
{label = "Load Game", action = MenuWindow.load_game},
|
|
||||||
}
|
|
||||||
|
|
||||||
if Context.game_in_progress then
|
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})
|
table.insert(Context.menu_items, {label = "Save Game", action = MenuWindow.save_game})
|
||||||
end
|
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 = "Configuration", action = MenuWindow.configuration})
|
||||||
table.insert(Context.menu_items, {label = "Exit", action = MenuWindow.exit})
|
table.insert(Context.menu_items, {label = "Exit", action = MenuWindow.exit})
|
||||||
|
|
||||||
Context.selected_menu_item = 1 -- Reset selection after refreshing
|
Context.selected_menu_item = 1 -- Reset selection after refreshing
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user