Preparing to add demos.
This commit is contained in:
parent
9754a82694
commit
34f76c0b7c
29
main.lua
29
main.lua
|
@ -20,6 +20,7 @@ local state
|
|||
state = {
|
||||
|
||||
Reset = function()
|
||||
state.tick = 0
|
||||
state.beat = {}
|
||||
state.startTime = love.timer.getTime()
|
||||
state.currentBeat = 1
|
||||
|
@ -92,7 +93,7 @@ OnImpact = function( impact )
|
|||
end
|
||||
local _OnImpact = OnImpact
|
||||
|
||||
local function NewGame()
|
||||
local function NewGame( isDemo )
|
||||
love.graphics.setBackgroundColor( 245 / 255, 169 / 255, 184 / 255 ) --Trans pink.
|
||||
OnImpact = _OnImpact
|
||||
ExtrapolateBeatScore = _ExtrapolateBeatScore
|
||||
|
@ -105,6 +106,12 @@ local function NewGame()
|
|||
wave.Reset()
|
||||
text.Reset()
|
||||
audio.Reset()
|
||||
recorder.Reset()
|
||||
|
||||
state.isDemo = isDemo
|
||||
if isDemo then
|
||||
recorder.Load()
|
||||
end
|
||||
end
|
||||
|
||||
function love.load()
|
||||
|
@ -203,6 +210,8 @@ end
|
|||
|
||||
OnVictory = function()
|
||||
|
||||
recorder.Save()
|
||||
|
||||
particles:setParticleLifetime( 0, 30 )
|
||||
particles:setSizes( 0.001, 0.0001, 0.0005 )
|
||||
particles:setColors(
|
||||
|
@ -296,7 +305,16 @@ Update = function( dt )
|
|||
|
||||
--Physics tick.
|
||||
while dt > step do
|
||||
recorder.Update( marble.GetAcceleration() ) --For savegames.
|
||||
|
||||
state.tick = state.tick + 1
|
||||
|
||||
if state.isDemo then
|
||||
local ddx, ddy = recorder.NextTick()
|
||||
if ddx then marble.SetAcceleration( ddx, ddy )
|
||||
else state.isDemo = false end
|
||||
else
|
||||
recorder.Update( marble.GetAcceleration() ) --For savegames.
|
||||
end
|
||||
|
||||
marble.Integrate( step )
|
||||
wave.Integrate( step )
|
||||
|
@ -318,11 +336,14 @@ _Update = Update
|
|||
function love.keypressed( key, code, isRepeat )
|
||||
if key == "escape" then return love.event.quit() end
|
||||
if key == "space" then return NewGame() end
|
||||
return marble.OnKey()
|
||||
if code == "return" then return NewGame( true ) end --Play demo.
|
||||
if code == "q" then recorder.Save() end
|
||||
if state.isDemo then return end
|
||||
return marble.OnKey( state.tick )
|
||||
end
|
||||
|
||||
function love.keyreleased( key, code )
|
||||
return marble.OnKey()
|
||||
return marble.OnKey( state.tick )
|
||||
end
|
||||
|
||||
function love.resize( w, h )
|
||||
|
|
|
@ -19,6 +19,8 @@ function marble.Next() return newState end
|
|||
|
||||
function marble.GetAcceleration( ) return ddx, ddy end
|
||||
|
||||
function marble.SetAcceleration( x, y ) ddx = x; ddy = y end
|
||||
|
||||
function marble.Integrate( step )
|
||||
newState.t = love.timer.getTime()
|
||||
newState.dx = (1.0 - INERTIA) * curState.dx + INERTIA * ddx
|
||||
|
@ -86,10 +88,11 @@ end
|
|||
|
||||
|
||||
|
||||
function marble.OnKey()
|
||||
function marble.OnKey( tick )
|
||||
ddx = (love.keyboard.isScancodeDown( "d" ) and 1.0 or 0.0) - (love.keyboard.isScancodeDown( "a" ) and 1.0 or 0.0)
|
||||
ddy = (love.keyboard.isScancodeDown( "w" ) and 1.0 or 0.0) - (love.keyboard.isScancodeDown( "s" ) and 1.0 or 0.0)
|
||||
|
||||
--print( tick, ddx, ddy )
|
||||
local n = math.sqrt( ddx * ddx + ddy * ddy )
|
||||
if n < 0.001 then return end
|
||||
ddx, ddy = ddx / n, ddy / n
|
||||
|
@ -177,6 +180,7 @@ function marble.Reset()
|
|||
INERTIA = 0.05
|
||||
MAXSPEED = 2.0
|
||||
|
||||
ddx, ddy = 0, 0
|
||||
oldState, curState, newState = State(), State(), State()
|
||||
marble.Resize()
|
||||
marble.Draw = marble._Draw
|
||||
|
|
36
recorder.lua
36
recorder.lua
|
@ -4,8 +4,9 @@ local recorder = {}
|
|||
recorder.isLoaded = false
|
||||
local i = 0
|
||||
|
||||
local function Reset()
|
||||
function recorder.Reset()
|
||||
i = 0
|
||||
for k, _ in ipairs( recorder ) do recorder[k] = nil end
|
||||
end
|
||||
|
||||
function recorder.Update( ddx, ddy )
|
||||
|
@ -16,17 +17,44 @@ function recorder.Update( ddx, ddy )
|
|||
if ddy < -0.5 then byte = byte + 8 end
|
||||
|
||||
i = i + 1
|
||||
recorder[i] = string.char( byte )
|
||||
recorder[i] = string.char( byte + 48 )
|
||||
end
|
||||
|
||||
function recorder.Load( )
|
||||
local s = love.filesystem.read( "demo" )
|
||||
|
||||
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( )
|
||||
assert( love.filesystem.write( "demo", table.concat( recorder ) ) )
|
||||
return assert( love.filesystem.write( "demo", table.concat( recorder, "", 1, i ) ) )
|
||||
end
|
||||
|
||||
return recorder
|
Loading…
Reference in New Issue