your-own-drum/recorder.lua

61 lines
1.3 KiB
Lua

--Record demos.
local love = love
local recorder = {}
recorder.isLoaded = false
local i = 0
function recorder.Reset()
i = 0
for k, _ in ipairs( recorder ) do recorder[k] = nil end
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 )
local s = love.filesystem.read( filename )
print( filename )
if not s then return end
local j = 1
for c in s:gmatch( "." ) do
recorder[j] = string.byte( c ) - 48
j = j + 1
end
recorder.isLoaded = true
i = 0
return true
end
function recorder.NextTick( )
local byte = recorder[ i + 1 ]
if not byte then return end
i = i + 1
local a, b, c, d =
math.floor( byte ) % 2,
math.floor( byte / 2 ) % 2,
math.floor( byte / 4 ) % 2,
math.floor( byte / 8 ) % 2
local ddx, ddy = a - c, b - d
local n = math.sqrt( ddx * ddx + ddy * ddy )
if n < 0.001 then return 0, 0 end
ddx, ddy = ddx / n, ddy / n
return ddx, ddy
end
function recorder.Save( )
return assert( love.filesystem.write( "demos/"..os.time()..".yod" , table.concat( recorder ) ) )
end
return recorder