Skip to content

Commit 9e2184d

Browse files
committed
update to latest master and remove dead code
1 parent 4a57a79 commit 9e2184d

File tree

6 files changed

+13
-461
lines changed

6 files changed

+13
-461
lines changed

net-pong/rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2024"
55
license = "MIT"
66

77
[dependencies]
8-
godot = "0.3.1"
8+
godot = {git = "https://github.com/godot-rust/gdext.git", rev = "18d8a2200cb0bb85b587336ba853db3bb54c9ffa"}
99

1010
[lib]
1111
crate-type = ["cdylib"] # Compile this crate to a dynamic C library.

net-pong/rust/src/ball.rs

Lines changed: 6 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub struct Ball {
1515
use godot::classes::IArea2D;
1616

1717
use crate::pong::Pong;
18-
use crate::variant_array_to_vec;
1918

2019
#[godot_api]
2120
impl IArea2D for Ball {
@@ -61,11 +60,9 @@ impl IArea2D for Ball {
6160
// fast. Otherwise, the ball might be out in the other
6261
// player's screen but not this one.
6362
if ball_pos.x < 0.0 {
64-
//self.base().get_parent().update_score.rpc(false);
65-
let args = varray![false];
66-
let args = variant_array_to_vec(args);
67-
parent.rpc("update_score", args.as_slice());
68-
self.base_mut().rpc("reset_ball", args.as_slice());
63+
let args = vslice![false];
64+
parent.rpc("update_score", args);
65+
self.base_mut().rpc("reset_ball", args);
6966
}
7067
} else {
7168
// Only the puppet will decide when the ball is out in
@@ -74,64 +71,16 @@ impl IArea2D for Ball {
7471
// is going fast. Otherwise, the ball might be out in the
7572
// other player's screen but not this one.
7673
if ball_pos.x > screen_size.x {
77-
//self.base().get_parent().update_score.rpc(true);
78-
let args = varray![true];
79-
let args = variant_array_to_vec(args);
80-
parent.rpc("update_score", args.as_slice());
81-
self.base_mut().rpc("reset_ball", args.as_slice());
74+
let args = vslice![true];
75+
parent.rpc("update_score", args);
76+
self.base_mut().rpc("reset_ball", args);
8277
}
8378
}
84-
85-
/*
86-
_speed += delta
87-
# Ball will move normally for both players,
88-
# even if it's sightly out of sync between them,
89-
# so each player sees the motion as smooth and not jerky.
90-
if not stopped:
91-
translate(_speed * delta * direction)
92-
93-
# Check screen bounds to make ball bounce.
94-
var ball_pos := position
95-
if (ball_pos.y < 0 and direction.y < 0) or (ball_pos.y > _screen_size.y and direction.y > 0):
96-
direction.y = -direction.y
97-
98-
if is_multiplayer_authority():
99-
# Only the master will decide when the ball is out in
100-
# the left side (its own side). This makes the game
101-
# playable even if latency is high and ball is going
102-
# fast. Otherwise, the ball might be out in the other
103-
# player's screen but not this one.
104-
if ball_pos.x < 0:
105-
get_parent().update_score.rpc(false)
106-
_reset_ball.rpc(false)
107-
else:
108-
# Only the puppet will decide when the ball is out in
109-
# the right side, which is its own side. This makes
110-
# the game playable even if latency is high and ball
111-
# is going fast. Otherwise, the ball might be out in the
112-
# other player's screen but not this one.
113-
if ball_pos.x > _screen_size.x:
114-
get_parent().update_score.rpc(true)
115-
_reset_ball.rpc(true)
116-
*/
11779
}
11880
}
11981

12082
#[godot_api]
12183
impl Ball {
122-
/*
123-
@rpc("any_peer", "call_local")
124-
func bounce(left: bool, random: float) -> void:
125-
# Using sync because both players can make it bounce.
126-
if left:
127-
direction.x = abs(direction.x)
128-
else:
129-
direction.x = -abs(direction.x)
130-
131-
_speed *= 1.1
132-
direction.y = random * 2.0 - 1
133-
direction = direction.normalized()
134-
*/
13584
#[rpc(any_peer, call_local)]
13685
fn bounce(&mut self, is_left: bool, random: f32) {
13786
// Using sync because both players can make it bounce.
@@ -145,27 +94,11 @@ impl Ball {
14594
self.direction = self.direction.normalized();
14695
}
14796

148-
/*
149-
@rpc("any_peer", "call_local")
150-
func stop() -> void:
151-
stopped = true
152-
*/
15397
#[rpc(any_peer, call_local)]
15498
fn stop(&mut self) {
15599
self.stopped = true;
156100
}
157101

158-
/*
159-
@rpc("any_peer", "call_local")
160-
func _reset_ball(for_left: float) -> void:
161-
position = _screen_size / 2
162-
if for_left:
163-
direction = Vector2.LEFT
164-
else:
165-
direction = Vector2.RIGHT
166-
_speed = DEFAULT_SPEED
167-
*/
168-
169102
#[rpc(any_peer, call_local)]
170103
fn reset_ball(&mut self, for_left: bool) {
171104
let screen_center = self.base().get_viewport_rect().size / 2.0;

net-pong/rust/src/lib.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,6 @@ struct RustExtension;
55
#[gdextension]
66
unsafe impl ExtensionLibrary for RustExtension {}
77

8-
fn variant_array_to_vec(array: VariantArray) -> Vec<Variant> {
9-
let mut vec = Vec::new();
10-
for i in 0..array.len() {
11-
vec.push(
12-
array
13-
.get(i)
14-
.expect("Failed to get element from VariantArray"),
15-
);
16-
}
17-
vec
18-
}
19-
208
mod ball;
219
mod lobby;
2210
mod paddle;

net-pong/rust/src/lobby.rs

Lines changed: 2 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,13 @@
1-
use godot::classes::enet_connection::CompressionMode;
2-
use godot::classes::object::ConnectFlags;
3-
use godot::classes::project_settings;
4-
/*
5-
extends Panel
6-
7-
# Default game server port. Can be any number between 1024 and 49151.
8-
# Not present on the list of registered or common ports as of May 2024:
9-
# https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
10-
const DEFAULT_PORT = 8910
11-
12-
@onready var address: LineEdit = $Address
13-
@onready var host_button: Button = $HostButton
14-
@onready var join_button: Button = $JoinButton
15-
@onready var status_ok: Label = $StatusOk
16-
@onready var status_fail: Label = $StatusFail
17-
@onready var port_forward_label: Label = $PortForward
18-
@onready var find_public_ip_button: LinkButton = $FindPublicIP
19-
20-
var peer: ENetMultiplayerPeer
21-
22-
func _ready() -> void:
23-
# Connect all the callbacks related to networking.
24-
multiplayer.peer_connected.connect(_player_connected)
25-
multiplayer.peer_disconnected.connect(_player_disconnected)
26-
multiplayer.connected_to_server.connect(_connected_ok)
27-
multiplayer.connection_failed.connect(_connected_fail)
28-
multiplayer.server_disconnected.connect(_server_disconnected)
29-
30-
#region Network callbacks from SceneTree
31-
# Callback from SceneTree.
32-
func _player_connected(_id: int) -> void:
33-
# Someone connected, start the game!
34-
var pong: Node2D = load("res://pong.tscn").instantiate()
35-
# Connect deferred so we can safely erase it from the callback.
36-
pong.game_finished.connect(_end_game, CONNECT_DEFERRED)
37-
38-
get_tree().get_root().add_child(pong)
39-
hide()
40-
41-
42-
func _player_disconnected(_id: int) -> void:
43-
if multiplayer.is_server():
44-
_end_game("Client disconnected.")
45-
else:
46-
_end_game("Server disconnected.")
47-
48-
49-
# Callback from SceneTree, only for clients (not server).
50-
func _connected_ok() -> void:
51-
pass # This function is not needed for this project.
52-
53-
54-
# Callback from SceneTree, only for clients (not server).
55-
func _connected_fail() -> void:
56-
_set_status("Couldn't connect.", false)
57-
58-
multiplayer.set_multiplayer_peer(null) # Remove peer.
59-
host_button.set_disabled(false)
60-
join_button.set_disabled(false)
61-
62-
63-
func _server_disconnected() -> void:
64-
_end_game("Server disconnected.")
65-
#endregion
66-
67-
#region Game creation methods
68-
func _end_game(with_error: String = "") -> void:
69-
if has_node("/root/Pong"):
70-
# Erase immediately, otherwise network might show
71-
# errors (this is why we connected deferred above).
72-
get_node(^"/root/Pong").free()
73-
show()
74-
75-
multiplayer.set_multiplayer_peer(null) # Remove peer.
76-
host_button.set_disabled(false)
77-
join_button.set_disabled(false)
78-
79-
_set_status(with_error, false)
80-
81-
82-
func _set_status(text: String, is_ok: bool) -> void:
83-
# Simple way to show status.
84-
if is_ok:
85-
status_ok.set_text(text)
86-
status_fail.set_text("")
87-
else:
88-
status_ok.set_text("")
89-
status_fail.set_text(text)
90-
91-
92-
func _on_host_pressed() -> void:
93-
peer = ENetMultiplayerPeer.new()
94-
# Set a maximum of 1 peer, since Pong is a 2-player game.
95-
var err := peer.create_server(DEFAULT_PORT, 1)
96-
if err != OK:
97-
# Is another server running?
98-
_set_status("Can't host, address in use.",false)
99-
return
100-
peer.get_host().compress(ENetConnection.COMPRESS_RANGE_CODER)
101-
102-
multiplayer.set_multiplayer_peer(peer)
103-
host_button.set_disabled(true)
104-
join_button.set_disabled(true)
105-
_set_status("Waiting for player...", true)
106-
get_window().title = ProjectSettings.get_setting("application/config/name") + ": Server"
107-
108-
# Only show hosting instructions when relevant.
109-
port_forward_label.visible = true
110-
find_public_ip_button.visible = true
111-
112-
113-
func _on_join_pressed() -> void:
114-
var ip := address.get_text()
115-
if not ip.is_valid_ip_address():
116-
_set_status("IP address is invalid.", false)
117-
return
118-
119-
peer = ENetMultiplayerPeer.new()
120-
peer.create_client(ip, DEFAULT_PORT)
121-
peer.get_host().compress(ENetConnection.COMPRESS_RANGE_CODER)
122-
multiplayer.set_multiplayer_peer(peer)
123-
124-
_set_status("Connecting...", true)
125-
get_window().title = ProjectSettings.get_setting("application/config/name") + ": Client"
126-
#endregion
127-
128-
func _on_find_public_ip_pressed() -> void:
129-
OS.shell_open("https://icanhazip.com/")
130-
131-
*/
1321
use godot::classes::Button;
133-
use godot::classes::ENetConnection;
1342
use godot::classes::ENetMultiplayerPeer;
1353
use godot::classes::Label;
1364
use godot::classes::LineEdit;
1375
use godot::classes::LinkButton;
1386
use godot::classes::Os;
1397
use godot::classes::Panel;
1408
use godot::classes::ProjectSettings;
9+
use godot::classes::enet_connection::CompressionMode;
10+
use godot::classes::object::ConnectFlags;
14111
use godot::global::Error;
14212
use godot::prelude::*;
14313

@@ -288,16 +158,6 @@ impl IPanel for Lobby {
288158

289159
#[godot_api]
290160
impl Lobby {
291-
/*
292-
func _set_status(text: String, is_ok: bool) -> void:
293-
# Simple way to show status.
294-
if is_ok:
295-
status_ok.set_text(text)
296-
status_fail.set_text("")
297-
else:
298-
status_ok.set_text("")
299-
status_fail.set_text(text)
300-
*/
301161
fn _set_status(&mut self, text: String, is_ok: bool) {
302162
// Simple way to show status.
303163
if is_ok {
@@ -309,20 +169,6 @@ impl Lobby {
309169
}
310170
}
311171

312-
/*
313-
func _end_game(with_error: String = "") -> void:
314-
if has_node("/root/Pong"):
315-
# Erase immediately, otherwise network might show
316-
# errors (this is why we connected deferred above).
317-
get_node(^"/root/Pong").free()
318-
show()
319-
320-
multiplayer.set_multiplayer_peer(null) # Remove peer.
321-
host_button.set_disabled(false)
322-
join_button.set_disabled(false)
323-
324-
_set_status(with_error, false)
325-
*/
326172
#[func]
327173
fn _end_game(&mut self, with_error: String) {
328174
if self.base().has_node("/root/Pong") {
@@ -340,27 +186,6 @@ impl Lobby {
340186
self._set_status(with_error, false);
341187
}
342188

343-
/*
344-
func _on_host_pressed() -> void:
345-
peer = ENetMultiplayerPeer.new()
346-
# Set a maximum of 1 peer, since Pong is a 2-player game.
347-
var err := peer.create_server(DEFAULT_PORT, 1)
348-
if err != OK:
349-
# Is another server running?
350-
_set_status("Can't host, address in use.",false)
351-
return
352-
peer.get_host().compress(ENetConnection.COMPRESS_RANGE_CODER)
353-
354-
multiplayer.set_multiplayer_peer(peer)
355-
host_button.set_disabled(true)
356-
join_button.set_disabled(true)
357-
_set_status("Waiting for player...", true)
358-
get_window().title = ProjectSettings.get_setting("application/config/name") + ": Server"
359-
360-
# Only show hosting instructions when relevant.
361-
port_forward_label.visible = true
362-
find_public_ip_button.visible = true
363-
*/
364189
fn _on_host_pressed(&mut self) {
365190
let mut peer = ENetMultiplayerPeer::new_gd();
366191
self.peer = Some(peer.clone());
@@ -397,21 +222,6 @@ impl Lobby {
397222
.set_visible(true);
398223
}
399224

400-
/*
401-
func _on_join_pressed() -> void:
402-
var ip := address.get_text()
403-
if not ip.is_valid_ip_address():
404-
_set_status("IP address is invalid.", false)
405-
return
406-
407-
peer = ENetMultiplayerPeer.new()
408-
peer.create_client(ip, DEFAULT_PORT)
409-
peer.get_host().compress(ENetConnection.COMPRESS_RANGE_CODER)
410-
multiplayer.set_multiplayer_peer(peer)
411-
412-
_set_status("Connecting...", true)
413-
get_window().title = ProjectSettings.get_setting("application/config/name") + ": Client"
414-
*/
415225
fn _on_join_pressed(&mut self) {
416226
let ip = self.address.as_mut().unwrap().get_text();
417227
if !ip.is_valid_ip_address() {

0 commit comments

Comments
 (0)