1- use godot:: classes:: Button ;
2- use godot:: classes:: ENetMultiplayerPeer ;
3- use godot:: classes:: Label ;
4- use godot:: classes:: LineEdit ;
5- use godot:: classes:: LinkButton ;
6- use godot:: classes:: Os ;
7- use godot:: classes:: Panel ;
8- use godot:: classes:: ProjectSettings ;
9- use godot:: classes:: enet_connection:: CompressionMode ;
10- use godot:: classes:: object:: ConnectFlags ;
11- use godot:: global:: Error ;
1+ use godot:: classes:: enet_connection:: CompressionMode ;
2+ use godot:: classes:: object:: ConnectFlags ;
3+ use godot:: classes:: {
4+ Button , ENetMultiplayerPeer , Label , LineEdit , LinkButton , Os , Panel , ProjectSettings , IPanel
5+ } ;
6+ use godot:: global:: Error ;
127use godot:: prelude:: * ;
138
9+ use crate :: pong:: Pong ;
10+
1411const DEFAULT_PORT : i32 = 8910 ;
1512
1613#[ derive( GodotClass ) ]
17- #[ class( base=Panel ) ]
14+ #[ class( init , base=Panel ) ]
1815pub struct Lobby {
1916 #[ export]
20- address : Option < Gd < LineEdit > > ,
17+ address : OnEditor < Gd < LineEdit > > ,
2118 #[ export]
22- host_button : Option < Gd < Button > > ,
19+ host_button : OnEditor < Gd < Button > > ,
2320 #[ export]
24- join_button : Option < Gd < Button > > ,
21+ join_button : OnEditor < Gd < Button > > ,
2522 #[ export]
26- status_ok : Option < Gd < Label > > ,
23+ status_ok : OnEditor < Gd < Label > > ,
2724 #[ export]
28- status_fail : Option < Gd < Label > > ,
25+ status_fail : OnEditor < Gd < Label > > ,
2926 #[ export]
30- port_forward_label : Option < Gd < Label > > ,
27+ port_forward_label : OnEditor < Gd < Label > > ,
3128 #[ export]
32- find_public_ip_button : Option < Gd < LinkButton > > ,
29+ find_public_ip_button : OnEditor < Gd < LinkButton > > ,
3330 peer : Option < Gd < ENetMultiplayerPeer > > ,
3431 base : Base < Panel > ,
3532}
3633
37- use godot:: classes:: IPanel ;
38-
39- use crate :: pong:: Pong ;
40-
4134#[ godot_api]
4235impl IPanel for Lobby {
43- fn init ( base : Base < Panel > ) -> Self {
44- Self {
45- address : None ,
46- host_button : None ,
47- join_button : None ,
48- status_ok : None ,
49- status_fail : None ,
50- port_forward_label : None ,
51- find_public_ip_button : None ,
52- peer : None ,
53- base,
54- }
55- }
5636
5737 fn ready ( & mut self ) {
5838 /*
59- # Connect all the callbacks related to networking.
39+ # Connect all the callbacks related to networking.
6040 multiplayer.peer_connected.connect(_player_connected)
6141 multiplayer.peer_disconnected.connect(_player_disconnected)
6242 multiplayer.connected_to_server.connect(_connected_ok)
6343 multiplayer.connection_failed.connect(_connected_fail)
6444 multiplayer.server_disconnected.connect(_server_disconnected)
65- */
45+ */
6646 let multiplayer = self . base ( ) . get_multiplayer ( ) . unwrap ( ) ;
6747 let gd_ref = self . to_gd ( ) ;
6848 multiplayer
@@ -72,17 +52,15 @@ impl IPanel for Lobby {
7252 . connect_other_gd ( & gd_ref, |mut this : Gd < Self > , _id : i64 | {
7353 godot_print ! ( "Someone connected, start the game!" ) ;
7454 let pong: Gd < Pong > = load :: < PackedScene > ( "res://pong.tscn" )
75- . instantiate ( )
76- . unwrap ( )
77- . cast ( ) ;
55+ . instantiate_as :: < Pong > ( ) ;
7856 // Connect deferred so we can safely erase it from the callback.
7957 pong. signals ( )
8058 . game_finished ( )
8159 . builder ( )
8260 . flags ( ConnectFlags :: DEFERRED )
8361 . connect_other_gd ( & this, |mut this : Gd < Self > | {
8462 this. bind_mut ( )
85- . _end_game ( "Client disconnected." . to_string ( ) ) ;
63+ . end_game ( "Client disconnected." . to_string ( ) ) ;
8664 } ) ;
8765
8866 this. bind_mut ( )
@@ -92,17 +70,17 @@ impl IPanel for Lobby {
9270 . get_root ( )
9371 . unwrap ( )
9472 . add_child ( & pong) ;
95- this. bind_mut ( ) . base_mut ( ) . hide ( ) ;
73+ this. hide ( ) ;
9674 } ) ;
9775 multiplayer
9876 . signals ( )
9977 . peer_disconnected ( )
10078 . builder ( )
10179 . connect_other_mut ( & self . to_gd ( ) , |this : & mut Self , _id : i64 | {
10280 if this. base ( ) . get_multiplayer ( ) . unwrap ( ) . is_server ( ) {
103- this. _end_game ( "Client disconnected." . to_string ( ) ) ;
81+ this. end_game ( "Client disconnected." . to_string ( ) ) ;
10482 } else {
105- this. _end_game ( "Server disconnected." . to_string ( ) ) ;
83+ this. end_game ( "Server disconnected." . to_string ( ) ) ;
10684 }
10785 } ) ;
10886 multiplayer
@@ -117,81 +95,76 @@ impl IPanel for Lobby {
11795 . connection_failed ( )
11896 . builder ( )
11997 . connect_other_mut ( & self . to_gd ( ) , |this : & mut Self | {
120- this. _set_status ( "Couldn't connect." . to_string ( ) , false ) ;
98+ this. set_status ( "Couldn't connect." , false ) ;
12199 let mut multiplayer = this. base ( ) . get_multiplayer ( ) . unwrap ( ) ;
122100 multiplayer. set_multiplayer_peer ( Gd :: null_arg ( ) ) ; // Remove peer.
123- this. host_button . as_mut ( ) . unwrap ( ) . set_disabled ( false ) ;
124- this. join_button . as_mut ( ) . unwrap ( ) . set_disabled ( false ) ;
101+ this. host_button . set_disabled ( false ) ;
102+ this. join_button . set_disabled ( false ) ;
125103 } ) ;
126104 multiplayer
127105 . signals ( )
128106 . server_disconnected ( )
129107 . builder ( )
130108 . connect_other_mut ( & self . to_gd ( ) , |this : & mut Self | {
131- this. _end_game ( "Server disconnected." . to_string ( ) ) ;
109+ this. end_game ( "Server disconnected." . to_string ( ) ) ;
132110 } ) ;
133111
134- let gd_ref = self . to_gd ( ) ;
135-
136- // Clone the Gd<Button> references to avoid borrowing self mutably and immutably at the same time
137- let host_button = self . host_button . as_ref ( ) . unwrap ( ) . clone ( ) ;
138- host_button
112+ self . host_button
139113 . signals ( )
140114 . pressed ( )
141115 . builder ( )
142116 . connect_other_mut ( & gd_ref, |this : & mut Self | {
143- this. _on_host_pressed ( ) ;
117+ this. on_host_pressed ( ) ;
144118 } ) ;
145119
146- let join_button = self . join_button . as_ref ( ) . unwrap ( ) . clone ( ) ;
147- join_button
120+ self . join_button
148121 . signals ( )
149122 . pressed ( )
150123 . builder ( )
151124 . connect_other_mut ( & gd_ref, |this : & mut Self | {
152- this. _on_join_pressed ( ) ;
125+ this. on_join_pressed ( ) ;
153126 } ) ;
154127 }
155128}
156129
157130#[ godot_api]
158131impl Lobby {
159- fn _set_status ( & mut self , text : String , is_ok : bool ) {
132+ fn set_status ( & mut self , text : & str , is_ok : bool ) {
160133 // Simple way to show status.
161134 if is_ok {
162- self . status_ok . as_mut ( ) . unwrap ( ) . set_text ( & text) ;
163- self . status_fail . as_mut ( ) . unwrap ( ) . set_text ( "" ) ;
135+ self . status_ok . set_text ( text) ;
136+ self . status_fail . set_text ( "" ) ;
164137 } else {
165- self . status_ok . as_mut ( ) . unwrap ( ) . set_text ( "" ) ;
166- self . status_fail . as_mut ( ) . unwrap ( ) . set_text ( & text) ;
138+ self . status_ok . set_text ( "" ) ;
139+ self . status_fail . set_text ( text) ;
167140 }
168141 }
169142
170143 #[ func]
171- fn _end_game ( & mut self , with_error : String ) {
144+ fn end_game ( & mut self , with_error : String ) {
172145 if self . base ( ) . has_node ( "/root/Pong" ) {
173146 // Erase immediately, otherwise network might show
174147 // errors (this is why we connected deferred above).
175148 self . base ( ) . get_node_as :: < Node > ( "/root/Pong" ) . free ( ) ;
176149 self . base_mut ( ) . show ( ) ;
177150 }
178-
151+
179152 let mut multiplayer = self . base ( ) . get_multiplayer ( ) . unwrap ( ) ;
180153 multiplayer. set_multiplayer_peer ( Gd :: null_arg ( ) ) ; // Remove peer.
181- self . host_button . as_mut ( ) . unwrap ( ) . set_disabled ( false ) ;
182- self . join_button . as_mut ( ) . unwrap ( ) . set_disabled ( false ) ;
154+ self . host_button . set_disabled ( false ) ;
155+ self . join_button . set_disabled ( false ) ;
183156
184- self . _set_status ( with_error, false ) ;
157+ self . set_status ( & with_error, false ) ;
185158 }
186159
187- fn _on_host_pressed ( & mut self ) {
160+ fn on_host_pressed ( & mut self ) {
188161 let mut peer = ENetMultiplayerPeer :: new_gd ( ) ;
189162 self . peer = Some ( peer. clone ( ) ) ;
190163 // Set a maximum of 1 peer, since Pong is a 2-player game.
191164 let err = peer. create_server_ex ( DEFAULT_PORT ) . max_clients ( 1 ) . done ( ) ;
192165 if err != Error :: OK {
193166 // Is another server running?
194- self . _set_status ( "Can't host, address in use." . to_string ( ) , false ) ;
167+ self . set_status ( "Can't host, address in use." , false ) ;
195168 return ;
196169 }
197170 peer. get_host ( )
@@ -200,30 +173,25 @@ impl Lobby {
200173
201174 let mut multiplayer = self . base ( ) . get_multiplayer ( ) . unwrap ( ) ;
202175 multiplayer. set_multiplayer_peer ( & peer) ;
203- self . host_button . as_mut ( ) . unwrap ( ) . set_disabled ( true ) ;
204- self . join_button . as_mut ( ) . unwrap ( ) . set_disabled ( true ) ;
205- self . _set_status ( "Waiting for player..." . to_string ( ) , true ) ;
206- let project_settings = ProjectSettings :: singleton ( ) ;
207- self . base_mut ( ) . get_window ( ) . unwrap ( ) . set_title (
208- ( project_settings
209- . get_setting ( "application/config/name" )
210- . to_string ( )
211- + ": Server" )
212- . as_str ( ) ,
213- ) ;
214-
176+ self . host_button . set_disabled ( true ) ;
177+ self . join_button . set_disabled ( true ) ;
178+ self . set_status ( "Waiting for player..." , true ) ;
179+ let application_name = ProjectSettings :: singleton ( )
180+ . get_setting ( "application/config/name" )
181+ . to_string ( ) ;
182+ self . base_mut ( )
183+ . get_window ( )
184+ . unwrap ( )
185+ . set_title ( & format ! ( "{application_name}: Server" ) ) ;
215186 // Only show hosting instructions when relevant.
216- self . port_forward_label . as_mut ( ) . unwrap ( ) . set_visible ( true ) ;
217- self . find_public_ip_button
218- . as_mut ( )
219- . unwrap ( )
220- . set_visible ( true ) ;
187+ self . port_forward_label . set_visible ( true ) ;
188+ self . find_public_ip_button . set_visible ( true ) ;
221189 }
222190
223- fn _on_join_pressed ( & mut self ) {
224- let ip = self . address . as_mut ( ) . unwrap ( ) . get_text ( ) ;
191+ fn on_join_pressed ( & mut self ) {
192+ let ip = self . address . get_text ( ) ;
225193 if !ip. is_valid_ip_address ( ) {
226- self . _set_status ( "IP address is invalid." . to_string ( ) , false ) ;
194+ self . set_status ( "IP address is invalid." , false ) ;
227195 return ;
228196 }
229197
@@ -236,15 +204,14 @@ impl Lobby {
236204 let mut multiplayer = self . base ( ) . get_multiplayer ( ) . unwrap ( ) ;
237205 multiplayer. set_multiplayer_peer ( & peer) ;
238206
239- self . _set_status ( "Connecting..." . to_string ( ) , true ) ;
240- let project_settings = ProjectSettings :: singleton ( ) ;
241- self . base_mut ( ) . get_window ( ) . unwrap ( ) . set_title (
242- ( project_settings
243- . get_setting ( "application/config/name" )
244- . to_string ( )
245- + ": Client" )
246- . as_str ( ) ,
247- ) ;
207+ self . set_status ( "Connecting..." , true ) ;
208+ let application_name = ProjectSettings :: singleton ( )
209+ . get_setting ( "application/config/name" )
210+ . to_string ( ) ;
211+ self . base_mut ( )
212+ . get_window ( )
213+ . unwrap ( )
214+ . set_title ( & format ! ( "{application_name}: Client" ) ) ;
248215 }
249216
250217 fn _on_find_public_ip_pressed ( & mut self ) {
0 commit comments