main.gd 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. extends Control
  2. const DEF_PORT = 8080
  3. const PROTO_NAME = "ludus"
  4. @onready var _host_btn: Button = $Panel/VBoxContainer/HBoxContainer2/HBoxContainer/Host
  5. @onready var _connect_btn: Button = $Panel/VBoxContainer/HBoxContainer2/HBoxContainer/Connect
  6. @onready var _disconnect_btn: Button = $Panel/VBoxContainer/HBoxContainer2/HBoxContainer/Disconnect
  7. @onready var _name_edit: LineEdit = $Panel/VBoxContainer/HBoxContainer/NameEdit
  8. @onready var _host_edit: LineEdit = $Panel/VBoxContainer/HBoxContainer2/Hostname
  9. @onready var _game: Control = $Panel/VBoxContainer/Game
  10. var peer := WebSocketMultiplayerPeer.new()
  11. func _init() -> void:
  12. peer.supported_protocols = ["ludus"]
  13. func _ready() -> void:
  14. multiplayer.peer_connected.connect(_peer_connected)
  15. multiplayer.peer_disconnected.connect(_peer_disconnected)
  16. multiplayer.server_disconnected.connect(_close_network)
  17. multiplayer.connection_failed.connect(_close_network)
  18. multiplayer.connected_to_server.connect(_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(OS.SYSTEM_DIR_DESKTOP).replace("\\", "/").split("/")
  26. _name_edit.text = desktop_path[desktop_path.size() - 2]
  27. func start_game() -> void:
  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() -> void:
  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() -> void:
  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() -> void:
  48. _game.set_player_name.rpc(_name_edit.text)
  49. func _peer_connected(id: int) -> void:
  50. _game.on_peer_add(id)
  51. func _peer_disconnected(id: int) -> void:
  52. print("Disconnected %d" % id)
  53. _game.on_peer_del(id)
  54. func _on_Host_pressed() -> void:
  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() -> void:
  61. _close_network()
  62. func _on_Connect_pressed() -> void:
  63. multiplayer.multiplayer_peer = null
  64. peer.create_client("ws://" + _host_edit.text + ":" + str(DEF_PORT))
  65. multiplayer.multiplayer_peer = peer
  66. start_game()