lobby.gd 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. extends Control
  2. # Default game server port. Can be any number between 1024 and 49151.
  3. # Not present on the list of registered or common ports as of December 2022:
  4. # https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
  5. const DEFAULT_PORT = 8910
  6. @onready var address = $Address
  7. @onready var host_button = $HostButton
  8. @onready var join_button = $JoinButton
  9. @onready var status_ok = $StatusOk
  10. @onready var status_fail = $StatusFail
  11. @onready var port_forward_label = $PortForward
  12. @onready var find_public_ip_button = $FindPublicIP
  13. var peer = null
  14. func _ready():
  15. # Connect all the callbacks related to networking.
  16. multiplayer.peer_connected.connect(self._player_connected)
  17. multiplayer.peer_disconnected.connect(self._player_disconnected)
  18. multiplayer.connected_to_server.connect(self._connected_ok)
  19. multiplayer.connection_failed.connect(self._connected_fail)
  20. multiplayer.server_disconnected.connect(self._server_disconnected)
  21. #### Network callbacks from SceneTree ####
  22. # Callback from SceneTree.
  23. func _player_connected(_id):
  24. # Someone connected, start the game!
  25. var pong = load("res://pong.tscn").instantiate()
  26. # Connect deferred so we can safely erase it from the callback.
  27. pong.game_finished.connect(self._end_game, CONNECT_DEFERRED)
  28. get_tree().get_root().add_child(pong)
  29. hide()
  30. func _player_disconnected(_id):
  31. if multiplayer.is_server():
  32. _end_game("Client disconnected")
  33. else:
  34. _end_game("Server disconnected")
  35. # Callback from SceneTree, only for clients (not server).
  36. func _connected_ok():
  37. pass # This function is not needed for this project.
  38. # Callback from SceneTree, only for clients (not server).
  39. func _connected_fail():
  40. _set_status("Couldn't connect.", false)
  41. multiplayer.set_multiplayer_peer(null) # Remove peer.
  42. host_button.set_disabled(false)
  43. join_button.set_disabled(false)
  44. func _server_disconnected():
  45. _end_game("Server disconnected.")
  46. ##### Game creation functions ######
  47. func _end_game(with_error = ""):
  48. if has_node("/root/Pong"):
  49. # Erase immediately, otherwise network might show
  50. # errors (this is why we connected deferred above).
  51. get_node(^"/root/Pong").free()
  52. show()
  53. multiplayer.set_multiplayer_peer(null) # Remove peer.
  54. host_button.set_disabled(false)
  55. join_button.set_disabled(false)
  56. _set_status(with_error, false)
  57. func _set_status(text, isok):
  58. # Simple way to show status.
  59. if isok:
  60. status_ok.set_text(text)
  61. status_fail.set_text("")
  62. else:
  63. status_ok.set_text("")
  64. status_fail.set_text(text)
  65. func _on_host_pressed():
  66. peer = ENetMultiplayerPeer.new()
  67. var err = peer.create_server(DEFAULT_PORT, 1) # Maximum of 1 peer, since it's a 2-player game.
  68. if err != OK:
  69. # Is another server running?
  70. _set_status("Can't host, address in use.",false)
  71. return
  72. peer.get_host().compress(ENetConnection.COMPRESS_RANGE_CODER)
  73. multiplayer.set_multiplayer_peer(peer)
  74. host_button.set_disabled(true)
  75. join_button.set_disabled(true)
  76. _set_status("Waiting for player...", true)
  77. # Only show hosting instructions when relevant.
  78. port_forward_label.visible = true
  79. find_public_ip_button.visible = true
  80. func _on_join_pressed():
  81. var ip = address.get_text()
  82. if not ip.is_valid_ip_address():
  83. _set_status("IP address is invalid.", false)
  84. return
  85. peer = ENetMultiplayerPeer.new()
  86. peer.create_client(ip, DEFAULT_PORT)
  87. peer.get_host().compress(ENetConnection.COMPRESS_RANGE_CODER)
  88. multiplayer.set_multiplayer_peer(peer)
  89. _set_status("Connecting...", true)
  90. func _on_find_public_ip_pressed():
  91. OS.shell_open("https://icanhazip.com/")