your-own-drum/recorder.lua

57 lines
1.3 KiB
Lua

--Record demos.
local love = love
local recorder = {}
recorder.isLoaded = false
local ddxs, ddys
local i = 0
function recorder.Reset()
i = 1
for k, _ in ipairs( recorder ) do recorder[k] = nil end
end
function recorder.Update( ddx, ddy )
i = i + 1
recorder[i] = love.data.pack( "string", "!16<dd", ddx, ddy )
end
--[[function recorder.Update( ddx, ddy )
local byte = 0
if ddx > 0.5 then byte = byte + 1 end
if ddy > 0.5 then byte = byte + 2 end
if ddx < -0.5 then byte = byte + 4 end
if ddy < -0.5 then byte = byte + 8 end
i = i + 1
recorder[i] = string.char( byte + 48 )
end]]
function recorder.Load( filename )
ddxs, ddys = {}, {}
local s = love.filesystem.read( filename )
if not s then return end
local j = 1
local score, ticks, k = love.data.unpack( "!16<dJ", s, 1)
local n = s:len()
while k < n do
ddxs[j], ddys[j], k = love.data.unpack( "!16<dd", s, k)
j = j + 1
end
recorder.isLoaded = true
i = 0
return true
end
function recorder.NextTick( )
i = i + 1
return ddxs[i], ddys[i]
end
function recorder.Save( score, ticks )
recorder[1] = love.data.pack( "string", "!16<dJ", score, ticks )
love.filesystem.createDirectory( "demos" )
return assert( love.filesystem.write( "demos/"..os.time()..".yod" , table.concat( recorder ) ) )
end
return recorder