main.gd 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. extends Control
  2. const DEF_PORT = 8080
  3. const PROTO_NAME = "ludus"
  4. @onready var _host_btn = $Panel/VBoxContainer/HBoxContainer2/HBoxContainer/Host
  5. @onready var _connect_btn = $Panel/VBoxContainer/HBoxContainer2/HBoxContainer/Connect
  6. @onready var _disconnect_btn = $Panel/VBoxContainer/HBoxContainer2/HBoxContainer/Disconnect
  7. @onready var _name_edit = $Panel/VBoxContainer/HBoxContainer/NameEdit
  8. @onready var _host_edit = $Panel/VBoxContainer/HBoxContainer2/Hostname
  9. @onready var _game = $Panel/VBoxContainer/Game
  10. var peer = WebSocketMultiplayerPeer.new()
  11. func _init():
  12. peer.supported_protocols = ["ludus"]
  13. func _ready():
  14. multiplayer.peer_connected.connect(self._peer_connected)
  15. multiplayer.peer_disconnected.connect(self._peer_disconnected)
  16. multiplayer.server_disconnected.connect(self._close_network)
  17. multiplayer.connection_failed.connect(self._close_network)
  18. multiplayer.connected_to_server.connect(self._connected)
  19. $AcceptDialog.get_label().horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
  20. $AcceptDialog.get_label().vertical_alignment = VERTICAL_ALIGNMENT_CENTER
  21. # Set the player name according to the system username. Fallback to the path.
  22. if OS.has_environment("USERNAME"):
  23. _name_edit.text = OS.get_environment("USERNAME")
  24. else:
  25. var desktop_path = OS.get_system_dir(0).replace("\\", "/").split("/")
  26. _name_edit.text = desktop_path[desktop_path.size() - 2]
  27. func start_game():
  28. _host_btn.disabled = true
  29. _name_edit.editable = false
  30. _host_edit.editable = false
  31. _connect_btn.hide()
  32. _disconnect_btn.show()
  33. _game.start()
  34. func stop_game():
  35. _host_btn.disabled = false
  36. _name_edit.editable = true
  37. _host_edit.editable = true
  38. _disconnect_btn.hide()
  39. _connect_btn.show()
  40. _game.stop()
  41. func _close_network():
  42. stop_game()
  43. $AcceptDialog.popup_centered()
  44. $AcceptDialog.get_ok_button().grab_focus()
  45. multiplayer.multiplayer_peer = null
  46. peer.close()
  47. func _connected():
  48. _game.set_player_name.rpc(_name_edit.text)
  49. func _peer_connected(id):
  50. _game.on_peer_add(id)
  51. func _peer_disconnected(id):
  52. print("Disconnected %d" % id)
  53. _game.on_peer_del(id)
  54. func _on_Host_pressed():
  55. multiplayer.multiplayer_peer = null
  56. peer.create_server(DEF_PORT)
  57. multiplayer.multiplayer_peer = peer
  58. _game.add_player(1, _name_edit.text)
  59. start_game()
  60. func _on_Disconnect_pressed():
  61. _close_network()
  62. func _on_Connect_pressed():
  63. multiplayer.multiplayer_peer = null
  64. peer.create_client("ws://" + _host_edit.text + ":" + str(DEF_PORT))
  65. multiplayer.multiplayer_peer = peer
  66. start_game()