lobby.gd 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. extends Control
  2. func _ready():
  3. # Called every time the node is added to the scene.
  4. gamestate.connection_failed.connect(self._on_connection_failed)
  5. gamestate.connection_succeeded.connect(self._on_connection_success)
  6. gamestate.player_list_changed.connect(self.refresh_lobby)
  7. gamestate.game_ended.connect(self._on_game_ended)
  8. gamestate.game_error.connect(self._on_game_error)
  9. # Set the player name according to the system username. Fallback to the path.
  10. if OS.has_environment("USERNAME"):
  11. $Connect/Name.text = OS.get_environment("USERNAME")
  12. else:
  13. var desktop_path = OS.get_system_dir(0).replace("\\", "/").split("/")
  14. $Connect/Name.text = desktop_path[desktop_path.size() - 2]
  15. func _on_host_pressed():
  16. if $Connect/Name.text == "":
  17. $Connect/ErrorLabel.text = "Invalid name!"
  18. return
  19. $Connect.hide()
  20. $Players.show()
  21. $Connect/ErrorLabel.text = ""
  22. var player_name = $Connect/Name.text
  23. gamestate.host_game(player_name)
  24. refresh_lobby()
  25. func _on_join_pressed():
  26. if $Connect/Name.text == "":
  27. $Connect/ErrorLabel.text = "Invalid name!"
  28. return
  29. var ip = $Connect/IPAddress.text
  30. if not ip.is_valid_ip_address():
  31. $Connect/ErrorLabel.text = "Invalid IP address!"
  32. return
  33. $Connect/ErrorLabel.text = ""
  34. $Connect/Host.disabled = true
  35. $Connect/Join.disabled = true
  36. var player_name = $Connect/Name.text
  37. gamestate.join_game(ip, player_name)
  38. func _on_connection_success():
  39. $Connect.hide()
  40. $Players.show()
  41. func _on_connection_failed():
  42. $Connect/Host.disabled = false
  43. $Connect/Join.disabled = false
  44. $Connect/ErrorLabel.set_text("Connection failed.")
  45. func _on_game_ended():
  46. show()
  47. $Connect.show()
  48. $Players.hide()
  49. $Connect/Host.disabled = false
  50. $Connect/Join.disabled = false
  51. func _on_game_error(errtxt):
  52. $ErrorDialog.dialog_text = errtxt
  53. $ErrorDialog.popup_centered()
  54. $Connect/Host.disabled = false
  55. $Connect/Join.disabled = false
  56. func refresh_lobby():
  57. var players = gamestate.get_player_list()
  58. players.sort()
  59. $Players/List.clear()
  60. $Players/List.add_item(gamestate.get_player_name() + " (You)")
  61. for p in players:
  62. $Players/List.add_item(p)
  63. $Players/Start.disabled = not multiplayer.is_server()
  64. func _on_start_pressed():
  65. gamestate.begin_game()
  66. func _on_find_public_ip_pressed():
  67. OS.shell_open("https://icanhazip.com/")