diff --git a/love/mike/.gitignore b/love/mike/.gitignore new file mode 100644 index 0000000..922da22 --- /dev/null +++ b/love/mike/.gitignore @@ -0,0 +1 @@ +save_game/* diff --git a/love/mike/lib/GameState.lua b/love/mike/lib/GameState.lua new file mode 100644 index 0000000..bf0670b --- /dev/null +++ b/love/mike/lib/GameState.lua @@ -0,0 +1,54 @@ +GameState = {} + +-- Supply the table then the name of the table in string format +function GameState.save(tbl, table_name) + file = io.open("save_game/savefile.lua", "w") + io.output(file) + print_table(tbl, table_name) + io.close(file) +end + + +function GameState.load() + dofile("save_game/savefile.lua") +end + +function printf (s, ...) + return io.write(s:format(...)) +end + +function print_table(tbl, table_name, only_once) + if only_once == nil then + printf("%s = { ", table_name) + end + for key,value in pairs(tbl) do + if type(value) == "table" then + printf("%s = { ",key) + print_table(value, table_name, true) + printf("}, ") + end + if type (value) ~= "table" then + if type(value) == "string" then + printf("%s = \"%s\", ", key, value) + else + printf("%s = %d, ", key, value) + end + end + end + if only_once == nil then + printf("}") + end +end + +--Counts contents of tables of tables as well +function tablelength(tbl, count) + local count = count or 0 + + for key, value in pairs(tbl) do + if type(value) == "table" then + count = tablelength(value, count) + end + count = count + 1 + end + return count +end diff --git a/love/mike/lib/keys.lua b/love/mike/lib/keys.lua index 7292787..56f741f 100644 --- a/love/mike/lib/keys.lua +++ b/love/mike/lib/keys.lua @@ -1,4 +1,4 @@ -require "lib/Tserial" +require "lib/GameState" QUIT_KEY = "q" SAVE_KEY = "s" @@ -13,15 +13,17 @@ function love.keyreleased(key) -- save elseif key == SAVE_KEY then - love.filesystem.write(saveFile, Tserial.pack(player, false, true)) + --love.filesystem.write(saveFile, Tserial.pack(player, false, true)) + GameState.save(player, "player") -- Load elseif key == LOAD_KEY then - save_exists = love.filesystem.exists(saveFile) + save_exists = love.filesystem.exists("save_game/savefile.lua") if save_exists then - player = Tserial.unpack( love.filesystem.read( saveFile ) ) + --player = Tserial.unpack( love.filesystem.read( saveFile ) ) + GameState.load() end end end diff --git a/love/mike/lib/testing.lua b/love/mike/lib/testing.lua deleted file mode 100644 index 17d1d95..0000000 --- a/love/mike/lib/testing.lua +++ /dev/null @@ -1,51 +0,0 @@ -require "io" - -t = {} -t.jerk = "dick" -t.saint = "Brit" -t.num = 92 -t.multi = {} -t.multi.cat = "mu mu" -t.multi.dog = "iz" -t.multi.number = 53 - -function printf (s, ...) - return io.write(s:format(...)) -end - -function print_table(tbl, only_once) - if only_once == nil then - printf("{") - end - for key,value in pairs(tbl) do - if type(value) == "table" then - printf("%s = {\n",key) - print_table(value, true) - printf("}\n") - end - if type (value) ~= "table" then - if type(value) == "string" then - printf("%s = \"%s\"\n", key, value) - else - printf("%s = %d\n", key, value) - end - end - end - if only_once == nil then - printf("}\n") - end -end - -function tablelength(tbl, count) - local count = count or 0 - - for key, value in pairs(tbl) do - if type(value) == "table" then - count = tablelength(value, count) - end - count = count + 1 - end - return count -end - -print_table(t) diff --git a/love/mike/save_game/savefile.lua b/love/mike/save_game/savefile.lua new file mode 100644 index 0000000..8f4ff2b --- /dev/null +++ b/love/mike/save_game/savefile.lua @@ -0,0 +1 @@ +player = { x = 266, y = 550, width = 30, height = 5, speed = 500, } \ No newline at end of file