Skip to content

Commit 8d4bff6

Browse files
committed
deal with comments
1 parent fe8a0c6 commit 8d4bff6

File tree

5 files changed

+45
-70
lines changed

5 files changed

+45
-70
lines changed

net-pong/rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "net-pong"
33
version = "1.0.0"
44
edition = "2024"
5-
rust-version = "1.88"
5+
rust-version = "1.90"
66
license = "MPL-2.0"
77
publish = false
88

net-pong/rust/src/ball.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,28 @@ use godot::prelude::*;
55
const DEFAULT_SPEED: f64 = 100.0;
66

77
#[derive(GodotClass)]
8-
#[class(base=Area2D)]
8+
#[class(init, base=Area2D)]
99
pub struct Ball {
10+
#[init(val = Vector2::LEFT)]
1011
direction: Vector2,
1112
stopped: bool,
12-
_speed: f64,
13+
#[init(val = DEFAULT_SPEED)]
14+
speed: f64,
1315
base: Base<Area2D>,
1416
}
1517

1618
#[godot_api]
1719
impl IArea2D for Ball {
18-
fn init(base: Base<Area2D>) -> Self {
19-
Self {
20-
direction: Vector2::LEFT,
21-
stopped: false,
22-
_speed: DEFAULT_SPEED,
23-
base,
24-
}
25-
}
26-
2720
fn process(&mut self, delta: f64) {
2821
let screen_size = self.base().get_viewport_rect().size;
29-
self._speed += delta;
22+
self.speed += delta;
3023

3124
if !self.stopped {
3225
// Ball will move normally for both players,
3326
// even if it's sightly out of sync between them,
3427
// so each player sees the motion as smooth and not jerky.
3528
let direction = self.direction;
36-
let translation = direction * (self._speed * delta) as f32;
29+
let translation = direction * (self.speed * delta) as f32;
3730
self.base_mut().translate(translation);
3831
}
3932

@@ -46,27 +39,30 @@ impl IArea2D for Ball {
4639
}
4740

4841
let mut parent = self.base().get_parent().unwrap().cast::<Pong>();
49-
if self.base().is_multiplayer_authority() {
50-
// Only the master will decide when the ball is out in
42+
// Allows re-entrancy – required if a game stops and we need to reset our ball.
43+
// this help fixes the double bind error
44+
let mut guard = self.base_mut();
45+
if guard.is_multiplayer_authority() {
46+
// Only the master will decide when the ball is out on
5147
// the left side (its own side). This makes the game
5248
// playable even if latency is high and ball is going
5349
// fast. Otherwise, the ball might be out in the other
5450
// player's screen but not this one.
5551
if ball_pos.x < 0.0 {
5652
let args = vslice![false];
5753
parent.rpc("update_score", args);
58-
self.base_mut().rpc("reset_ball", args);
54+
guard.rpc("reset_ball", args);
5955
}
6056
} else {
61-
// Only the puppet will decide when the ball is out in
57+
// Only the puppet will decide when the ball is out on
6258
// the right side, which is its own side. This makes
6359
// the game playable even if latency is high and ball
6460
// is going fast. Otherwise, the ball might be out in the
6561
// other player's screen but not this one.
6662
if ball_pos.x > screen_size.x {
6763
let args = vslice![true];
6864
parent.rpc("update_score", args);
69-
self.base_mut().rpc("reset_ball", args);
65+
guard.rpc("reset_ball", args);
7066
}
7167
}
7268
}
@@ -82,7 +78,7 @@ impl Ball {
8278
} else {
8379
self.direction.x = -self.direction.x.abs();
8480
}
85-
self._speed *= 1.1;
81+
self.speed *= 1.1;
8682
self.direction.y = random * 2.0 - 1.0;
8783
self.direction = self.direction.normalized();
8884
}
@@ -101,6 +97,6 @@ impl Ball {
10197
} else {
10298
self.direction = Vector2::RIGHT;
10399
}
104-
self._speed = DEFAULT_SPEED;
100+
self.speed = DEFAULT_SPEED;
105101
}
106102
}

net-pong/rust/src/lobby.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,14 @@ impl IPanel for Lobby {
5050
.builder()
5151
.connect_other_gd(&gd_ref, |mut this: Gd<Self>, _id: i64| {
5252
godot_print!("Someone connected, start the game!");
53-
let pong: Gd<Pong> =
54-
load::<PackedScene>("res://pong.tscn").instantiate_as::<Pong>();
53+
let pong = load::<PackedScene>("res://pong.tscn").instantiate_as::<Pong>();
5554
// Connect deferred so we can safely erase it from the callback.
5655
pong.signals()
5756
.game_finished()
5857
.builder()
5958
.flags(ConnectFlags::DEFERRED)
60-
.connect_other_gd(&this, |mut this: Gd<Self>| {
61-
this.bind_mut().end_game("Client disconnected.".to_string());
59+
.connect_other_mut(&this, |this| {
60+
this.end_game("Client disconnected.");
6261
});
6362

6463
this.bind_mut()
@@ -74,25 +73,18 @@ impl IPanel for Lobby {
7473
.signals()
7574
.peer_disconnected()
7675
.builder()
77-
.connect_other_mut(&self.to_gd(), |this: &mut Self, _id: i64| {
76+
.connect_other_mut(&gd_ref, |this, _id: i64| {
7877
if this.base().get_multiplayer().unwrap().is_server() {
79-
this.end_game("Client disconnected.".to_string());
78+
this.end_game("Client disconnected.");
8079
} else {
81-
this.end_game("Server disconnected.".to_string());
80+
this.end_game("Server disconnected.");
8281
}
8382
});
84-
multiplayer
85-
.signals()
86-
.connected_to_server()
87-
.builder()
88-
.connect_other_mut(&self.to_gd(), |_this: &mut Self| {
89-
// This function is not needed for this project.
90-
});
9183
multiplayer
9284
.signals()
9385
.connection_failed()
9486
.builder()
95-
.connect_other_mut(&self.to_gd(), |this: &mut Self| {
87+
.connect_other_mut(&gd_ref, |this| {
9688
this.set_status("Couldn't connect.", false);
9789
let mut multiplayer = this.base().get_multiplayer().unwrap();
9890
multiplayer.set_multiplayer_peer(Gd::null_arg()); // Remove peer.
@@ -103,23 +95,23 @@ impl IPanel for Lobby {
10395
.signals()
10496
.server_disconnected()
10597
.builder()
106-
.connect_other_mut(&self.to_gd(), |this: &mut Self| {
107-
this.end_game("Server disconnected.".to_string());
98+
.connect_other_mut(&gd_ref, |this| {
99+
this.end_game("Server disconnected.");
108100
});
109101

110102
self.host_button
111103
.signals()
112104
.pressed()
113105
.builder()
114-
.connect_other_mut(&gd_ref, |this: &mut Self| {
106+
.connect_other_mut(&gd_ref, |this| {
115107
this.on_host_pressed();
116108
});
117109

118110
self.join_button
119111
.signals()
120112
.pressed()
121113
.builder()
122-
.connect_other_mut(&gd_ref, |this: &mut Self| {
114+
.connect_other_mut(&gd_ref, |this| {
123115
this.on_join_pressed();
124116
});
125117
}
@@ -138,8 +130,7 @@ impl Lobby {
138130
}
139131
}
140132

141-
#[func]
142-
fn end_game(&mut self, with_error: String) {
133+
fn end_game(&mut self, with_error: &str) {
143134
if self.base().has_node("/root/Pong") {
144135
// Erase immediately, otherwise network might show
145136
// errors (this is why we connected deferred above).
@@ -152,7 +143,7 @@ impl Lobby {
152143
self.host_button.set_disabled(false);
153144
self.join_button.set_disabled(false);
154145

155-
self.set_status(&with_error, false);
146+
self.set_status(with_error, false);
156147
}
157148

158149
fn on_host_pressed(&mut self) {

net-pong/rust/src/paddle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl IArea2D for Paddle {
2727
.area_entered()
2828
.connect_self(|this: &mut Self, mut area: Gd<Area2D>| {
2929
if this.base().is_multiplayer_authority() {
30-
// Random for new direction generated checked each peer.
30+
// Set a random direction for the ball to go in
3131
let args = vslice![this.left, randf()];
3232
area.rpc("bounce", args);
3333
}

net-pong/rust/src/pong.rs

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ pub struct Pong {
1010
score_left: i32,
1111
score_right: i32,
1212
#[export]
13-
player1: Option<Gd<Area2D>>,
13+
player1: OnEditor<Gd<Area2D>>,
1414
#[export]
15-
player2: Option<Gd<Area2D>>,
15+
player2: OnEditor<Gd<Area2D>>,
1616
#[export]
17-
score_left_node: Option<Gd<Label>>,
17+
score_left_node: OnEditor<Gd<Label>>,
1818
#[export]
19-
score_right_node: Option<Gd<Label>>,
19+
score_right_node: OnEditor<Gd<Label>>,
2020
#[export]
21-
winner_left: Option<Gd<Label>>,
21+
winner_left: OnEditor<Gd<Label>>,
2222
#[export]
23-
winner_right: Option<Gd<Label>>,
23+
winner_right: OnEditor<Gd<Label>>,
2424
#[export]
25-
exit_game: Option<Gd<Button>>,
25+
exit_game: OnEditor<Gd<Button>>,
2626
#[export]
27-
ball: Option<Gd<Ball>>,
27+
ball: OnEditor<Gd<Ball>>,
2828
base: Base<Node2D>,
2929
}
3030

@@ -34,23 +34,15 @@ impl INode2D for Pong {
3434
if self.base().get_multiplayer().unwrap().is_server() {
3535
// For the server, give control of player 2 to the other peer.
3636
let authority = self.base().get_multiplayer().unwrap().get_peers()[0];
37-
self.player2
38-
.as_mut()
39-
.unwrap()
40-
.set_multiplayer_authority(authority);
37+
self.player2.set_multiplayer_authority(authority);
4138
} else {
4239
// For the client, give control of player 2 to itself.
4340
let authority = self.base().get_multiplayer().unwrap().get_unique_id();
44-
self.player2
45-
.as_mut()
46-
.unwrap()
47-
.set_multiplayer_authority(authority);
41+
self.player2.set_multiplayer_authority(authority);
4842
}
4943

5044
let gd_ref = self.to_gd();
5145
self.exit_game
52-
.as_mut()
53-
.unwrap()
5446
.signals()
5547
.pressed()
5648
.builder()
@@ -70,29 +62,25 @@ impl Pong {
7062
if add_to_left {
7163
self.score_left += 1;
7264
self.score_left_node
73-
.as_mut()
74-
.unwrap()
7565
.set_text(self.score_left.to_string().as_str());
7666
} else {
7767
self.score_right += 1;
7868
self.score_right_node
79-
.as_mut()
80-
.unwrap()
8169
.set_text(self.score_right.to_string().as_str());
8270
}
8371

8472
let mut game_ended = false;
8573
if self.score_left == SCORE_TO_WIN {
86-
self.winner_left.as_mut().unwrap().show();
74+
self.winner_left.show();
8775
game_ended = true;
8876
} else if self.score_right == SCORE_TO_WIN {
89-
self.winner_right.as_mut().unwrap().show();
77+
self.winner_right.show();
9078
game_ended = true;
9179
}
9280

9381
if game_ended {
94-
self.exit_game.as_mut().unwrap().show();
95-
self.ball.as_mut().unwrap().rpc("stop", &[]);
82+
self.exit_game.show();
83+
self.ball.rpc("stop", &[]);
9684
}
9785
}
9886

0 commit comments

Comments
 (0)