56 lines
1.3 KiB
Lua
56 lines
1.3 KiB
Lua
local lg = assert( love.graphics )
|
|
local scene = assert( require 'client.scene' )
|
|
local shared = assert( require 'shared' )
|
|
local server = assert( require 'client.udp' )
|
|
local packet = shared.packet
|
|
local crepuscular = assert( require 'client.crepuscular' )
|
|
|
|
local game = {}
|
|
local t = 0
|
|
local tick = 0
|
|
local serverTick = "0"
|
|
|
|
|
|
function game.draw()
|
|
lg.print( tick, 0, 0 )
|
|
lg.print( serverTick, 0, 25 )
|
|
end
|
|
|
|
function game.onPacket( msg )
|
|
if not msg then return end
|
|
local msgs, types = packet.deserialise( msg )
|
|
if not msgs then return game.onPacket( server.receive() ) end
|
|
for i = 1, #msgs do
|
|
game[ types[i] ]( msgs[i], ip, port )
|
|
end
|
|
return game.onPacket( server.receive() )
|
|
end
|
|
|
|
function game.update( dt )
|
|
t = dt + t
|
|
game.onPacket( server.receive() )
|
|
if t > 0.1 then
|
|
t = 0
|
|
tick = tick + 1
|
|
assert( server.send( packet.get( packet.heartbeat{ tick = tick, hash = 1234 } ) ) )
|
|
end
|
|
end
|
|
|
|
function game.newGame( )
|
|
game.curWorld = shared.NewWorld() --Last world state received from server.
|
|
game.preWorld = shared.NewWorld() --Current world state predicted by this client.
|
|
end
|
|
|
|
function game.disconnect( )
|
|
return scene.mainmenu( server.disconnect() )
|
|
end
|
|
|
|
function game.onLoad( params )
|
|
end
|
|
|
|
function game.keypressed( key, code, isRepeat )
|
|
if code == "escape" then return game.disconnect() end
|
|
end
|
|
|
|
scene.game = game
|
|
return game |