Lobby.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using Godot;
  2. using System;
  3. using Godot.Collections;
  4. public partial class Lobby : Control
  5. {
  6. private const int DefaultPort = 8910; // An arbitrary number.
  7. private const int MaxNumberOfPeers = 1; // How many people we want to have in a game
  8. private LineEdit _address;
  9. private Button _hostButton;
  10. private Button _joinButton;
  11. private Label _statusOk;
  12. private Label _statusFail;
  13. private NetworkedMultiplayerENet _peer;
  14. public override void _Ready()
  15. {
  16. // Get nodes - the generic is a class, argument is node path.
  17. _address = GetNode<LineEdit>("Address");
  18. _hostButton = GetNode<Button>("HostButton");
  19. _joinButton = GetNode<Button>("JoinButton");
  20. _statusOk = GetNode<Label>("StatusOk");
  21. _statusFail = GetNode<Label>("StatusFail");
  22. // Connect all callbacks related to networking.
  23. // Note: Use snake_case when talking to engine API.
  24. GetTree().Connect("network_peer_connected", this, nameof(PlayerConnected));
  25. GetTree().Connect("network_peer_disconnected", this, nameof(PlayerDisconnected));
  26. GetTree().Connect("connected_to_server", this, nameof(ConnectedOk));
  27. GetTree().Connect("connection_failed", this, nameof(ConnectedFail));
  28. GetTree().Connect("server_disconnected", this, nameof(ServerDisconnected));
  29. }
  30. // Network callbacks from SceneTree
  31. // Callback from SceneTree.
  32. private void PlayerConnected(int id)
  33. {
  34. // Someone connected, start the game!
  35. var pong = ResourceLoader.Load<PackedScene>("res://pong.tscn").Instantiate();
  36. // Connect deferred so we can safely erase it from the callback.
  37. pong.Connect("GameFinished", this, nameof(EndGame), new Godot.Collections.Array(), (int) ConnectFlags.Deferred);
  38. GetTree().Root.AddChild(pong);
  39. Hide();
  40. }
  41. private void PlayerDisconnected(int id)
  42. {
  43. EndGame(GetTree().IsNetworkServer() ? "Client disconnected" : "Server disconnected");
  44. }
  45. // Callback from SceneTree, only for clients (not server).
  46. private void ConnectedOk()
  47. {
  48. // This function is not needed for this project.
  49. }
  50. // Callback from SceneTree, only for clients (not server).
  51. private void ConnectedFail()
  52. {
  53. SetStatus("Couldn't connect", false);
  54. GetTree().NetworkPeer = null; // Remove peer.
  55. _hostButton.Disabled = false;
  56. _joinButton.Disabled = false;
  57. }
  58. private void ServerDisconnected()
  59. {
  60. EndGame("Server disconnected");
  61. }
  62. // Game creation functions
  63. private void EndGame(string withError = "")
  64. {
  65. if (HasNode("/root/Pong"))
  66. {
  67. // Erase immediately, otherwise network might show
  68. // errors (this is why we connected deferred above).
  69. GetNode("/root/Pong").Free();
  70. Show();
  71. }
  72. GetTree().NetworkPeer = null; // Remove peer.
  73. _hostButton.Disabled = false;
  74. _joinButton.Disabled = false;
  75. SetStatus(withError, false);
  76. }
  77. private void SetStatus(string text, bool isOk)
  78. {
  79. // Simple way to show status.
  80. if (isOk)
  81. {
  82. _statusOk.Text = text;
  83. _statusFail.Text = "";
  84. }
  85. else
  86. {
  87. _statusOk.Text = "";
  88. _statusFail.Text = text;
  89. }
  90. }
  91. private void OnHostPressed()
  92. {
  93. _peer = new NetworkedMultiplayerENet();
  94. _peer.CompressionMode = NetworkedMultiplayerENet.CompressionModeEnum.RangeCoder;
  95. Error err = _peer.CreateServer(DefaultPort, MaxNumberOfPeers);
  96. if (err != Error.Ok)
  97. {
  98. // Is another server running?
  99. SetStatus("Can't host, address in use.", false);
  100. return;
  101. }
  102. GetTree().NetworkPeer = _peer;
  103. _hostButton.Disabled = true;
  104. _joinButton.Disabled = true;
  105. SetStatus("Waiting for player...", true);
  106. }
  107. private void OnJoinPressed()
  108. {
  109. string ip = _address.Text;
  110. if (!ip.IsValidIPAddress())
  111. {
  112. SetStatus("IP address is invalid", false);
  113. return;
  114. }
  115. _peer = new NetworkedMultiplayerENet();
  116. _peer.CompressionMode = NetworkedMultiplayerENet.CompressionModeEnum.RangeCoder;
  117. _peer.CreateClient(ip, DefaultPort);
  118. GetTree().NetworkPeer = _peer;
  119. SetStatus("Connecting...", true);
  120. }
  121. }