mike: implemented my own saving; added .gitignore for savefiles

This commit is contained in:
Logen Kain 2017-07-25 21:25:45 -07:00
parent 48611008dc
commit e6c1ecb364
5 changed files with 62 additions and 55 deletions

1
love/mike/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
save_game/*

View File

@ -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

View File

@ -1,4 +1,4 @@
require "lib/Tserial" require "lib/GameState"
QUIT_KEY = "q" QUIT_KEY = "q"
SAVE_KEY = "s" SAVE_KEY = "s"
@ -13,15 +13,17 @@ function love.keyreleased(key)
-- save -- save
elseif key == SAVE_KEY then 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 -- Load
elseif key == LOAD_KEY then elseif key == LOAD_KEY then
save_exists = love.filesystem.exists(saveFile) save_exists = love.filesystem.exists("save_game/savefile.lua")
if save_exists then if save_exists then
player = Tserial.unpack( love.filesystem.read( saveFile ) ) --player = Tserial.unpack( love.filesystem.read( saveFile ) )
GameState.load()
end end
end end
end end

View File

@ -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)

View File

@ -0,0 +1 @@
player = { x = 266, y = 550, width = 30, height = 5, speed = 500, }