90 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Lua
		
	
	
	
			
		
		
	
	
			90 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Lua
		
	
	
	
local scene = assert( require 'scene' )
 | 
						|
local lg = assert( love.graphics )
 | 
						|
local server = assert( require 'udp' )
 | 
						|
local button = assert( require 'ui.button' )
 | 
						|
local strings = assert( require 'strings' )
 | 
						|
local connecting = {}
 | 
						|
 | 
						|
local time, ip, port, attempts, svInfo = 0, 0, 0, 1
 | 
						|
 | 
						|
local cancelButton = button{ 
 | 
						|
  x = lg.getWidth() / 4,
 | 
						|
  y = lg.getHeight() / 2,
 | 
						|
  w = lg.getWidth() / 2,
 | 
						|
  h = 100,
 | 
						|
  text = lg.newText( lg.getFont(), strings.cancel_button ),
 | 
						|
}
 | 
						|
 | 
						|
function connecting.mousemoved( x, y )
 | 
						|
  cancelButton.selected = cancelButton:contains( x, y )
 | 
						|
end
 | 
						|
 | 
						|
function connecting.mousepressed( x, y )
 | 
						|
  if cancelButton:contains( x, y ) then return scene.browser() end
 | 
						|
end
 | 
						|
 | 
						|
function connecting.keypressed( x, y )
 | 
						|
  return scene.browser()
 | 
						|
end
 | 
						|
 | 
						|
function connecting.draw()
 | 
						|
  lg.setColor( 1,1,1,1 )
 | 
						|
  lg.printf( ("ADDRESS: %s"):format( ip ), lg.getWidth() / 4, 15, lg.getWidth() - 30, "left" )
 | 
						|
  lg.printf( ("ATTEMPTS: %d"):format( attempts ), lg.getWidth() / 4, 20 + lg.getFont():getHeight(), lg.getWidth() - 30, "left" )
 | 
						|
  cancelButton:draw()
 | 
						|
  return false
 | 
						|
end
 | 
						|
 | 
						|
function connecting.svChimo( msg )
 | 
						|
  return server.answerChallenge( msg.nonce )
 | 
						|
end
 | 
						|
 | 
						|
function connecting.connected( msg )
 | 
						|
  server.setToken( msg.token )
 | 
						|
  return scene.game()
 | 
						|
end
 | 
						|
 | 
						|
function connecting.keypressed(key, code, isrepeat)
 | 
						|
  if code == "escape" then return scene.browser() end
 | 
						|
end
 | 
						|
 | 
						|
function connecting.read( msg )
 | 
						|
  if not msg then return false end
 | 
						|
  local msgs, types = server.deserialise( msg )
 | 
						|
  for i = 1, #msgs do
 | 
						|
    ( connecting[ types[i] ] or print )( msgs[i], ip, port )
 | 
						|
  end
 | 
						|
  return connecting.read( server.receive() )
 | 
						|
end
 | 
						|
 | 
						|
function connecting.update(dt)
 | 
						|
  time = time + dt
 | 
						|
  
 | 
						|
  if time > 2 then
 | 
						|
    time = 0
 | 
						|
    attempts = attempts + 1
 | 
						|
    server.connect( ip, port )
 | 
						|
    --return scene.loadScene( scene.game )
 | 
						|
  end
 | 
						|
 | 
						|
  return connecting.read( server.receive() )
 | 
						|
end
 | 
						|
 | 
						|
function connecting:onLoad( params )
 | 
						|
  lg.setCanvas()
 | 
						|
  time = 0
 | 
						|
  attempts = 1
 | 
						|
  params = params or { ip = "8.8.8.8", port = 8 }
 | 
						|
  ip, port = params.ip, params.port
 | 
						|
  if server.isValid( ip, port ) then 
 | 
						|
    server.setInfo( params.svInfo )
 | 
						|
    return server.connect( ip, port )
 | 
						|
  end
 | 
						|
end
 | 
						|
 | 
						|
function connecting.exit()
 | 
						|
  
 | 
						|
end
 | 
						|
 | 
						|
scene.connecting = connecting
 | 
						|
return connecting |