From 278dab56901aef29a6127615771cf36ce40b8574 Mon Sep 17 00:00:00 2001 From: Allyson Date: Sun, 14 Apr 2024 16:45:02 -0400 Subject: [PATCH 1/5] Updated Multiplayer Pong C# to Godot 4.2 --- .../.idea/.gitignore | 13 + .../.idea/.name | 1 + .../.idea/encodings.xml | 4 + .../.idea/indexLayout.xml | 8 + .../.idea/vcs.xml | 6 + .../Pong Multiplayer with C#.csproj | 9 +- .../Pong Multiplayer with C#.sln | 16 +- mono/multiplayer_pong/lobby.tscn | 33 +- .../.idea/.idea.logic.dir/.idea/.gitignore | 13 + .../.idea/.idea.logic.dir/.idea/encodings.xml | 4 + .../.idea.logic.dir/.idea/indexLayout.xml | 8 + .../logic/.idea/.idea.logic.dir/.idea/vcs.xml | 6 + mono/multiplayer_pong/logic/Ball.cs | 164 +++++----- mono/multiplayer_pong/logic/Lobby.cs | 284 +++++++++--------- mono/multiplayer_pong/logic/Paddle.cs | 18 +- mono/multiplayer_pong/logic/Pong.cs | 14 +- mono/multiplayer_pong/project.godot | 6 +- 17 files changed, 343 insertions(+), 264 deletions(-) create mode 100644 mono/multiplayer_pong/.idea/.idea.Pong Multiplayer with C#/.idea/.gitignore create mode 100644 mono/multiplayer_pong/.idea/.idea.Pong Multiplayer with C#/.idea/.name create mode 100644 mono/multiplayer_pong/.idea/.idea.Pong Multiplayer with C#/.idea/encodings.xml create mode 100644 mono/multiplayer_pong/.idea/.idea.Pong Multiplayer with C#/.idea/indexLayout.xml create mode 100644 mono/multiplayer_pong/.idea/.idea.Pong Multiplayer with C#/.idea/vcs.xml create mode 100644 mono/multiplayer_pong/logic/.idea/.idea.logic.dir/.idea/.gitignore create mode 100644 mono/multiplayer_pong/logic/.idea/.idea.logic.dir/.idea/encodings.xml create mode 100644 mono/multiplayer_pong/logic/.idea/.idea.logic.dir/.idea/indexLayout.xml create mode 100644 mono/multiplayer_pong/logic/.idea/.idea.logic.dir/.idea/vcs.xml diff --git a/mono/multiplayer_pong/.idea/.idea.Pong Multiplayer with C#/.idea/.gitignore b/mono/multiplayer_pong/.idea/.idea.Pong Multiplayer with C#/.idea/.gitignore new file mode 100644 index 0000000000..c0fe738ade --- /dev/null +++ b/mono/multiplayer_pong/.idea/.idea.Pong Multiplayer with C#/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/modules.xml +/contentModel.xml +/.idea.Pong Multiplayer with C#.iml +/projectSettingsUpdater.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/mono/multiplayer_pong/.idea/.idea.Pong Multiplayer with C#/.idea/.name b/mono/multiplayer_pong/.idea/.idea.Pong Multiplayer with C#/.idea/.name new file mode 100644 index 0000000000..895a6c3149 --- /dev/null +++ b/mono/multiplayer_pong/.idea/.idea.Pong Multiplayer with C#/.idea/.name @@ -0,0 +1 @@ +Pong Multiplayer with C# \ No newline at end of file diff --git a/mono/multiplayer_pong/.idea/.idea.Pong Multiplayer with C#/.idea/encodings.xml b/mono/multiplayer_pong/.idea/.idea.Pong Multiplayer with C#/.idea/encodings.xml new file mode 100644 index 0000000000..df87cf951f --- /dev/null +++ b/mono/multiplayer_pong/.idea/.idea.Pong Multiplayer with C#/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/mono/multiplayer_pong/.idea/.idea.Pong Multiplayer with C#/.idea/indexLayout.xml b/mono/multiplayer_pong/.idea/.idea.Pong Multiplayer with C#/.idea/indexLayout.xml new file mode 100644 index 0000000000..7b08163ceb --- /dev/null +++ b/mono/multiplayer_pong/.idea/.idea.Pong Multiplayer with C#/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/mono/multiplayer_pong/.idea/.idea.Pong Multiplayer with C#/.idea/vcs.xml b/mono/multiplayer_pong/.idea/.idea.Pong Multiplayer with C#/.idea/vcs.xml new file mode 100644 index 0000000000..b2bdec2d71 --- /dev/null +++ b/mono/multiplayer_pong/.idea/.idea.Pong Multiplayer with C#/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/mono/multiplayer_pong/Pong Multiplayer with C#.csproj b/mono/multiplayer_pong/Pong Multiplayer with C#.csproj index 18dad044ec..cf8558f4d8 100644 --- a/mono/multiplayer_pong/Pong Multiplayer with C#.csproj +++ b/mono/multiplayer_pong/Pong Multiplayer with C#.csproj @@ -1,6 +1,9 @@ - + - net472 - PongMultiplayer + net6.0 + net7.0 + net8.0 + true + PongMultiplayerwithC \ No newline at end of file diff --git a/mono/multiplayer_pong/Pong Multiplayer with C#.sln b/mono/multiplayer_pong/Pong Multiplayer with C#.sln index 158b131783..f689da3a66 100644 --- a/mono/multiplayer_pong/Pong Multiplayer with C#.sln +++ b/mono/multiplayer_pong/Pong Multiplayer with C#.sln @@ -1,6 +1,6 @@ -Microsoft Visual Studio Solution File, Format Version 12.00 +Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 2012 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pong Multiplayer with C#", "Pong Multiplayer with C#.csproj", "{4BB6C2D0-FC11-466E-8C73-8DAD689F135A}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pong Multiplayer with C#", "Pong Multiplayer with C#.csproj", "{58FECEC2-0618-4042-81F2-4EDD7982C229}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -9,11 +9,11 @@ Global ExportRelease|Any CPU = ExportRelease|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {4BB6C2D0-FC11-466E-8C73-8DAD689F135A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {4BB6C2D0-FC11-466E-8C73-8DAD689F135A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {4BB6C2D0-FC11-466E-8C73-8DAD689F135A}.ExportDebug|Any CPU.ActiveCfg = ExportDebug|Any CPU - {4BB6C2D0-FC11-466E-8C73-8DAD689F135A}.ExportDebug|Any CPU.Build.0 = ExportDebug|Any CPU - {4BB6C2D0-FC11-466E-8C73-8DAD689F135A}.ExportRelease|Any CPU.ActiveCfg = ExportRelease|Any CPU - {4BB6C2D0-FC11-466E-8C73-8DAD689F135A}.ExportRelease|Any CPU.Build.0 = ExportRelease|Any CPU + {58FECEC2-0618-4042-81F2-4EDD7982C229}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {58FECEC2-0618-4042-81F2-4EDD7982C229}.Debug|Any CPU.Build.0 = Debug|Any CPU + {58FECEC2-0618-4042-81F2-4EDD7982C229}.ExportDebug|Any CPU.ActiveCfg = ExportDebug|Any CPU + {58FECEC2-0618-4042-81F2-4EDD7982C229}.ExportDebug|Any CPU.Build.0 = ExportDebug|Any CPU + {58FECEC2-0618-4042-81F2-4EDD7982C229}.ExportRelease|Any CPU.ActiveCfg = ExportRelease|Any CPU + {58FECEC2-0618-4042-81F2-4EDD7982C229}.ExportRelease|Any CPU.Build.0 = ExportRelease|Any CPU EndGlobalSection EndGlobal diff --git a/mono/multiplayer_pong/lobby.tscn b/mono/multiplayer_pong/lobby.tscn index e9e0aa74cc..75c438af5b 100644 --- a/mono/multiplayer_pong/lobby.tscn +++ b/mono/multiplayer_pong/lobby.tscn @@ -1,8 +1,10 @@ -[gd_scene load_steps=2 format=2] +[gd_scene load_steps=2 format=3 uid="uid://bxj0km4t28u17"] -[ext_resource path="res://logic/Lobby.cs" type="Script" id=1] +[ext_resource type="Script" path="res://logic/Lobby.cs" id="1"] [node name="Lobby" type="Control"] +layout_mode = 3 +anchors_preset = 8 anchor_left = 0.5 anchor_top = 0.5 anchor_right = 0.5 @@ -11,13 +13,13 @@ offset_left = -320.0 offset_top = -200.0 offset_right = 320.0 offset_bottom = 200.0 +grow_horizontal = 2 +grow_vertical = 2 size_flags_horizontal = 2 size_flags_vertical = 2 -__meta__ = { -"_edit_use_anchors_": false -} [node name="Title" type="Label" parent="."] +layout_mode = 0 offset_left = 210.0 offset_top = 40.0 offset_right = 430.0 @@ -25,22 +27,19 @@ offset_bottom = 80.0 size_flags_horizontal = 2 size_flags_vertical = 0 text = "Multiplayer Pong" -align = 1 -valign = 1 [node name="LobbyPanel" type="Panel" parent="."] +layout_mode = 0 offset_left = 210.0 offset_top = 160.0 offset_right = 430.0 offset_bottom = 270.0 size_flags_horizontal = 2 size_flags_vertical = 2 -script = ExtResource( 1 ) -__meta__ = { -"_edit_use_anchors_": false -} +script = ExtResource("1") [node name="AddressLabel" type="Label" parent="LobbyPanel"] +layout_mode = 0 offset_left = 10.0 offset_top = 10.0 offset_right = 62.0 @@ -50,6 +49,7 @@ size_flags_vertical = 0 text = "Address" [node name="Address" type="LineEdit" parent="LobbyPanel"] +layout_mode = 0 offset_left = 10.0 offset_top = 30.0 offset_right = 210.0 @@ -59,6 +59,7 @@ size_flags_vertical = 2 text = "127.0.0.1" [node name="HostButton" type="Button" parent="LobbyPanel"] +layout_mode = 0 offset_left = 10.0 offset_top = 60.0 offset_right = 90.0 @@ -68,6 +69,7 @@ size_flags_vertical = 2 text = "Host" [node name="JoinButton" type="Button" parent="LobbyPanel"] +layout_mode = 0 offset_left = 130.0 offset_top = 60.0 offset_right = 210.0 @@ -75,29 +77,24 @@ offset_bottom = 80.0 size_flags_horizontal = 2 size_flags_vertical = 2 text = "Join" -__meta__ = { -"_edit_use_anchors_": false -} [node name="StatusOk" type="Label" parent="LobbyPanel"] +layout_mode = 0 offset_left = 10.0 offset_top = 90.0 offset_right = 210.0 offset_bottom = 104.0 size_flags_horizontal = 2 size_flags_vertical = 0 -custom_colors/font_color = Color(0, 1, 0.015625, 1) -align = 1 [node name="StatusFail" type="Label" parent="LobbyPanel"] +layout_mode = 0 offset_left = 10.0 offset_top = 90.0 offset_right = 210.0 offset_bottom = 104.0 size_flags_horizontal = 2 size_flags_vertical = 0 -custom_colors/font_color = Color(1, 0, 0, 1) -align = 1 [connection signal="pressed" from="LobbyPanel/HostButton" to="LobbyPanel" method="OnHostPressed"] [connection signal="pressed" from="LobbyPanel/JoinButton" to="LobbyPanel" method="OnJoinPressed"] diff --git a/mono/multiplayer_pong/logic/.idea/.idea.logic.dir/.idea/.gitignore b/mono/multiplayer_pong/logic/.idea/.idea.logic.dir/.idea/.gitignore new file mode 100644 index 0000000000..d19d5b411e --- /dev/null +++ b/mono/multiplayer_pong/logic/.idea/.idea.logic.dir/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/projectSettingsUpdater.xml +/modules.xml +/contentModel.xml +/.idea.logic.iml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/mono/multiplayer_pong/logic/.idea/.idea.logic.dir/.idea/encodings.xml b/mono/multiplayer_pong/logic/.idea/.idea.logic.dir/.idea/encodings.xml new file mode 100644 index 0000000000..df87cf951f --- /dev/null +++ b/mono/multiplayer_pong/logic/.idea/.idea.logic.dir/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/mono/multiplayer_pong/logic/.idea/.idea.logic.dir/.idea/indexLayout.xml b/mono/multiplayer_pong/logic/.idea/.idea.logic.dir/.idea/indexLayout.xml new file mode 100644 index 0000000000..7b08163ceb --- /dev/null +++ b/mono/multiplayer_pong/logic/.idea/.idea.logic.dir/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/mono/multiplayer_pong/logic/.idea/.idea.logic.dir/.idea/vcs.xml b/mono/multiplayer_pong/logic/.idea/.idea.logic.dir/.idea/vcs.xml new file mode 100644 index 0000000000..c2365ab11f --- /dev/null +++ b/mono/multiplayer_pong/logic/.idea/.idea.logic.dir/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/mono/multiplayer_pong/logic/Ball.cs b/mono/multiplayer_pong/logic/Ball.cs index ee9e779629..afdbe0b4e9 100644 --- a/mono/multiplayer_pong/logic/Ball.cs +++ b/mono/multiplayer_pong/logic/Ball.cs @@ -3,95 +3,95 @@ public partial class Ball : Area2D { - private const int DefaultSpeed = 100; + private const int DefaultSpeed = 100; - private Vector2 _direction = Vector2.Left; - private bool _stopped = false; - private float _speed = DefaultSpeed; - private Vector2 _screenSize; + private Vector2 _direction = Vector2.Left; + private bool _stopped = false; + private double _speed = DefaultSpeed; + private Vector2 _screenSize; - // Called when the node enters the scene tree for the first time. - public override void _Ready() - { - _screenSize = GetViewportRect().Size; - } + // Called when the node enters the scene tree for the first time. + public override void _Ready() + { + _screenSize = GetViewportRect().Size; + } - // Called every frame. 'delta' is the elapsed time since the previous frame. - public override void _Process(float delta) - { - _speed += delta; - // Ball will move normally for both players, - // even if it's slightly out of sync between them, - // so each player sees the motion as smooth and not jerky. - if (!_stopped) - { - Translate(_speed * delta * _direction); - } + // Called every frame. 'delta' is the elapsed time since the previous frame. + public override void _Process(double delta) + { + _speed += delta; + // Ball will move normally for both players, + // even if it's slightly out of sync between them, + // so each player sees the motion as smooth and not jerky. + if (!_stopped) + { + Translate((float)(_speed * delta) * _direction); + } - // Check screen bounds to make ball bounce. - var ballPosition = Position; - if ((ballPosition.y < 0 && _direction.y < 0) || (ballPosition.y > _screenSize.y && _direction.y > 0)) - { - _direction.y = -_direction.y; - } + // Check screen bounds to make ball bounce. + var ballPosition = Position; + if ((ballPosition.Y < 0 && _direction.Y < 0) || (ballPosition.Y > _screenSize.Y && _direction.Y > 0)) + { + _direction.Y = -_direction.Y; + } - if (IsNetworkMaster()) - { - // Only the master will decide when the ball is out in - // the left side (it's own side). This makes the game - // playable even if latency is high and ball is going - // fast. Otherwise ball might be out in the other - // player's screen but not this one. - if (ballPosition.x < 0) - { - GetParent().Rpc("UpdateScore", false); - Rpc("ResetBall", false); - } - else - { - // Only the puppet will decide when the ball is out in - // the right side, which is it's own side. This makes - // the game playable even if latency is high and ball - // is going fast. Otherwise ball might be out in the - // other player's screen but not this one. - if (ballPosition.x > _screenSize.x) - { - GetParent().Rpc("UpdateScore", true); - Rpc("ResetBall", true); - } - } - } - } + if (IsMultiplayerAuthority()) + { + // Only the server (Multiplayer Authority) will decide + // when the ball is out in the left side (it's own side). + // This makes the game playable even if latency is high and + // ball is going fast. Otherwise ball might be out in the other + // player's screen but not this one. + if (ballPosition.X < 0) + { + GetParent().Rpc("UpdateScore", false); + Rpc("ResetBall", false); + } + else + { + // Only the peer will decide when the ball is out in + // the right side, which is it's own side. This makes + // the game playable even if latency is high and ball + // is going fast. Otherwise ball might be out in the + // other player's screen but not this one. + if (ballPosition.X > _screenSize.X) + { + GetParent().Rpc("UpdateScore", true); + Rpc("ResetBall", true); + } + } + } + } - [Sync] - private void Bounce(bool left, float random) - { - // Using sync because both players can make it bounce. - if (left) - { - _direction.x = Mathf.Abs(_direction.x); - } - else - { - _direction.x = -Mathf.Abs(_direction.x); - } + [Rpc(mode: MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)] + private void Bounce(bool left, float random) + { + // Using sync because both players can make it bounce. + if (left) + { + _direction.X = Mathf.Abs(_direction.X); + } + else + { + _direction.X = -Mathf.Abs(_direction.X); + } - _speed *= 1.1f; - _direction.y = random * 2.0f - 1; - _direction = _direction.Normalized(); - } + _speed *= 1.1f; + _direction.Y = random * 2.0f - 1; + _direction = _direction.Normalized(); + } - [Sync] - private void Stop() - { - _stopped = true; - } + [Rpc(mode: MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)] + private void Stop() + { + _stopped = true; + } - [Sync] - private void ResetBall(bool forLeft) - { - Position = _screenSize / 2; - _direction = forLeft ? Vector2.Left : Vector2.Right; - _speed = DefaultSpeed; - } + [Rpc(mode: MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)] + private void ResetBall(bool forLeft) + { + Position = _screenSize / 2; + _direction = forLeft ? Vector2.Left : Vector2.Right; + _speed = DefaultSpeed; + } } diff --git a/mono/multiplayer_pong/logic/Lobby.cs b/mono/multiplayer_pong/logic/Lobby.cs index 78ebb8c247..c7d5c52a77 100644 --- a/mono/multiplayer_pong/logic/Lobby.cs +++ b/mono/multiplayer_pong/logic/Lobby.cs @@ -4,140 +4,152 @@ public partial class Lobby : Control { - private const int DefaultPort = 8910; // An arbitrary number. - private const int MaxNumberOfPeers = 1; // How many people we want to have in a game - - private LineEdit _address; - private Button _hostButton; - private Button _joinButton; - private Label _statusOk; - private Label _statusFail; - private NetworkedMultiplayerENet _peer; - - public override void _Ready() - { - // Get nodes - the generic is a class, argument is node path. - _address = GetNode("Address"); - _hostButton = GetNode