Skip to content

Commit

Permalink
Added simple config load/save + how to option on main menu
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonLarsen committed Feb 12, 2013
1 parent e333d4e commit e201b3f
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 15 deletions.
46 changes: 46 additions & 0 deletions TSerial.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--- TSerial v1.3, a simple table serializer which turns tables into Lua script
-- @author Taehl (SelfMadeSpirit@gmail.com)
TSerial = {}

--- Serializes a table into a string, in form of Lua script.
-- @param t table to be serialized (may not contain any circular reference)
-- @param drop if true, unserializable types will be silently dropped instead of raising errors
-- if drop is a function, it will be called to serialize unsupported types
-- @param indent if true, output "human readable" mode with newlines and indentation (for debug)
-- @return string recreating given table
function TSerial.pack(t, drop, indent)
assert(type(t) == "table", "Can only TSerial.pack tables.")
local s, indent = "{"..(indent and "\n" or ""), indent and math.max(type(indent)=="number" and indent or 0,0)
for k, v in pairs(t) do
local tk, tv, skip = type(k), type(v)
if tk == "boolean" then k = k and "[true]" or "[false]"
elseif tk == "string" then if string.format("%q",k) ~= '"'..k..'"' then k = '['..string.format("%q",k)..']' end
elseif tk == "number" then k = "["..k.."]"
elseif tk == "table" then k = "["..TSerial.pack(k, drop, indent and indent+1).."]"
elseif type(drop) == "function" then k = "["..string.format("%q",drop(k)).."]"
elseif drop then skip = true
else error("Attempted to TSerial.pack a table with an invalid key: "..tostring(k))
end
if tv == "boolean" then v = v and "true" or "false"
elseif tv == "string" then v = string.format("%q", v)
elseif tv == "number" then -- no change needed
elseif tv == "table" then v = TSerial.pack(v, drop, indent and indent+1)
elseif type(drop) == "function" then v = "["..string.format("%q",drop(v)).."]"
elseif drop then skip = true
else error("Attempted to TSerial.pack a table with an invalid value: "..tostring(v))
end
if not skip then s = s..string.rep("\t",indent or 0)..k.."="..v..","..(indent and "\n" or "") end
end
return s..string.rep("\t",(indent or 1)-1).."}"
end

--- Loads a table into memory from a string (like those output by Tserial.pack)
-- @param s a string of Lua defining a table, such as "{2,4,8,ex="ample"}"
-- @return a table recreated from the given string
function TSerial.unpack(s)
assert(type(s) == "string", "Can only TSerial.unpack strings.")
assert(loadstring("TSerial.table="..s))()
local t = TSerial.table
TSerial.table = nil
return t
end
6 changes: 3 additions & 3 deletions conf.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
function love.conf(t)
t.title = "Firefighter"
t.title = "mrrescue"
t.author = "Simon Larsen"
t.url = nil
t.identity = nil
t.url = "http://simonlarsen.github.com/mrrescue"
t.identity = "mrrescue"
t.version = "0.8.0"
t.console = false
t.release = false
Expand Down
13 changes: 13 additions & 0 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,23 @@ default_config = {
}

function loadConfig()
-- Read default settings first
config = {}
for i,v in pairs(default_config) do
config[i] = v
end
if love.filesystem.exists("settings") then
local data = love.filesystem.read("settings")
local file = TSerial.unpack(data)
for i,v in pairs(file) do
config[i] = v
end
end
end

function saveConfig()
local data = TSerial.pack(config)
love.filesystem.write("settings", data)
end

function setMode()
Expand Down
4 changes: 2 additions & 2 deletions fire.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ function Fire.create(x,y,map)
self.x, self.y = x*16, y*16
self.frame = math.random()*5
self.flframe = 0
self.MIN_SPREAD_WAIT = math.round(6 - map.section*(4/LAST_SECTION))
self.MAX_SPREAD_WAIT = math.round(12 - map.section*(8/LAST_SECTION))
self.MIN_SPREAD_WAIT = math.round(8 - map.section*(6/LAST_SECTION))
self.MAX_SPREAD_WAIT = math.round(14 - map.section*(10/LAST_SECTION))
self.nextSpread = math.random(self.MIN_SPREAD_WAIT, self.MAX_SPREAD_WAIT)
self.bbox = {x=self.x+4, y=self.y+4, w=8, h=8}

Expand Down
5 changes: 3 additions & 2 deletions main.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require("config")
require("resources")
require("util")
require("AnAL")
require("map")
require("player")
require("human")
Expand All @@ -10,7 +9,10 @@ require("door")
require("item")
require("fire")
require("particles")
-- 3rd party libraries
require("AnAL")
require("slam")
require("TSerial")
-- gamestates
require("splash")
require("mainmenu")
Expand All @@ -23,7 +25,6 @@ HEIGHT = 200
MAPW = 41*16
MAPH = 16*16
show_debug = false
--disable_music = true

local MIN_FRAMERATE = 1/15
local MAX_FRAMERATE = 1/120
Expand Down
16 changes: 8 additions & 8 deletions mainmenu.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mainmenu = {}

local MENU_STRINGS = {
"START GAME", "HIGHSCORES", "OPTIONS", "HISTORY", "EXIT"
"START GAME", "HOW TO PLAY", "HIGHSCORES", "OPTIONS", "HISTORY", "EXIT"
}

lg = love.graphics
Expand All @@ -21,29 +21,29 @@ function mainmenu.draw()

lg.drawq(img.splash, quad.splash, 0,0)
lg.setFont(font.bold)
for i=1,5 do
for i=1,6 do
if i == selection then
lg.print(">", 144, 91+i*13)
lg.print(">", 144, 86+i*13)
end
lg.print(MENU_STRINGS[i], 152, 91+i*13)
lg.print(MENU_STRINGS[i], 152, 86+i*13)
end

lg.pop()
end

function mainmenu.keypressed(k, uni)
if k == "down" then
selection = wrap(selection + 1, 1,5)
selection = wrap(selection + 1, 1,6)
playSound("blip")
elseif k == "up" then
selection = wrap(selection - 1, 1,5)
selection = wrap(selection - 1, 1,6)
playSound("blip")
elseif k == "return" or k == " " then
if selection == 1 then
levelselection.enter()
elseif selection == 3 then
elseif selection == 4 then
options.enter()
elseif selection == 5 then
elseif selection == 6 then
love.event.quit()
end
playSound("confirm")
Expand Down
3 changes: 3 additions & 0 deletions options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function options.keypressed(k, uni)
elseif k == "up" then
selection = wrap(selection - 1, 1,7)
playSound("blip")

elseif k == "left" or k == "right" then
if selection == 1 then -- SCALE
if k == "left" then
Expand Down Expand Up @@ -69,12 +70,14 @@ function options.keypressed(k, uni)
love.audio.tags.music.setVolume(config.music_volume)
playSound("blip")
end

elseif k == "return" then
if selection == 2 then -- VSYNC
toggleVSync()
playSound("blip")
elseif selection == 7 then -- BACK
mainmenu.enter()
saveConfig()
playSound("confirm")
end
end
Expand Down

0 comments on commit e201b3f

Please sign in to comment.