lobby.gd 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. extends Control
  2. func _ready() -> void:
  3. # Called every time the node is added to the scene.
  4. gamestate.connection_failed.connect(_on_connection_failed)
  5. gamestate.connection_succeeded.connect(_on_connection_success)
  6. gamestate.player_list_changed.connect(refresh_lobby)
  7. gamestate.game_ended.connect(_on_game_ended)
  8. gamestate.game_error.connect(_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(OS.SYSTEM_DIR_DESKTOP).replace("\\", "/").split("/")
  14. $Connect/Name.text = desktop_path[desktop_path.size() - 2]
  15. func _on_host_pressed() -> void:
  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: String = $Connect/Name.text
  23. gamestate.host_game(player_name)
  24. get_window().title = ProjectSettings.get_setting("application/config/name") + ": Server (%s)" % $Connect/Name.text
  25. refresh_lobby()
  26. func _on_join_pressed() -> void:
  27. if $Connect/Name.text == "":
  28. $Connect/ErrorLabel.text = "Invalid name!"
  29. return
  30. var ip: String = $Connect/IPAddress.text
  31. if not ip.is_valid_ip_address():
  32. $Connect/ErrorLabel.text = "Invalid IP address!"
  33. return
  34. $Connect/ErrorLabel.text = ""
  35. $Connect/Host.disabled = true
  36. $Connect/Join.disabled = true
  37. var player_name: String = $Connect/Name.text
  38. gamestate.join_game(ip, player_name)
  39. get_window().title = ProjectSettings.get_setting("application/config/name") + ": Client (%s)" % $Connect/Name.text
  40. func _on_connection_success() -> void:
  41. $Connect.hide()
  42. $Players.show()
  43. func _on_connection_failed() -> void:
  44. $Connect/Host.disabled = false
  45. $Connect/Join.disabled = false
  46. $Connect/ErrorLabel.set_text("Connection failed.")
  47. func _on_game_ended() -> void:
  48. show()
  49. $Connect.show()
  50. $Players.hide()
  51. $Connect/Host.disabled = false
  52. $Connect/Join.disabled = false
  53. func _on_game_error(errtxt: String) -> void:
  54. $ErrorDialog.dialog_text = errtxt
  55. $ErrorDialog.popup_centered()
  56. $Connect/Host.disabled = false
  57. $Connect/Join.disabled = false
  58. func refresh_lobby() -> void:
  59. var players := gamestate.get_player_list()
  60. players.sort()
  61. $Players/List.clear()
  62. $Players/List.add_item(gamestate.player_name + " (you)")
  63. for p: String in players:
  64. $Players/List.add_item(p)
  65. $Players/Start.disabled = not multiplayer.is_server()
  66. func _on_start_pressed() -> void:
  67. gamestate.begin_game()
  68. func _on_find_public_ip_pressed() -> void:
  69. OS.shell_open("https://icanhazip.com/")