lobby.gd 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. extends Control
  2. func _ready():
  3. # Called every time the node is added to the scene.
  4. gamestate.connect("connection_failed", self, "_on_connection_failed")
  5. gamestate.connect("connection_succeeded", self, "_on_connection_success")
  6. gamestate.connect("player_list_changed", self, "refresh_lobby")
  7. gamestate.connect("game_ended", self, "_on_game_ended")
  8. gamestate.connect("game_error", 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_minsize()
  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 get_tree().is_network_server()
  64. func _on_start_pressed():
  65. gamestate.begin_game()
  66. func _on_find_public_ip_pressed():
  67. OS.shell_open("https://icanhazip.com/")