Skip to content

Commit 87a930e

Browse files
committed
- Update example to keep it in line with the latest gdext changes
- Move all constants to their own file - Add copyright information - Fix paths for compiled libraries
1 parent 204fbd8 commit 87a930e

File tree

12 files changed

+198
-91
lines changed

12 files changed

+198
-91
lines changed

examples/multiplayer-bomber/godot/rust.gdextension

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ entry_symbol = "gdext_rust_init"
33
compatibility_minimum = 4.1
44

55
[libraries]
6-
linux.debug.x86_64 = "res://../../../target/debug/multiplayer_bomber.so"
7-
linux.release.x86_64 = "res://../../../target/release/multiplayer_bomber.so"
6+
linux.debug.x86_64 = "res://../../../target/debug/libmultiplayer_bomber.so"
7+
linux.release.x86_64 = "res://../../../target/release/libmultiplayer_bomber.so"
88
windows.debug.x86_64 = "res://../../../target/debug/multiplayer_bomber.dll"
99
windows.release.x86_64 = "res://../../../target/release/multiplayer_bomber.dll"
10-
macos.debug = "res://../../../target/debug/multiplayer_bomber.dylib"
11-
macos.release = "res://../../../target/release/multiplayer_bomber.dylib"
12-
macos.debug.arm64 = "res://../../../target/debug/multiplayer_bomber.dylib"
13-
macos.release.arm64 = "res://../../../target/release/multiplayer_bomber.dylib"
10+
macos.debug = "res://../../../target/debug/libmultiplayer_bomber.dylib"
11+
macos.release = "res://../../../target/release/libmultiplayer_bomber.dylib"
12+
macos.debug.arm64 = "res://../../../target/debug/libmultiplayer_bomber.dylib"
13+
macos.release.arm64 = "res://../../../target/release/libmultiplayer_bomber.dylib"
1414
web.debug.wasm32 = "res://../../../target/wasm32-unknown-emscripten/debug/multiplayer_bomber.wasm"
1515
web.release.wasm32 = "res://../../../target/wasm32-unknown-emscripten/release/multiplayer_bomber.wasm"

examples/multiplayer-bomber/rust/src/bomb.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
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+
18
use godot::classes::{Area2D, PhysicsRayQueryParameters2D, TileMap};
29
use godot::prelude::*;
310

@@ -26,7 +33,7 @@ impl Bomb {
2633
panic!("couldn't access World2D from Bomb!")
2734
};
2835
for mut collider in self.base().get_overlapping_bodies().iter_shared() {
29-
if !collider.has_method("exploded".into()) {
36+
if !collider.has_method("exploded") {
3037
continue;
3138
}
3239
// check if there is any solid wall between bomb and the object
@@ -37,7 +44,7 @@ impl Bomb {
3744
panic!("couldn't create PhysicsRayQueryParameters!")
3845
};
3946
query.set_hit_from_inside(true);
40-
let result = space_state.intersect_ray(query);
47+
let result = space_state.intersect_ray(&query);
4148
let is_wall_between = result
4249
.get("collider")
4350
.map(|c| c.try_to::<Gd<TileMap>>().is_ok())
@@ -46,7 +53,7 @@ impl Bomb {
4653
continue;
4754
}
4855

49-
collider.rpc("exploded".into(), &[self.from_player.to_variant()]);
56+
collider.rpc("exploded", &[self.from_player.to_variant()]);
5057
}
5158
}
5259

examples/multiplayer-bomber/rust/src/bomb_spawner.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
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+
18
use crate::bomb::Bomb;
9+
use crate::constants::SIGNAL_SPAWN_BOMB;
210
use crate::game_state::{GameSingleton, GameState};
311
use godot::classes::{IMultiplayerSpawner, MultiplayerSpawner};
412
use godot::prelude::*;
@@ -40,7 +48,10 @@ impl FromGodot for BombArgs {
4048
}
4149

4250
impl ToGodot for BombArgs {
43-
type ToVia<'v> = VariantArray where Self: 'v;
51+
type ToVia<'v>
52+
= VariantArray
53+
where
54+
Self: 'v;
4455

4556
fn to_godot(&self) -> Self::ToVia<'_> {
4657
varray![self.position.to_variant(), self.player_id.to_variant()]
@@ -59,9 +70,9 @@ pub struct BombSpawner {
5970
impl IMultiplayerSpawner for BombSpawner {
6071
fn ready(&mut self) {
6172
let spawn_bomb = self.base().callable("spawn_bomb");
62-
self.base_mut().set_spawn_function(spawn_bomb);
73+
self.base_mut().set_spawn_function(&spawn_bomb);
6374
let spawn = self.base().callable("spawn");
64-
GameState::singleton().connect("spawn_bomb".into(), spawn);
75+
GameState::singleton().connect(SIGNAL_SPAWN_BOMB, &spawn);
6576
}
6677
}
6778

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
8+
pub const FLAG_CONNECT_DEFERRED: u32 = 1;
9+
10+
pub const SIGNAL_CHILD_EXITING_TREE: &str = "child_exiting_tree";
11+
pub const SIGNAL_SCORE_INCREASED: &str = "score_increased";
12+
pub const SIGNAL_CONNECTION_SUCCEEDED: &str = "connection_succeeded";
13+
pub const SIGNAL_CONNECTION_FAILED: &str = "connection_failed";
14+
pub const SIGNAL_PLAYER_LIST_CHANGED: &str = "player_list_changed";
15+
pub const SIGNAL_GAME_ENDED: &str = "game_ended";
16+
pub const SIGNAL_GAME_ERROR: &str = "game_error";
17+
pub const SIGNAL_GAME_STARTED: &str = "game_started";
18+
pub const SIGNAL_PEER_CONNECTED: &str = "peer_connected";
19+
pub const SIGNAL_PEER_DISCONNECTED: &str = "peer_disconnected";
20+
pub const SIGNAL_CONNECTED_TO_SERVER: &str = "connected_to_server";
21+
pub const SIGNAL_SERVER_DISCONNECTED: &str = "server_disconnected";
22+
23+
pub const SIGNAL_SPAWN_BOMB: &str = "spawn_bomb";
24+
25+
pub const ANIMATION_EXPLODE: &str = "explode";
26+
pub const ANIMATION_STUNNED: &str = "stunned";
27+
28+
pub const ANIMATION_WALK_DOWN: &str = "walk_down";
29+
pub const ANIMATION_WALK_UP: &str = "walk_up";
30+
pub const ANIMATION_WALK_RIGHT: &str = "walk_right";
31+
pub const ANIMATION_WALK_LEFT: &str = "walk_left";
32+
pub const ANIMATION_STANDING: &str = "standing";
33+
34+
pub const ACTION_MOVE_LEFT: &str = "move_left";
35+
pub const ACTION_MOVE_RIGHT: &str = "move_right";
36+
pub const ACTION_MOVE_UP: &str = "move_up";
37+
pub const ACTION_MOVE_DOWN: &str = "move_down";
38+
pub const ACTION_SET_BOMB: &str = "set_bomb";

examples/multiplayer-bomber/rust/src/game_state.rs

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
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+
18
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+
};
214
use crate::player::Player;
315
use crate::world::World;
416
use godot::classes::{ENetMultiplayerPeer, Engine, Marker2D, MultiplayerApi};
517
use godot::obj::{bounds, Bounds};
618
use godot::prelude::*;
719
use std::collections::HashMap;
820

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.
1123
/// 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).
1426
#[derive(GodotClass)]
1527
#[class(init, base=Node)]
1628
pub struct GameState {
@@ -47,23 +59,23 @@ impl INode for GameState {
4759

4860
let player_connected = self.base().callable("player_connected");
4961
self.multiplayer
50-
.connect("peer_connected".into(), player_connected);
62+
.connect(SIGNAL_PEER_CONNECTED, &player_connected);
5163

5264
let player_disconnected = self.base().callable("player_disconnected");
5365
self.multiplayer
54-
.connect("peer_disconnected".into(), player_disconnected);
66+
.connect(SIGNAL_PEER_DISCONNECTED, &player_disconnected);
5567

5668
let connected_ok = self.base().callable("connected_ok");
5769
self.multiplayer
58-
.connect("connected_to_server".into(), connected_ok);
70+
.connect(SIGNAL_CONNECTED_TO_SERVER, &connected_ok);
5971

6072
let connected_fail = self.base().callable("connected_fail");
6173
self.multiplayer
62-
.connect("connection_failed".into(), connected_fail);
74+
.connect(SIGNAL_CONNECTION_FAILED, &connected_fail);
6375

6476
let server_disconnected = self.base().callable("server_disconnected");
6577
self.multiplayer
66-
.connect("server_disconnected".into(), server_disconnected);
78+
.connect(SIGNAL_SERVER_DISCONNECTED, &server_disconnected);
6779
}
6880
}
6981

@@ -106,19 +118,16 @@ impl GameState {
106118
#[func]
107119
fn player_connected(&mut self, player_id: i32) {
108120
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]);
114123
}
115124

116125
#[func]
117126
fn player_disconnected(&mut self, player_id: i32) {
118127
if self.game_board.is_some() && self.multiplayer.is_server() {
119128
let message = GString::from(format!("Player {player_id} Disconnected"));
120129
self.base_mut()
121-
.emit_signal("game_error".into(), &[message.to_variant()]);
130+
.emit_signal(SIGNAL_GAME_ERROR, &[message.to_variant()]);
122131
self.end_game();
123132
return;
124133
}
@@ -128,28 +137,26 @@ impl GameState {
128137
#[func]
129138
fn connected_ok(&mut self) {
130139
self.base_mut()
131-
.emit_signal("connection_succeeded".into(), &[]);
140+
.emit_signal(SIGNAL_CONNECTION_SUCCEEDED, &[]);
132141
}
133142

134143
#[func]
135144
fn server_disconnected(&mut self) {
136145
self.base_mut()
137-
.emit_signal("game_error".into(), &["server disconnected".to_variant()]);
146+
.emit_signal(SIGNAL_GAME_ERROR, &["server disconnected".to_variant()]);
138147
}
139148

140149
#[rpc(any_peer)]
141150
fn register_player(&mut self, new_player_name: GString) {
142151
let id = self.multiplayer.get_remote_sender_id();
143152
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, &[]);
146154
}
147155

148156
#[func]
149157
fn unregister_player(&mut self, player_id: i32) {
150158
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, &[]);
153160
}
154161

155162
#[func]
@@ -170,7 +177,7 @@ impl GameState {
170177
pub fn join_game(&mut self, address: GString, new_player_name: GString) {
171178
self.player_name = new_player_name;
172179
let mut peer = ENetMultiplayerPeer::new_gd();
173-
peer.create_client(address, Self::DEFAULT_PORT);
180+
peer.create_client(&address, Self::DEFAULT_PORT);
174181
self.multiplayer.set_multiplayer_peer(&peer);
175182
self.peer = Some(peer);
176183
self.players
@@ -183,7 +190,7 @@ impl GameState {
183190
if !self.multiplayer.is_server() {
184191
panic!("Only server can start a game!")
185192
}
186-
self.base_mut().rpc("load_world".into(), &[]);
193+
self.base_mut().rpc("load_world", &[]);
187194
let Some(world) = self.game_board.as_mut() else {
188195
panic!("no game board!")
189196
};
@@ -203,11 +210,11 @@ impl GameState {
203210
};
204211
player.bind_mut().synced_position = spawn_marker.get_position();
205212
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());
207214
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);
209216
}
210-
self.base_mut().emit_signal("game_started".into(), &[]);
217+
self.base_mut().emit_signal(SIGNAL_GAME_STARTED, &[]);
211218
}
212219

213220
#[rpc(call_local)]
@@ -242,7 +249,7 @@ impl GameState {
242249
if let Some(mut game_board) = self.game_board.take() {
243250
game_board.queue_free();
244251
}
245-
self.base_mut().emit_signal("game_ended".into(), &[]);
252+
self.base_mut().emit_signal(SIGNAL_GAME_ENDED, &[]);
246253
self.players.clear();
247254
if let Some(peer) = self.peer.as_mut() {
248255
peer.close();
@@ -274,23 +281,19 @@ pub trait GameSingleton:
274281

275282
fn singleton() -> Gd<Self> {
276283
Engine::singleton()
277-
.get_singleton(Self::singleton_name())
284+
.get_singleton(Self::NAME)
278285
.unwrap()
279286
.cast::<Self>()
280287
}
281288

282-
fn singleton_name() -> StringName {
283-
StringName::from(Self::NAME)
284-
}
285-
286289
fn register(game_system: Gd<Object>) {
287290
// in real world usage you might want to use this method to create your autoload.
288291
// In such case add trait bound 'NewAlloc'
289292
// 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);
291294
}
292295

293296
fn exit(&mut self) {
294-
Engine::singleton().unregister_singleton(Self::singleton_name());
297+
Engine::singleton().unregister_singleton(Self::NAME);
295298
}
296299
}

examples/multiplayer-bomber/rust/src/inputs.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
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+
8+
use crate::constants::{
9+
ACTION_MOVE_DOWN, ACTION_MOVE_LEFT, ACTION_MOVE_RIGHT, ACTION_MOVE_UP, ACTION_SET_BOMB,
10+
};
111
use godot::prelude::*;
212

313
#[derive(GodotClass)]
@@ -12,11 +22,11 @@ pub struct PlayerInputs {
1222
impl PlayerInputs {
1323
pub fn update(&mut self) {
1424
self.motion = Input::singleton().get_vector(
15-
"move_left".into(),
16-
"move_right".into(),
17-
"move_up".into(),
18-
"move_down".into(),
25+
ACTION_MOVE_LEFT,
26+
ACTION_MOVE_RIGHT,
27+
ACTION_MOVE_UP,
28+
ACTION_MOVE_DOWN,
1929
);
20-
self.bombing = Input::singleton().is_action_pressed("set_bomb".into());
30+
self.bombing = Input::singleton().is_action_pressed(ACTION_SET_BOMB);
2131
}
2232
}

examples/multiplayer-bomber/rust/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
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+
18
mod bomb;
29
mod bomb_spawner;
10+
mod constants;
311
mod game_state;
412
mod inputs;
513
mod lobby;

0 commit comments

Comments
 (0)