Added love folder and tommy

This commit is contained in:
Logen Kain 2016-11-25 03:52:53 -07:00
parent 94df037339
commit 249581fd9c
3 changed files with 128 additions and 0 deletions

7
love/tommy/Readme.txt Normal file
View File

@ -0,0 +1,7 @@
arrow keys to move
'q' to quit
's' to save
'l' to load

61
love/tommy/Tserial.lua Normal file
View File

@ -0,0 +1,61 @@
--- Tserial v1.5, a simple table serializer which turns tables into Lua script
-- @author Taehl (SelfMadeSpirit@gmail.com)
Tserial = {}
TSerial = Tserial -- for backwards-compatibility
--- 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
-- if drop is a table, it will be used as a serialization table (where {[value] = serial})
-- @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, empty, indent = "{"..(indent and "\n" or ""), true, indent and math.max(type(indent)=="number" and indent or 0,0)
local function proc(k,v, omitKey) -- encode a key/value pair
empty = nil -- helps ensure empty tables return as "{}"
local tk, tv, skip = type(k), type(v)
if type(drop)=="table" and drop[k] then k = "["..drop[k].."]"
elseif tk == "boolean" then k = k and "[true]" or "[false]"
elseif tk == "string" then local f = string.format("%q",k) if f ~= '"'..k..'"' then k = '['..f..']' 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 type(drop)=="table" and drop[v] then v = drop[v]
elseif 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 return string.rep("\t",indent or 0)..(omitKey and "" or k.."=")..v..","..(indent and "\n" or "") end
return ""
end
local l=-1 repeat l=l+1 until rawget(t,l+1)==nil -- #t "can" lie!
for i=1,l do s = s..proc(i, t[i], true) end -- use ordered values when possible for better string
for k, v in pairs(t) do if not (type(k)=="number" and k<=l) then s = s..proc(k, v) end end
if not empty then s = string.sub(s,1,string.len(s)-1) end
if indent then s = string.sub(s,1,string.len(s)-1).."\n" 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'}"
-- @param safe if true, all extraneous parts of the string will be removed, leaving only a table (prevents running anomalous code when unpacking untrusted strings). Will also cause malformed tables to quietly return nil and an error message, instead of throwing an error (so your program can't be crashed with a bad string)
-- @return a table recreated from the given string.
function Tserial.unpack(s, safe)
if safe then s = string.match(s, "(%b{})") end
assert(type(s) == "string", "Can only Tserial.unpack strings.")
local f, result = loadstring("Tserial.table="..s)
if not safe then assert(f,result) elseif not f then return nil, result end
result = f()
local t = Tserial.table
Tserial.table = nil
return t, result
end

60
love/tommy/main.lua Normal file
View File

@ -0,0 +1,60 @@
require "Tserial"
function love.load()
love.filesystem.setIdentity("tommy")
saveFile = "test.lua"
-- Check if Save file exists
save_exists = love.filesystem.exists(saveFile)
player = {}
player.x = 100
player.y = 100
player.w = 25
player.h = 25
player.speed = 100
end
function love.keyreleased(key)
if key == "q" then
love.event.quit()
end
-- save
if key == "s" then
love.filesystem.write(saveFile, Tserial.pack(player, false, true))
end
-- Load
if key == "l" then
if save_exists then
player = Tserial.unpack( love.filesystem.read( saveFile ) )
end
end
end
function love.update(dt)
--Move character left or right
if love.keyboard.isDown("right") then
player.x = player.x + player.speed*dt
elseif love.keyboard.isDown("left") then
player.x = player.x - player.speed*dt
end
--Also able to move or up down at the same time as one of the above
if love.keyboard.isDown("up") then
player.y = player.y - player.speed*dt
elseif love.keyboard.isDown ("down") then
player.y = player.y + player.speed*dt
end
end
function love.draw()
--draw the rectangle. Fill/line for the type,
--(x,y, width, height)
love.graphics.rectangle("line", player.x, player.y, player.w, player.h)
end