From 3f8567f920b65206971f313557da99c0c49cd353 Mon Sep 17 00:00:00 2001 From: Duroxxigar Date: Sun, 18 Oct 2020 18:44:23 -0400 Subject: [PATCH] Created C# networked Pong --- 2d/pong/left_pallete.png | Bin 104 -> 0 bytes 2d/pong/paddle.png | Bin 0 -> 84 bytes .../pong/paddle.png.import | 6 +- 2d/pong/pong.tscn | 9 +- 2d/pong/right_pallete.png | Bin 104 -> 0 bytes 2d/pong/right_pallete.png.import | 34 ----- .../Pong Multiplayer with C#.csproj | 6 + .../Pong Multiplayer with C#.sln | 19 +++ mono/multiplayer_pong/README.md | 13 ++ mono/multiplayer_pong/ball.png | Bin 0 -> 115 bytes .../multiplayer_pong/ball.png.import | 6 +- mono/multiplayer_pong/ball.tscn | 16 ++ mono/multiplayer_pong/icon.png | Bin 0 -> 549 bytes .../icon.png.import} | 6 +- mono/multiplayer_pong/lobby.tscn | 102 +++++++++++++ mono/multiplayer_pong/logic/Ball.cs | 97 ++++++++++++ mono/multiplayer_pong/logic/Lobby.cs | 143 ++++++++++++++++++ mono/multiplayer_pong/logic/Paddle.cs | 70 +++++++++ mono/multiplayer_pong/logic/Pong.cs | 81 ++++++++++ mono/multiplayer_pong/paddle.png | Bin 0 -> 84 bytes mono/multiplayer_pong/paddle.png.import | 34 +++++ mono/multiplayer_pong/paddle.tscn | 31 ++++ mono/multiplayer_pong/pong.tscn | 92 +++++++++++ mono/multiplayer_pong/project.godot | 56 +++++++ mono/multiplayer_pong/separator.png | Bin 0 -> 108 bytes .../multiplayer_pong/separator.png.import | 6 +- mono/pong/left_pallete.png | Bin 104 -> 0 bytes mono/pong/paddle.png | Bin 0 -> 84 bytes mono/pong/paddle.png.import | 34 +++++ mono/pong/pong.tscn | 9 +- mono/pong/right_pallete.png | Bin 104 -> 0 bytes networking/multiplayer_pong/logic/ball.gd | 16 +- networking/multiplayer_pong/logic/lobby.gd | 5 +- networking/multiplayer_pong/logic/paddle.gd | 24 +-- networking/multiplayer_pong/logic/pong.gd | 7 +- networking/multiplayer_pong/pong.tscn | 9 +- networking/multiplayer_pong/project.godot | 1 - visual_script/pong/left_pallete.png | Bin 104 -> 0 bytes visual_script/pong/paddle.png | Bin 0 -> 84 bytes visual_script/pong/paddle.png.import | 34 +++++ visual_script/pong/pong.tscn | 9 +- visual_script/pong/right_pallete.png | Bin 104 -> 0 bytes visual_script/pong/right_pallete.png.import | 34 ----- 43 files changed, 886 insertions(+), 123 deletions(-) delete mode 100644 2d/pong/left_pallete.png create mode 100644 2d/pong/paddle.png rename mono/pong/left_pallete.png.import => 2d/pong/paddle.png.import (70%) delete mode 100644 2d/pong/right_pallete.png delete mode 100644 2d/pong/right_pallete.png.import create mode 100644 mono/multiplayer_pong/Pong Multiplayer with C#.csproj create mode 100644 mono/multiplayer_pong/Pong Multiplayer with C#.sln create mode 100644 mono/multiplayer_pong/README.md create mode 100644 mono/multiplayer_pong/ball.png rename 2d/pong/left_pallete.png.import => mono/multiplayer_pong/ball.png.import (70%) create mode 100644 mono/multiplayer_pong/ball.tscn create mode 100644 mono/multiplayer_pong/icon.png rename mono/{pong/right_pallete.png.import => multiplayer_pong/icon.png.import} (69%) create mode 100644 mono/multiplayer_pong/lobby.tscn create mode 100644 mono/multiplayer_pong/logic/Ball.cs create mode 100644 mono/multiplayer_pong/logic/Lobby.cs create mode 100644 mono/multiplayer_pong/logic/Paddle.cs create mode 100644 mono/multiplayer_pong/logic/Pong.cs create mode 100644 mono/multiplayer_pong/paddle.png create mode 100644 mono/multiplayer_pong/paddle.png.import create mode 100644 mono/multiplayer_pong/paddle.tscn create mode 100644 mono/multiplayer_pong/pong.tscn create mode 100644 mono/multiplayer_pong/project.godot create mode 100644 mono/multiplayer_pong/separator.png rename visual_script/pong/left_pallete.png.import => mono/multiplayer_pong/separator.png.import (70%) delete mode 100644 mono/pong/left_pallete.png create mode 100644 mono/pong/paddle.png create mode 100644 mono/pong/paddle.png.import delete mode 100644 mono/pong/right_pallete.png delete mode 100644 visual_script/pong/left_pallete.png create mode 100644 visual_script/pong/paddle.png create mode 100644 visual_script/pong/paddle.png.import delete mode 100644 visual_script/pong/right_pallete.png delete mode 100644 visual_script/pong/right_pallete.png.import diff --git a/2d/pong/left_pallete.png b/2d/pong/left_pallete.png deleted file mode 100644 index a44c5bba5ee7a51dd29fca77cb9a4b1e9a8bbb38..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 104 zcmeAS@N?(olHy`uVBq!ia0vp^96+qV$P6S!?N(0)QfvV}A+A80;s1YW$JGWv24hK( yUoeBivm0qZj)13&V@SoV(e}0SxQqrC-jv*Ddk{KHR2QVZhbNz8= iKJ1nd(opb*nL+W%J%gF!$r~6C{eR*(A&z0zTbZkGOU*X|u_g diff --git a/2d/pong/right_pallete.png.import b/2d/pong/right_pallete.png.import deleted file mode 100644 index 4b442ffb..00000000 --- a/2d/pong/right_pallete.png.import +++ /dev/null @@ -1,34 +0,0 @@ -[remap] - -importer="texture" -type="StreamTexture" -path="res://.import/right_pallete.png-fc6e4a6a7c8197834656482b94708e47.stex" -metadata={ -"vram_texture": false -} - -[deps] - -source_file="res://right_pallete.png" -dest_files=[ "res://.import/right_pallete.png-fc6e4a6a7c8197834656482b94708e47.stex" ] - -[params] - -compress/mode=0 -compress/lossy_quality=0.7 -compress/hdr_mode=0 -compress/bptc_ldr=0 -compress/normal_map=0 -flags/repeat=0 -flags/filter=false -flags/mipmaps=false -flags/anisotropic=false -flags/srgb=2 -process/fix_alpha_border=true -process/premult_alpha=false -process/HDR_as_SRGB=false -process/invert_color=false -stream=false -size_limit=0 -detect_3d=true -svg/scale=1.0 diff --git a/mono/multiplayer_pong/Pong Multiplayer with C#.csproj b/mono/multiplayer_pong/Pong Multiplayer with C#.csproj new file mode 100644 index 00000000..ffaa5571 --- /dev/null +++ b/mono/multiplayer_pong/Pong Multiplayer with C#.csproj @@ -0,0 +1,6 @@ + + + net472 + PongMultiplayer + + \ 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 new file mode 100644 index 00000000..158b1317 --- /dev/null +++ b/mono/multiplayer_pong/Pong Multiplayer with C#.sln @@ -0,0 +1,19 @@ +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}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + ExportDebug|Any CPU = ExportDebug|Any CPU + 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 + EndGlobalSection +EndGlobal diff --git a/mono/multiplayer_pong/README.md b/mono/multiplayer_pong/README.md new file mode 100644 index 00000000..38f21dab --- /dev/null +++ b/mono/multiplayer_pong/README.md @@ -0,0 +1,13 @@ +# Pong Multiplayer with C# + +A multiplayer demo of Pong. One player presses "host". The other presses "join". This only works locally. + +Language: [C#](https://docs.godotengine.org/en/latest/getting_started/scripting/c_sharp/index.html) + +Renderer: GLES 2 + +Note: There is a GDScript version available [here](https://github.com/godotengine/godot-demo-projects/tree/master/networking/multiplayer_pong). + +## Screenshots + +![Screenshot](../../2d/pong/screenshots/pong.png) diff --git a/mono/multiplayer_pong/ball.png b/mono/multiplayer_pong/ball.png new file mode 100644 index 0000000000000000000000000000000000000000..465d3521aa8d143ea46b540f2902a13a507b5b00 GIT binary patch literal 115 zcmeAS@N?(olHy`uVBq!ia0vp^93afZ3?z3ZhDiV^o&cW^S0Mc#2pIlx)!^6a-r8E{6*u%sB|NjL7 z0y`TU-pkAS_xBYC2R$4d>D}Go(9j+W3pyGa`}z6Kw6r4)4KWoJ{{a8Pq@*DX4D#ve zyqlXE2?-1Y1ON*Q05vrLkdWij(z}|P4+I3-#>T;+p)eE_78VwNe}CA*!UzZmEG#UO zlatJ`u{s$U+sDV!tD${J3JN?M8};w+)xEtU3=B0F z7sjZlA`K1r^Ygx)o$x+BY}k?A0002|Nkl;u~3+>pe#scPIo4mySjBx&wbx>&b`Or<5FBd2ViIi5EQ^vppI{j z-GCG%)EUe;2XFjmgbQ%eki@+dkYe{UeGma6Km-T_U;$1*KT?|Q+xx+KXwaPlTMEds z`kO||C>ydD7Jg^r!c<@%1s2Yla&V9WlA)JzCIu9k`T&&HU`+ss`%zF _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); + } + } + } + } + + [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); + } + + _speed *= 1.1f; + _direction.y = random * 2.0f - 1; + _direction = _direction.Normalized(); + } + + [Sync] + private void Stop() + { + _stopped = true; + } + + [Sync] + 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 new file mode 100644 index 00000000..c770eb1f --- /dev/null +++ b/mono/multiplayer_pong/logic/Lobby.cs @@ -0,0 +1,143 @@ +using Godot; +using System; +using Godot.Collections; + +public 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