mike: implemented my own saving; added .gitignore for savefiles
This commit is contained in:
parent
48611008dc
commit
e6c1ecb364
1
love/mike/.gitignore
vendored
Normal file
1
love/mike/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
save_game/*
|
54
love/mike/lib/GameState.lua
Normal file
54
love/mike/lib/GameState.lua
Normal 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
|
@ -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
|
||||
|
@ -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)
|
1
love/mike/save_game/savefile.lua
Normal file
1
love/mike/save_game/savefile.lua
Normal file
@ -0,0 +1 @@
|
||||
player = { x = 266, y = 550, width = 30, height = 5, speed = 500, }
|
Loading…
x
Reference in New Issue
Block a user