2023-09-04 18:56:14 +00:00
|
|
|
local lg = assert( love.graphics )
|
|
|
|
local scene = assert( require 'client.scene' )
|
|
|
|
local socket = assert( require 'socket' )
|
|
|
|
local shared = assert( require 'shared' )
|
|
|
|
local udp = socket.udp()
|
|
|
|
|
|
|
|
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-05 00:40:07 +00:00
|
|
|
function game.onPacket( data, msg )
|
2023-09-07 01:03:22 +00:00
|
|
|
if data then serverTick = data end
|
2023-09-04 18:56:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function game.update( dt )
|
|
|
|
t = dt + t
|
2023-09-05 00:40:07 +00:00
|
|
|
game.onPacket( udp:receive() )
|
2023-09-04 18:56:14 +00:00
|
|
|
if t > 0.1 then
|
|
|
|
t = 0
|
|
|
|
tick = tick + 1
|
2023-09-05 00:40:07 +00:00
|
|
|
udp:send( tostring(tick) )
|
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( )
|
|
|
|
return scene.mainmenu()
|
|
|
|
end
|
|
|
|
|
2023-09-07 01:03:22 +00:00
|
|
|
function game.onLoad( params )
|
|
|
|
params = params or {}
|
|
|
|
local serverIP, serverPort = params.serverIP or "192.168.2.15", params.serverPort or 51312
|
2023-09-04 18:56:14 +00:00
|
|
|
udp:settimeout( 0 )
|
2023-09-07 01:03:22 +00:00
|
|
|
udp:setpeername( serverIP, serverPort )
|
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
|