vision/src/client/game.lua

51 lines
1.1 KiB
Lua
Raw Normal View History

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' )
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-11 22:44:36 +00:00
function game.onPacket( data )
if not data then return end
serverTick = data
return game.onPacket( server.receive() ) --Recurse to get all waiting packets.
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-11 22:44:36 +00:00
assert( server.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( )
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