From 03cfda0be4922c22eeac31c3f4da54b35c2a992c Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Thu, 18 Jan 2018 01:19:50 -0500 Subject: [PATCH] save game --- main.lua | 17 +++++++-- show.lua | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 show.lua diff --git a/main.lua b/main.lua index 93f4a7f..45a9d33 100644 --- a/main.lua +++ b/main.lua @@ -14,13 +14,20 @@ function love.load() sprites.player_stand = love.graphics.newImage('sprites/player_stand.png') require('player') - resetPlayerPosition() - + require('show') require('coin') anim8 = require 'anim8' sti = require 'sti' camera = require 'camera' + saveData = {} + saveData.bestTime = 999 + + if love.filesystem.exists("data.lua") then + local data = love.filesystem.load("data.lua") + data() + end + platforms = {} cam = camera() @@ -30,6 +37,7 @@ function love.load() end spawnCoins() + resetPlayerPosition() DIRECTION_LEFT = -1 DIRECTION_RIGHT = 1 @@ -55,6 +63,10 @@ function love.update(dt) gameState = "menu" resetPlayerPosition() spawnCoins() + if timer < saveData.bestTime then + saveData.bestTime = math.floor(timer) + love.filesystem.write("data.lua", table.show(saveData, "saveData")) + end end end @@ -77,6 +89,7 @@ function love.draw() if gameState == "menu" then love.graphics.setFont(myFont) love.graphics.printf("Press any key to begin!", 0, 50, love.graphics.getWidth(), "center") + love.graphics.printf("Best time:" .. saveData.bestTime, 0, 150, love.graphics.getWidth(), "center") end if gameState == "game" then love.graphics.print("Time:" .. math.ceil(timer), 10, 660) diff --git a/show.lua b/show.lua new file mode 100644 index 0000000..141fa87 --- /dev/null +++ b/show.lua @@ -0,0 +1,105 @@ +--[[ + Author: Julio Manuel Fernandez-Diaz + Date: January 12, 2007 + (For Lua 5.1) + + Modified slightly by RiciLake to avoid the unnecessary table traversal in tablecount() + + Formats tables with cycles recursively to any depth. + The output is returned as a string. + References to other tables are shown as values. + Self references are indicated. + + The string returned is "Lua code", which can be procesed + (in the case in which indent is composed by spaces or "--"). + Userdata and function keys and values are shown as strings, + which logically are exactly not equivalent to the original code. + + This routine can serve for pretty formating tables with + proper indentations, apart from printing them: + + print(table.show(t, "t")) -- a typical use + + Heavily based on "Saving tables with cycles", PIL2, p. 113. + + Arguments: + t is the table. + name is the name of the table (optional) + indent is a first indentation (optional). +--]] +function table.show(t, name, indent) + local cart -- a container + local autoref -- for self references + + --[[ counts the number of elements in a table + local function tablecount(t) + local n = 0 + for _, _ in pairs(t) do n = n+1 end + return n + end + ]] + -- (RiciLake) returns true if the table is empty + local function isemptytable(t) return next(t) == nil end + + local function basicSerialize (o) + local so = tostring(o) + if type(o) == "function" then + local info = debug.getinfo(o, "S") + -- info.name is nil because o is not a calling level + if info.what == "C" then + return string.format("%q", so .. ", C function") + else + -- the information is defined through lines + return string.format("%q", so .. ", defined in (" .. + info.linedefined .. "-" .. info.lastlinedefined .. + ")" .. info.source) + end + elseif type(o) == "number" or type(o) == "boolean" then + return so + else + return string.format("%q", so) + end + end + + local function addtocart (value, name, indent, saved, field) + indent = indent or "" + saved = saved or {} + field = field or name + + cart = cart .. indent .. field + + if type(value) ~= "table" then + cart = cart .. " = " .. basicSerialize(value) .. ";\n" + else + if saved[value] then + cart = cart .. " = {}; -- " .. saved[value] + .. " (self reference)\n" + autoref = autoref .. name .. " = " .. saved[value] .. ";\n" + else + saved[value] = name + --if tablecount(value) == 0 then + if isemptytable(value) then + cart = cart .. " = {};\n" + else + cart = cart .. " = {\n" + for k, v in pairs(value) do + k = basicSerialize(k) + local fname = string.format("%s[%s]", name, k) + field = string.format("[%s]", k) + -- three spaces between levels + addtocart(v, fname, indent .. " ", saved, field) + end + cart = cart .. indent .. "};\n" + end + end + end + end + + name = name or "__unnamed__" + if type(t) ~= "table" then + return name .. " = " .. basicSerialize(t) + end + cart, autoref = "", "" + addtocart(t, name, indent) + return cart .. autoref +end