your-own-drum/recorder.lua

61 lines
1.3 KiB
Lua
Raw Normal View History

2023-01-15 13:39:58 +00:00
--Record demos.
local love = love
local recorder = {}
recorder.isLoaded = false
local i = 0
2023-01-18 22:51:03 +00:00
function recorder.Reset()
2023-01-15 13:39:58 +00:00
i = 0
2023-01-18 22:51:03 +00:00
for k, _ in ipairs( recorder ) do recorder[k] = nil end
2023-01-15 13:39:58 +00:00
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
2023-01-18 22:51:03 +00:00
recorder[i] = string.char( byte + 48 )
2023-01-15 13:39:58 +00:00
end
2023-01-20 22:38:06 +00:00
function recorder.Load( filename )
local s = love.filesystem.read( filename )
print( filename )
2023-01-18 22:51:03 +00:00
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
2023-01-15 13:39:58 +00:00
recorder.isLoaded = true
2023-01-18 22:51:03 +00:00
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
2023-01-15 13:39:58 +00:00
end
function recorder.Save( )
2023-01-20 22:38:06 +00:00
return assert( love.filesystem.write( "demos/"..os.time()..".yod" , table.concat( recorder ) ) )
2023-01-15 13:39:58 +00:00
end
return recorder