Trying to make a server

This commit is contained in:
2022-10-23 20:58:01 +02:00
parent 9d04246d75
commit 7a60703eb8
9 changed files with 125 additions and 16 deletions
+8 -6
View File
@@ -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)
+28 -5
View File
@@ -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<Assignation>(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!");
}
}
}
}
+2 -2
View File
@@ -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();
}
}
}
+4
View File
@@ -25,6 +25,10 @@
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
<PackageReference Include="MonoGame.UI.Forms" Version="1.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\shared\shared.csproj" />
</ItemGroup>
<Target Name="RestoreDotnetTools" BeforeTargets="Restore">
<Message Text="Restoring dotnet tools" Importance="High" />