2023-09-04 18:56:14 +00:00
|
|
|
local lg = assert( love.graphics )
|
|
|
|
local scene = assert( require 'client.scene' )
|
|
|
|
local shared = assert( require 'shared' )
|
2023-09-11 22:44:36 +00:00
|
|
|
local server = assert( require 'client.udp' )
|
2023-09-15 02:21:34 +00:00
|
|
|
local packet = shared.packet
|
2023-09-11 22:44:36 +00:00
|
|
|
local crepuscular = assert( require 'client.crepuscular' )
|
2023-09-04 18:56:14 +00:00
|
|
|
|
|
|
|
local game = {}
|
|
|
|
local t = 0
|
|
|
|
local tick = 0
|
2023-09-07 01:03:22 +00:00
|
|
|
local serverTick = "0"
|
2023-09-04 18:56:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
function game.draw()
|
2023-09-07 01:03:22 +00:00
|
|
|
lg.print( tick, 0, 0 )
|
|
|
|
lg.print( serverTick, 0, 25 )
|
2023-09-05 00:40:07 +00:00
|
|
|
end
|
2023-09-04 18:56:14 +00:00
|
|
|
|
2023-09-15 02:21:34 +00:00
|
|
|
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() )
|
2023-09-04 18:56:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function game.update( dt )
|
|
|
|
t = dt + t
|
2023-09-11 22:44:36 +00:00
|
|
|
game.onPacket( server.receive() )
|
2023-09-04 18:56:14 +00:00
|
|
|
if t > 0.1 then
|
|
|
|
t = 0
|
|
|
|
tick = tick + 1
|
2023-09-15 02:21:34 +00:00
|
|
|
assert( server.send( packet.get( packet.heartbeat{ tick = tick, hash = 1234 } ) ) )
|
2023-09-04 18:56:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-05 00:40:07 +00:00
|
|
|
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( )
|
2023-09-11 22:44:36 +00:00
|
|
|
return scene.mainmenu( server.disconnect() )
|
2023-09-05 00:40:07 +00:00
|
|
|
end
|
|
|
|
|
2023-09-07 01:03:22 +00:00
|
|
|
function game.onLoad( params )
|
2023-09-04 18:56:14 +00:00
|
|
|
end
|
|
|
|
|
2023-09-05 00:40:07 +00:00
|
|
|
function game.keypressed( key, code, isRepeat )
|
|
|
|
if code == "escape" then return game.disconnect() end
|
|
|
|
end
|
|
|
|
|
|
|
|
scene.game = game
|
|
|
|
return game
|