feat: Test Network

This commit is contained in:
Syntriax 2024-07-16 22:50:16 +03:00
parent 91d301677f
commit c28568d0cb
8 changed files with 56 additions and 110 deletions

View File

@ -1,16 +1,19 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Pong.Behaviours;
using Apos.Shapes;
using Pong.Behaviours;
using Syntriax.Engine.Network;
using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Physics2D;
using Syntriax.Engine.Physics2D.Primitives;
using Syntriax.Engine.Physics2D.Abstract;
using Syntriax.Engine.Physics2D.Primitives;
namespace Pong;
@ -64,58 +67,81 @@ public class GamePong : Game
////////////////////////////////////////////////////////////////////////////////////
IGameObject gameObjectCamera = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Camera"); ;
string[] commandLineArguments = Environment.GetCommandLineArgs();
if (commandLineArguments.Length != 1)
{
if (commandLineArguments[1].Equals("server", StringComparison.OrdinalIgnoreCase))
{
gameManager.InstantiateGameObject().BehaviourController.AddBehaviour<NetworkServer>().Start(8888, 2);
Window.Title = "Pong - Server";
}
else
{
Window.Title = $"Pong - Client -> {commandLineArguments[1]}";
gameManager.InstantiateGameObject().BehaviourController.AddBehaviour<NetworkClient>().Connect(commandLineArguments[1], 8888);
}
}
else
{
gameManager.InstantiateGameObject().BehaviourController.AddBehaviour<NetworkClient>().Connect("127.0.0.1", 8888);
Window.Title = $"Pong - Client -> 127.0.0.1";
}
////////////////////////////////////////////////////////////////////////////////////
IGameObject gameObjectCamera = gameManager.InstantiateGameObject().SetGameObject("Camera"); ;
gameObjectCamera.BehaviourController.AddBehaviour<CameraController>();
cameraBehaviour = gameObjectCamera.BehaviourController.AddBehaviour<MonoGameCamera2DBehaviour>(graphics);
////////////////////////////////////////////////////////////////////////////////////
IGameObject gameObjectPongManager = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Pong Game Manager");
IGameObject gameObjectPongManager = gameManager.InstantiateGameObject().SetGameObject("Pong Game Manager");
pongManager = gameObjectPongManager.BehaviourController.AddBehaviour<PongManagerBehaviour>(5);
pongManager.Id = "pongManager";
////////////////////////////////////////////////////////////////////////////////////
IGameObject gameObjectBall = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Ball");
IGameObject gameObjectBall = gameManager.InstantiateGameObject().SetGameObject("Ball");
gameObjectBall.Transform.SetTransform(position: new Vector2D(0, 0f), scale: new Vector2D(10f, 10f));
gameObjectBall.BehaviourController.AddBehaviour<CircleBehaviour>(new Circle(Vector2D.Zero, 1f));
gameObjectBall.BehaviourController.AddBehaviour<BallBehaviour>();
gameObjectBall.BehaviourController.AddBehaviour<BallBehaviour>().Id = "ball";
gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>();
////////////////////////////////////////////////////////////////////////////////////
IGameObject gameObjectLeftPaddle = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Left Paddle");
IGameObject gameObjectLeftPaddle = gameManager.InstantiateGameObject().SetGameObject("Left Paddle");
gameObjectLeftPaddle.Transform.SetTransform(position: new Vector2D(-468f, 0f), scale: new Vector2D(15f, 60f));
gameObjectLeftPaddle.BehaviourController.AddBehaviour<PaddleBehaviour>(Keys.W, Keys.S, 228f, -228f, 400f);
gameObjectLeftPaddle.BehaviourController.AddBehaviour<PaddleBehaviour>(Keys.W, Keys.S, 228f, -228f, 400f).Id = "leftPaddle";
gameObjectLeftPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
gameObjectLeftPaddle.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
IGameObject gameObjectRightPaddle = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Right Paddle");
IGameObject gameObjectRightPaddle = gameManager.InstantiateGameObject().SetGameObject("Right Paddle");
gameObjectRightPaddle.Transform.SetTransform(position: new Vector2D(468f, 0f), scale: new Vector2D(15f, 60f));
gameObjectRightPaddle.BehaviourController.AddBehaviour<PaddleBehaviour>(Keys.Up, Keys.Down, 228f, -228f, 400f);
gameObjectRightPaddle.BehaviourController.AddBehaviour<PaddleBehaviour>(Keys.Up, Keys.Down, 228f, -228f, 400f).Id = "rightPaddle";
gameObjectRightPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
gameObjectRightPaddle.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
////////////////////////////////////////////////////////////////////////////////////
IGameObject gameObjectWallTop = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Top");
IGameObject gameObjectWallTop = gameManager.InstantiateGameObject().SetGameObject("Wall Top");
gameObjectWallTop.Transform.SetTransform(position: new Vector2D(0f, 308f), scale: new Vector2D(552f, 20f));
gameObjectWallTop.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
gameObjectWallTop.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
IGameObject gameObjectWallBottom = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Bottom");
IGameObject gameObjectWallBottom = gameManager.InstantiateGameObject().SetGameObject("Wall Bottom");
gameObjectWallBottom.Transform.SetTransform(position: new Vector2D(0f, -308f), scale: new Vector2D(552f, 20f));
gameObjectWallBottom.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
gameObjectWallBottom.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
IGameObject gameObjectWallRight = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Right");
IGameObject gameObjectWallRight = gameManager.InstantiateGameObject().SetGameObject("Wall Right");
gameObjectWallRight.Transform.SetTransform(position: new Vector2D(532f, 0f), scale: new Vector2D(20f, 328f));
gameObjectWallRight.BehaviourController.AddBehaviour<WallScoreBehaviour>((Action)pongManager.ScoreToLeft);
gameObjectWallRight.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
gameObjectWallRight.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
IGameObject gameObjectWallLeft = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Wall Left");
IGameObject gameObjectWallLeft = gameManager.InstantiateGameObject().SetGameObject("Wall Left");
gameObjectWallLeft.Transform.SetTransform(position: new Vector2D(-532f, 0f), scale: new Vector2D(20f, 328f));
gameObjectWallLeft.BehaviourController.AddBehaviour<WallScoreBehaviour>((Action)pongManager.ScoreToRight);
gameObjectWallLeft.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
@ -123,11 +149,11 @@ public class GamePong : Game
////////////////////////////////////////////////////////////////////////////////////
IGameObject gameObjectLeftScoreText = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Score Left");
IGameObject gameObjectLeftScoreText = gameManager.InstantiateGameObject().SetGameObject("Score Left");
gameObjectLeftScoreText.Transform.SetTransform(position: new Vector2D(-250f, 250f), scale: Vector2D.One * .25f);
gameObjectLeftScoreText.BehaviourController.AddBehaviour<TextScoreBehaviour>(true, spriteFont);
IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Score Right");
IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject().SetGameObject("Score Right");
gameObjectRightScoreText.Transform.SetTransform(position: new Vector2D(250f, 250f), scale: Vector2D.One * .25f);
gameObjectRightScoreText.BehaviourController.AddBehaviour<TextScoreBehaviour>(false, spriteFont);
}

View File

@ -1,5 +1,4 @@
using LiteNetLib;
using LiteNetLib.Utils;
namespace Syntriax.Engine.Network.Abstract;
@ -10,10 +9,4 @@ public interface INetworkCommunicator
void PollEvents();
void Stop();
void RegisterEntityPacketListener<T>(INetworkEntity networkEntity, EntityPacketReceivedDelegate onPacketReceived) where T : INetSerializable;
void UnregisterEntityPacketListener<T>(INetworkEntity networkEntity, EntityPacketReceivedDelegate onPacketReceived) where T : INetSerializable;
void SendEntityPacket<T>(INetworkEntity networkEntity, T packet, params NetPeer[] netPeer) where T : INetSerializable;
delegate void EntityPacketReceivedDelegate(INetworkEntity networkEntity, object packet, NetPeer netPeer);
}

View File

@ -1,5 +1,3 @@
using LiteNetLib.Utils;
namespace Syntriax.Engine.Network.Abstract;
public interface INetworkEntity
@ -9,9 +7,4 @@ public interface INetworkEntity
uint NetworkId { get; set; }
delegate void OnNetworkIdChangedDelegate(INetworkEntity sender, uint previousId);
delegate void PacketReceivedDelegate(INetworkEntity entity, object packet);
void RegisterPacketListener<T>(PacketReceivedDelegate onPacketReceived) where T : INetSerializable;
void UnregisterPacketListener<T>(PacketReceivedDelegate onPacketReceived) where T : INetSerializable;
void SendPacket<T>(T packet) where T : INetSerializable;
}

View File

@ -1,21 +1,7 @@
using LiteNetLib.Utils;
namespace Syntriax.Engine.Network.Abstract;
public class NetworkPacket<T>()
: INetSerializable
where T : INetSerializable
public class NetworkPacket<T>
{
public uint NetworkId = 0;
public T Data = default!;
public void Deserialize(NetDataReader reader)
{
NetworkId = reader.GetUInt();
}
public void Serialize(NetDataWriter writer)
{
writer.Put(NetworkId);
}
public uint NetworkId { get; set; } = 0;
public T Data { get; set; } = default!;
}

View File

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using LiteNetLib;
using LiteNetLib.Utils;
@ -15,7 +14,6 @@ public abstract class NetworkBase : BehaviourOverride, INetworkCommunicator
private readonly NetPacketProcessor netPacketProcessor = new();
private readonly Dictionary<uint, INetworkEntity> networkEntities = [];
private readonly Dictionary<Type, List<INetworkCommunicator.EntityPacketReceivedDelegate>> callbacks = [];
private BehaviourCollector<INetworkEntity> networkEntityCollector = null!;
@ -30,6 +28,12 @@ public abstract class NetworkBase : BehaviourOverride, INetworkCommunicator
Manager = new NetManager(Listener);
Listener.NetworkReceiveEvent += NetworkReceiveEvent;
netPacketProcessor.SubscribeReusable<NetworkPacket<int>, NetPeer>(OnPacketArrived);
}
private void OnPacketArrived(NetworkPacket<int> packet, NetPeer peer)
{
Debug.WriteLine($"Packet Arrived for {packet.NetworkId}: {packet.Data}");
}
protected override void OnInitialize()
@ -40,6 +44,7 @@ public abstract class NetworkBase : BehaviourOverride, INetworkCommunicator
networkEntityCollector.OnCollected += OnNetworkEntityCollected;
networkEntityCollector.OnRemoved += OnNetworkEntityRemoved;
}
protected override void OnFinalize()
{
networkEntityCollector.OnCollected -= OnNetworkEntityCollected;
@ -62,48 +67,4 @@ public abstract class NetworkBase : BehaviourOverride, INetworkCommunicator
public void Stop() => Manager.Stop();
protected override void OnUpdate() => PollEvents();
public void RegisterEntityPacketListener<T>(INetworkEntity networkEntity, INetworkCommunicator.EntityPacketReceivedDelegate onPacketReceived) where T : INetSerializable
{
if (!callbacks.TryGetValue(typeof(T), out var list))
{
list = [];
callbacks.Add(typeof(T), list);
netPacketProcessor.SubscribeReusable<NetworkPacket<T>, NetPeer>(OnPacketReceived);
}
if (list.Contains(onPacketReceived))
return;
list.Add(onPacketReceived);
}
public void UnregisterEntityPacketListener<T>(INetworkEntity networkEntity, INetworkCommunicator.EntityPacketReceivedDelegate onPacketReceived) where T : INetSerializable
{
if (!callbacks.TryGetValue(typeof(T), out var list))
return;
list.Remove(onPacketReceived);
}
public void SendEntityPacket<T>(INetworkEntity networkEntity, T packet, params NetPeer[] netPeers) where T : INetSerializable
{
NetworkPacket<T> networkPacket = new() { NetworkId = networkEntity.NetworkId, Data = packet };
NetDataWriter netDataWriter = new();
netPacketProcessor.Write(netDataWriter, networkPacket);
foreach (var netPeer in netPeers)
netPeer.Send(netDataWriter, DeliveryMethod.ReliableOrdered);
}
private void OnPacketReceived<T>(NetworkPacket<T> packet, NetPeer peer) where T : INetSerializable
{
Debug.WriteLine($"Packet Received: {packet.NetworkId} - {typeof(T)}");
if (!callbacks.TryGetValue(typeof(T), out var list))
return;
INetworkEntity networkEntity = networkEntities[packet.NetworkId];
foreach (INetworkCommunicator.EntityPacketReceivedDelegate callback in list)
callback.Invoke(networkEntity, packet, peer);
}
}

View File

@ -48,21 +48,4 @@ public abstract class NetworkBehaviour : BehaviourOverride, INetworkBehaviour
IsServer = true;
}
public void RegisterPacketListener<T>(INetworkEntity.PacketReceivedDelegate onPacketReceived) where T : INetSerializable
private void OnEntityPacketReceived(INetworkEntity networkEntity, object packet, NetPeer netPeer)
{
throw new NotImplementedException();
}
public void UnregisterPacketListener<T>(INetworkEntity.PacketReceivedDelegate onPacketReceived) where T : INetSerializable
{
throw new NotImplementedException();
}
public void SendPacket<T>(T packet) where T : INetSerializable
{
throw new NotImplementedException();
}
}

View File

@ -1,3 +1,5 @@
using System.Diagnostics;
using System.Threading.Tasks;
using Syntriax.Engine.Network.Abstract;
namespace Syntriax.Engine.Network;

View File

@ -1,3 +1,5 @@
using System.Diagnostics;
using System.Threading.Tasks;
using Syntriax.Engine.Network.Abstract;
namespace Syntriax.Engine.Network;