1
+ /*
2
+ * Copyright (c) godot-rust; Bromeon and contributors.
3
+ * This Source Code Form is subject to the terms of the Mozilla Public
4
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
5
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
+ */
7
+
1
8
use crate :: bomb_spawner:: BombArgs ;
9
+ use crate :: constants:: {
10
+ SIGNAL_CONNECTED_TO_SERVER , SIGNAL_CONNECTION_FAILED , SIGNAL_CONNECTION_SUCCEEDED ,
11
+ SIGNAL_GAME_ENDED , SIGNAL_GAME_ERROR , SIGNAL_GAME_STARTED , SIGNAL_PEER_CONNECTED ,
12
+ SIGNAL_PEER_DISCONNECTED , SIGNAL_PLAYER_LIST_CHANGED , SIGNAL_SERVER_DISCONNECTED ,
13
+ } ;
2
14
use crate :: player:: Player ;
3
15
use crate :: world:: World ;
4
16
use godot:: classes:: { ENetMultiplayerPeer , Engine , Marker2D , MultiplayerApi } ;
5
17
use godot:: obj:: { bounds, Bounds } ;
6
18
use godot:: prelude:: * ;
7
19
use std:: collections:: HashMap ;
8
20
9
- /// In our example the GameState is being registered both as an Autoload and Engine Singleton
10
- /// Autoloads and Engine Singletons works similar to each other.
21
+ /// In our example the GameState is being registered both as an [ Autoload](https://docs.godotengine.org/en/stable/tutorials/scripting/singletons_autoload.html) and Engine Singleton
22
+ /// Autoloads and Engine Singletons works similarly to each other.
11
23
/// Engine singleton is anything that inherits an object and is accessible anywhere inside godot's runtime via Engine::singleton().get_singleton(…)
12
- /// Autoload is anything that lives in scene tree and is registered as Variant via ScriptServer
13
- /// In real-world usage you might want to declare your singleton as an Object and register it inside your MainLoop
24
+ /// Autoload is anything that lives in a scene tree and is registered as a Variant via ScriptServer
25
+ /// In real-world usage you might want to declare your singleton as an Object and register it in your [ MainLoop](https://docs.godotengine.org/en/stable/classes/class_mainloop.html) or [during initialization of your gdext library](https://godot-rust.github.io/book/recipes/engine-singleton.html).
14
26
#[ derive( GodotClass ) ]
15
27
#[ class( init, base=Node ) ]
16
28
pub struct GameState {
@@ -47,23 +59,23 @@ impl INode for GameState {
47
59
48
60
let player_connected = self . base ( ) . callable ( "player_connected" ) ;
49
61
self . multiplayer
50
- . connect ( "peer_connected" . into ( ) , player_connected) ;
62
+ . connect ( SIGNAL_PEER_CONNECTED , & player_connected) ;
51
63
52
64
let player_disconnected = self . base ( ) . callable ( "player_disconnected" ) ;
53
65
self . multiplayer
54
- . connect ( "peer_disconnected" . into ( ) , player_disconnected) ;
66
+ . connect ( SIGNAL_PEER_DISCONNECTED , & player_disconnected) ;
55
67
56
68
let connected_ok = self . base ( ) . callable ( "connected_ok" ) ;
57
69
self . multiplayer
58
- . connect ( "connected_to_server" . into ( ) , connected_ok) ;
70
+ . connect ( SIGNAL_CONNECTED_TO_SERVER , & connected_ok) ;
59
71
60
72
let connected_fail = self . base ( ) . callable ( "connected_fail" ) ;
61
73
self . multiplayer
62
- . connect ( "connection_failed" . into ( ) , connected_fail) ;
74
+ . connect ( SIGNAL_CONNECTION_FAILED , & connected_fail) ;
63
75
64
76
let server_disconnected = self . base ( ) . callable ( "server_disconnected" ) ;
65
77
self . multiplayer
66
- . connect ( "server_disconnected" . into ( ) , server_disconnected) ;
78
+ . connect ( SIGNAL_SERVER_DISCONNECTED , & server_disconnected) ;
67
79
}
68
80
}
69
81
@@ -106,19 +118,16 @@ impl GameState {
106
118
#[ func]
107
119
fn player_connected ( & mut self , player_id : i32 ) {
108
120
let player_name_arg = self . player_name . clone ( ) . to_variant ( ) ;
109
- self . base_mut ( ) . rpc_id (
110
- player_id as i64 ,
111
- "register_player" . into ( ) ,
112
- & [ player_name_arg] ,
113
- ) ;
121
+ self . base_mut ( )
122
+ . rpc_id ( player_id as i64 , "register_player" , & [ player_name_arg] ) ;
114
123
}
115
124
116
125
#[ func]
117
126
fn player_disconnected ( & mut self , player_id : i32 ) {
118
127
if self . game_board . is_some ( ) && self . multiplayer . is_server ( ) {
119
128
let message = GString :: from ( format ! ( "Player {player_id} Disconnected" ) ) ;
120
129
self . base_mut ( )
121
- . emit_signal ( "game_error" . into ( ) , & [ message. to_variant ( ) ] ) ;
130
+ . emit_signal ( SIGNAL_GAME_ERROR , & [ message. to_variant ( ) ] ) ;
122
131
self . end_game ( ) ;
123
132
return ;
124
133
}
@@ -128,28 +137,26 @@ impl GameState {
128
137
#[ func]
129
138
fn connected_ok ( & mut self ) {
130
139
self . base_mut ( )
131
- . emit_signal ( "connection_succeeded" . into ( ) , & [ ] ) ;
140
+ . emit_signal ( SIGNAL_CONNECTION_SUCCEEDED , & [ ] ) ;
132
141
}
133
142
134
143
#[ func]
135
144
fn server_disconnected ( & mut self ) {
136
145
self . base_mut ( )
137
- . emit_signal ( "game_error" . into ( ) , & [ "server disconnected" . to_variant ( ) ] ) ;
146
+ . emit_signal ( SIGNAL_GAME_ERROR , & [ "server disconnected" . to_variant ( ) ] ) ;
138
147
}
139
148
140
149
#[ rpc( any_peer) ]
141
150
fn register_player ( & mut self , new_player_name : GString ) {
142
151
let id = self . multiplayer . get_remote_sender_id ( ) ;
143
152
self . players . entry ( id) . or_insert ( new_player_name) ;
144
- self . base_mut ( )
145
- . emit_signal ( "player_list_changed" . into ( ) , & [ ] ) ;
153
+ self . base_mut ( ) . emit_signal ( SIGNAL_PLAYER_LIST_CHANGED , & [ ] ) ;
146
154
}
147
155
148
156
#[ func]
149
157
fn unregister_player ( & mut self , player_id : i32 ) {
150
158
self . players . remove ( & player_id) ;
151
- self . base_mut ( )
152
- . emit_signal ( "player_list_changed" . into ( ) , & [ ] ) ;
159
+ self . base_mut ( ) . emit_signal ( SIGNAL_PLAYER_LIST_CHANGED , & [ ] ) ;
153
160
}
154
161
155
162
#[ func]
@@ -170,7 +177,7 @@ impl GameState {
170
177
pub fn join_game ( & mut self , address : GString , new_player_name : GString ) {
171
178
self . player_name = new_player_name;
172
179
let mut peer = ENetMultiplayerPeer :: new_gd ( ) ;
173
- peer. create_client ( address, Self :: DEFAULT_PORT ) ;
180
+ peer. create_client ( & address, Self :: DEFAULT_PORT ) ;
174
181
self . multiplayer . set_multiplayer_peer ( & peer) ;
175
182
self . peer = Some ( peer) ;
176
183
self . players
@@ -183,7 +190,7 @@ impl GameState {
183
190
if !self . multiplayer . is_server ( ) {
184
191
panic ! ( "Only server can start a game!" )
185
192
}
186
- self . base_mut ( ) . rpc ( "load_world" . into ( ) , & [ ] ) ;
193
+ self . base_mut ( ) . rpc ( "load_world" , & [ ] ) ;
187
194
let Some ( world) = self . game_board . as_mut ( ) else {
188
195
panic ! ( "no game board!" )
189
196
} ;
@@ -203,11 +210,11 @@ impl GameState {
203
210
} ;
204
211
player. bind_mut ( ) . synced_position = spawn_marker. get_position ( ) ;
205
212
player. bind_mut ( ) . player_id = * player_id;
206
- player. set_name ( GString :: from ( player_id. to_string ( ) ) ) ;
213
+ player. set_name ( & player_id. to_string ( ) ) ;
207
214
world. bind_mut ( ) . players . add_child ( & player) ;
208
- player. bind_mut ( ) . label . set_text ( player_name. clone ( ) ) ;
215
+ player. bind_mut ( ) . label . set_text ( player_name) ;
209
216
}
210
- self . base_mut ( ) . emit_signal ( "game_started" . into ( ) , & [ ] ) ;
217
+ self . base_mut ( ) . emit_signal ( SIGNAL_GAME_STARTED , & [ ] ) ;
211
218
}
212
219
213
220
#[ rpc( call_local) ]
@@ -242,7 +249,7 @@ impl GameState {
242
249
if let Some ( mut game_board) = self . game_board . take ( ) {
243
250
game_board. queue_free ( ) ;
244
251
}
245
- self . base_mut ( ) . emit_signal ( "game_ended" . into ( ) , & [ ] ) ;
252
+ self . base_mut ( ) . emit_signal ( SIGNAL_GAME_ENDED , & [ ] ) ;
246
253
self . players . clear ( ) ;
247
254
if let Some ( peer) = self . peer . as_mut ( ) {
248
255
peer. close ( ) ;
@@ -274,23 +281,19 @@ pub trait GameSingleton:
274
281
275
282
fn singleton ( ) -> Gd < Self > {
276
283
Engine :: singleton ( )
277
- . get_singleton ( Self :: singleton_name ( ) )
284
+ . get_singleton ( Self :: NAME )
278
285
. unwrap ( )
279
286
. cast :: < Self > ( )
280
287
}
281
288
282
- fn singleton_name ( ) -> StringName {
283
- StringName :: from ( Self :: NAME )
284
- }
285
-
286
289
fn register ( game_system : Gd < Object > ) {
287
290
// in real world usage you might want to use this method to create your autoload.
288
291
// In such case add trait bound 'NewAlloc'
289
292
// and create your game system with `let game_system = Self::new_alloc();`
290
- Engine :: singleton ( ) . register_singleton ( Self :: singleton_name ( ) , game_system) ;
293
+ Engine :: singleton ( ) . register_singleton ( Self :: NAME , & game_system) ;
291
294
}
292
295
293
296
fn exit ( & mut self ) {
294
- Engine :: singleton ( ) . unregister_singleton ( Self :: singleton_name ( ) ) ;
297
+ Engine :: singleton ( ) . unregister_singleton ( Self :: NAME ) ;
295
298
}
296
299
}
0 commit comments