diff --git a/Pong.sln b/Pong.sln new file mode 100644 index 0000000..3d15127 --- /dev/null +++ b/Pong.sln @@ -0,0 +1,36 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32929.385 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "server", "server\server.csproj", "{AB011134-CF45-4362-B3AC-5F11D9C39B80}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "client", "client\client.csproj", "{5AE7A447-37B1-45D1-91D3-664049C1645F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "shared", "shared\shared.csproj", "{EB920FE1-25C7-4AB2-8DC2-A712904E9ABA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AB011134-CF45-4362-B3AC-5F11D9C39B80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AB011134-CF45-4362-B3AC-5F11D9C39B80}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AB011134-CF45-4362-B3AC-5F11D9C39B80}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AB011134-CF45-4362-B3AC-5F11D9C39B80}.Release|Any CPU.Build.0 = Release|Any CPU + {5AE7A447-37B1-45D1-91D3-664049C1645F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5AE7A447-37B1-45D1-91D3-664049C1645F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5AE7A447-37B1-45D1-91D3-664049C1645F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5AE7A447-37B1-45D1-91D3-664049C1645F}.Release|Any CPU.Build.0 = Release|Any CPU + {EB920FE1-25C7-4AB2-8DC2-A712904E9ABA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EB920FE1-25C7-4AB2-8DC2-A712904E9ABA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EB920FE1-25C7-4AB2-8DC2-A712904E9ABA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EB920FE1-25C7-4AB2-8DC2-A712904E9ABA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {431509A6-DE8B-46C1-AC0D-5FF9344BD6FB} + EndGlobalSection +EndGlobal diff --git a/client/Ball.cs b/client/Ball.cs index 7742225..581579e 100644 --- a/client/Ball.cs +++ b/client/Ball.cs @@ -61,8 +61,10 @@ namespace client // Start | Stop ball moving public Ball StartMoving() { - MoveX = random.Next((int)-Speed, (int)Speed); - MoveY = random.Next((int)-Speed, (int)Speed); + /*MoveX = random.Next((int)-Speed, (int)Speed); + MoveY = 1000f;*/ + MoveX = Speed; + MoveY = Speed; CanMove = true; return this; } @@ -71,7 +73,7 @@ namespace client { if (CanMove) { - // X + /*// X if (Position.X < 0) { MoveX -= Speed; @@ -88,10 +90,10 @@ namespace client else { MoveY += Speed; - } + }*/ - Position.X += MoveX; - Position.Y += MoveY; + Position.X += MoveX + Speed; + Position.Y += MoveY + Speed; // Collisions if (Position.Y < Radius * 2 && MoveY < 0) diff --git a/client/Game1.cs b/client/Game1.cs index c8084ae..d083710 100644 --- a/client/Game1.cs +++ b/client/Game1.cs @@ -1,9 +1,10 @@ -using LiteNetLib; +using System; +using System.Collections.Generic; +using LiteNetLib; using LiteNetLib.Utils; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; -using System; -using System.Collections.Generic; +using shared; namespace client { @@ -13,6 +14,7 @@ namespace client EventBasedNetListener listener = new(); NetManager client; NetPeer server; + NetPacketProcessor processor = new(); // Game instances private Player pl1; @@ -57,6 +59,7 @@ namespace client // TODO: Replace by a error message :) Exit(); } + processor.SubscribeReusable(AssignationHandler); // Window Window.Title = "Pong"; @@ -70,7 +73,7 @@ namespace client 15, 100, 100f - ).SetControllable(true); + ); pl2 = new Player( new Vector2(_graphics.PreferredBackBufferWidth - 30, _graphics.PreferredBackBufferHeight / 2 - 50), @@ -83,7 +86,7 @@ namespace client ball = new Ball( new Vector2(_graphics.PreferredBackBufferWidth / 2 - 5, _graphics.PreferredBackBufferHeight / 2 - 5), 10, - 0.2f + 1f ); // KILLZONE @@ -143,5 +146,25 @@ namespace client _spriteBatch.End(); base.Draw(gameTime); } + + private void AssignationHandler(Assignation assignation) + { + if (assignation != null) + { + if (assignation.controller == 0) { + pl1.SetControllable(true); + pl2.SetControllable(false); + } + else + { + pl1.SetControllable(false); + pl2.SetControllable(true); + } + } + else + { + Console.Error.WriteLine("Packet malformed!"); + } + } } } \ No newline at end of file diff --git a/client/KillZone.cs b/client/KillZone.cs index 807047f..9be798d 100644 --- a/client/KillZone.cs +++ b/client/KillZone.cs @@ -50,14 +50,14 @@ namespace client { if (ball.Position.X < Position.X + Width && ball.CanMove) { - ball.StopMoving(); + //ball.StopMoving(); } } else { if (ball.Position.X > Position.X && ball.CanMove) { - ball.StopMoving(); + //ball.StopMoving(); } } } diff --git a/client/client.csproj b/client/client.csproj index 591848b..bfdf6e4 100644 --- a/client/client.csproj +++ b/client/client.csproj @@ -25,6 +25,10 @@ + + + + diff --git a/server/Program.cs b/server/Program.cs index 386dbe0..25e9545 100644 --- a/server/Program.cs +++ b/server/Program.cs @@ -1,5 +1,6 @@ using LiteNetLib; using LiteNetLib.Utils; +using shared; namespace Server { @@ -7,10 +8,15 @@ namespace Server { static EventBasedNetListener listener = new(); static NetManager? server; + static NetPacketProcessor processor = new(); + static NetSerializer serializer = new(); static string? port; static string? pswd; + static string gameState = "Idle"; // Idle || Playing || Paused || Ended + static List players; + public static void Main(string[] args) { server = new NetManager(listener); @@ -28,6 +34,8 @@ namespace Server return; } + serializer.Register(); + listener.ConnectionRequestEvent += request => { @@ -38,19 +46,33 @@ namespace Server listener.PeerConnectedEvent += peer => { Console.WriteLine("Connection accepted for: {0}", peer.EndPoint); - /* NetDataWriter writer = new(); - writer.Put("Hello client!"); - peer.Send(writer, DeliveryMethod.ReliableOrdered); */ + if (peer != null) + { + players.Add(peer); + } }; listener.PeerDisconnectedEvent += (peer, infos) => { Console.WriteLine($"{peer.EndPoint} has left.\nError code: {infos.SocketErrorCode}\nReason: {infos.Reason}"); + players.Remove(peer); }; while (!Console.KeyAvailable) { server.PollEvents(); + if (server.GetPeersCount(ConnectionState.Connected) >= 2) + { + // Differents game states + if (gameState == "Idle") + { + Assignation packet = new() { controller = 2, ballX = 0, ballY = 0 }; + processor.Send(players[0], packet, DeliveryMethod.ReliableOrdered); + processor.Send(players[1], packet, DeliveryMethod.ReliableOrdered); + Console.WriteLine("Assignation sended"); + gameState = "Playing"; + } + } Thread.Sleep(15); } diff --git a/server/server.csproj b/server/server.csproj index b25b491..0bfa0d5 100644 --- a/server/server.csproj +++ b/server/server.csproj @@ -12,4 +12,8 @@ + + + + diff --git a/shared/Messages.cs b/shared/Messages.cs new file mode 100644 index 0000000..63c34e8 --- /dev/null +++ b/shared/Messages.cs @@ -0,0 +1,9 @@ +namespace shared +{ + public class Assignation + { + public int controller { get; set; } // 0 || 1 + public float ballX { get; set; } + public float ballY { get; set; } + } +} \ No newline at end of file diff --git a/shared/shared.csproj b/shared/shared.csproj new file mode 100644 index 0000000..132c02c --- /dev/null +++ b/shared/shared.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + +