client_ui.gd 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. extends Control
  2. @onready var client: Node = $Client
  3. @onready var host: LineEdit = $VBoxContainer/Connect/Host
  4. @onready var room: LineEdit = $VBoxContainer/Connect/RoomSecret
  5. @onready var mesh: CheckBox = $VBoxContainer/Connect/Mesh
  6. func _ready() -> void:
  7. client.lobby_joined.connect(_lobby_joined)
  8. client.lobby_sealed.connect(_lobby_sealed)
  9. client.connected.connect(_connected)
  10. client.disconnected.connect(_disconnected)
  11. multiplayer.connected_to_server.connect(_mp_server_connected)
  12. multiplayer.connection_failed.connect(_mp_server_disconnect)
  13. multiplayer.server_disconnected.connect(_mp_server_disconnect)
  14. multiplayer.peer_connected.connect(_mp_peer_connected)
  15. multiplayer.peer_disconnected.connect(_mp_peer_disconnected)
  16. @rpc("any_peer", "call_local")
  17. func ping(argument: float) -> void:
  18. _log("[Multiplayer] Ping from peer %d: arg: %f" % [multiplayer.get_remote_sender_id(), argument])
  19. func _mp_server_connected() -> void:
  20. _log("[Multiplayer] Server connected (I am %d)" % client.rtc_mp.get_unique_id())
  21. func _mp_server_disconnect() -> void:
  22. _log("[Multiplayer] Server disconnected (I am %d)" % client.rtc_mp.get_unique_id())
  23. func _mp_peer_connected(id: int) -> void:
  24. _log("[Multiplayer] Peer %d connected" % id)
  25. func _mp_peer_disconnected(id: int) -> void:
  26. _log("[Multiplayer] Peer %d disconnected" % id)
  27. func _connected(id: int, use_mesh: bool) -> void:
  28. _log("[Signaling] Server connected with ID: %d. Mesh: %s" % [id, use_mesh])
  29. func _disconnected() -> void:
  30. _log("[Signaling] Server disconnected: %d - %s" % [client.code, client.reason])
  31. func _lobby_joined(lobby: String) -> void:
  32. _log("[Signaling] Joined lobby %s" % lobby)
  33. func _lobby_sealed() -> void:
  34. _log("[Signaling] Lobby has been sealed")
  35. func _log(msg: String) -> void:
  36. print(msg)
  37. $VBoxContainer/TextEdit.text += str(msg) + "\n"
  38. func _on_peers_pressed() -> void:
  39. _log(str(multiplayer.get_peers()))
  40. func _on_ping_pressed() -> void:
  41. ping.rpc(randf())
  42. func _on_seal_pressed() -> void:
  43. client.seal_lobby()
  44. func _on_start_pressed() -> void:
  45. client.start(host.text, room.text, mesh.button_pressed)
  46. func _on_stop_pressed() -> void:
  47. client.stop()