Compare commits
No commits in common. "fe5a08840c6ff747fe23907e03bc9ed97e8e3006" and "de267a9d0f358240e7aa434c5d0ee268462603cc" have entirely different histories.
fe5a08840c
...
de267a9d0f
2
Engine
2
Engine
@ -1 +1 @@
|
|||||||
Subproject commit dbb263ebed24a1658d5d18d27182c8749cc8ee06
|
Subproject commit 5d897f2f56c711629f8cbf915d0ed112cf21680f
|
@ -1,6 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using LiteNetLib;
|
|
||||||
using Pong.Network;
|
|
||||||
using Syntriax.Engine.Core;
|
using Syntriax.Engine.Core;
|
||||||
using Syntriax.Engine.Physics2D;
|
using Syntriax.Engine.Physics2D;
|
||||||
using Syntriax.Engine.Physics2D.Abstract;
|
using Syntriax.Engine.Physics2D.Abstract;
|
||||||
@ -15,7 +14,6 @@ public class BallBehaviour : BehaviourOverride
|
|||||||
|
|
||||||
private readonly Random random = new();
|
private readonly Random random = new();
|
||||||
private IRigidBody2D rigidBody = null!;
|
private IRigidBody2D rigidBody = null!;
|
||||||
private INetworkCommunicator communicator = null!;
|
|
||||||
|
|
||||||
protected override void OnFirstActiveFrame()
|
protected override void OnFirstActiveFrame()
|
||||||
{
|
{
|
||||||
@ -23,14 +21,11 @@ public class BallBehaviour : BehaviourOverride
|
|||||||
throw new Exception($"{nameof(IRigidBody2D)} is missing on {GameObject.Name}.");
|
throw new Exception($"{nameof(IRigidBody2D)} is missing on {GameObject.Name}.");
|
||||||
if (!BehaviourController.TryGetBehaviour(out ICollider2D? foundCollider))
|
if (!BehaviourController.TryGetBehaviour(out ICollider2D? foundCollider))
|
||||||
throw new Exception($"{nameof(ICollider2D)} is missing on {GameObject.Name}.");
|
throw new Exception($"{nameof(ICollider2D)} is missing on {GameObject.Name}.");
|
||||||
if (!GameObject.GameManager.TryFindBehaviour(out INetworkCommunicator? foundCommunicator))
|
|
||||||
throw new Exception($"{nameof(INetworkCommunicator)} is missing on GameManager.");
|
|
||||||
|
|
||||||
foundCollider.OnCollisionDetected += OnCollisionDetected;
|
foundCollider.OnCollisionDetected += OnCollisionDetected;
|
||||||
|
|
||||||
rigidBody = foundRigidBody;
|
rigidBody = foundRigidBody;
|
||||||
communicator = foundCommunicator;
|
|
||||||
communicator.RegisterEntityListener(this, OnDataReceived);
|
|
||||||
if (GameObject.GameManager.TryFindBehaviour(out PongManagerBehaviour? pongManager))
|
if (GameObject.GameManager.TryFindBehaviour(out PongManagerBehaviour? pongManager))
|
||||||
{
|
{
|
||||||
pongManager.OnReset += ResetBall;
|
pongManager.OnReset += ResetBall;
|
||||||
@ -39,12 +34,6 @@ public class BallBehaviour : BehaviourOverride
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDataReceived(NetPacketReader reader)
|
|
||||||
{
|
|
||||||
rigidBody.Transform.Position = reader.GetVector2D();
|
|
||||||
rigidBody.Velocity = reader.GetVector2D();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void DisableBall(PongManagerBehaviour pongManager)
|
private void DisableBall(PongManagerBehaviour pongManager)
|
||||||
{
|
{
|
||||||
BehaviourController.GameObject.Transform.Position = Vector2D.Zero;
|
BehaviourController.GameObject.Transform.Position = Vector2D.Zero;
|
||||||
@ -56,8 +45,6 @@ public class BallBehaviour : BehaviourOverride
|
|||||||
StateEnable.Enabled = true;
|
StateEnable.Enabled = true;
|
||||||
BehaviourController.GameObject.Transform.Position = Vector2D.Zero;
|
BehaviourController.GameObject.Transform.Position = Vector2D.Zero;
|
||||||
rigidBody.Velocity = GetRandomDirection() * Speed;
|
rigidBody.Velocity = GetRandomDirection() * Speed;
|
||||||
|
|
||||||
SendBallData();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Vector2D GetRandomDirection()
|
private Vector2D GetRandomDirection()
|
||||||
@ -83,19 +70,6 @@ public class BallBehaviour : BehaviourOverride
|
|||||||
rigidBody.Velocity = information.Left.Transform.Position.FromTo(information.Right.Transform.Position).Normalized * rigidBody.Velocity.Magnitude;
|
rigidBody.Velocity = information.Left.Transform.Position.FromTo(information.Right.Transform.Position).Normalized * rigidBody.Velocity.Magnitude;
|
||||||
else
|
else
|
||||||
rigidBody.Velocity = rigidBody.Velocity.Reflect(information.Normal);
|
rigidBody.Velocity = rigidBody.Velocity.Reflect(information.Normal);
|
||||||
|
|
||||||
SendBallData();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SendBallData()
|
|
||||||
{
|
|
||||||
if (communicator is not INetworkServer server)
|
|
||||||
return;
|
|
||||||
|
|
||||||
LiteNetLib.Utils.NetDataWriter dataWriter = communicator.GetEntityWriter(this);
|
|
||||||
dataWriter.Put(rigidBody.Transform.Position);
|
|
||||||
dataWriter.Put(rigidBody.Velocity);
|
|
||||||
server.Manager.SendToAll(dataWriter, LiteNetLib.DeliveryMethod.ReliableOrdered);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public BallBehaviour() { }
|
public BallBehaviour() { }
|
||||||
|
@ -2,11 +2,6 @@ using System;
|
|||||||
|
|
||||||
using Microsoft.Xna.Framework.Input;
|
using Microsoft.Xna.Framework.Input;
|
||||||
|
|
||||||
using LiteNetLib;
|
|
||||||
using LiteNetLib.Utils;
|
|
||||||
|
|
||||||
using Pong.Network;
|
|
||||||
|
|
||||||
using Syntriax.Engine.Core;
|
using Syntriax.Engine.Core;
|
||||||
using Syntriax.Engine.Input;
|
using Syntriax.Engine.Input;
|
||||||
|
|
||||||
@ -24,7 +19,6 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
|
|||||||
private bool isDownPressed = false;
|
private bool isDownPressed = false;
|
||||||
|
|
||||||
private IButtonInputs<Keys> inputs = null!;
|
private IButtonInputs<Keys> inputs = null!;
|
||||||
private INetworkCommunicator communicator = null!;
|
|
||||||
|
|
||||||
protected override void OnUpdate()
|
protected override void OnUpdate()
|
||||||
{
|
{
|
||||||
@ -32,43 +26,23 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
if (isUpPressed)
|
if (isUpPressed)
|
||||||
MovePaddle(Vector2D.Up * (float)Time.Elapsed.TotalSeconds * Speed);
|
GameObject.Transform.Position = GameObject.Transform.Position + Vector2D.Up * (float)Time.Elapsed.TotalSeconds * Speed;
|
||||||
else if (isDownPressed)
|
else if (isDownPressed)
|
||||||
MovePaddle(-Vector2D.Up * (float)Time.Elapsed.TotalSeconds * Speed);
|
GameObject.Transform.Position = GameObject.Transform.Position + -Vector2D.Up * (float)Time.Elapsed.TotalSeconds * Speed;
|
||||||
|
|
||||||
GameObject.Transform.Position = new Vector2D(GameObject.Transform.Position.X, MathF.Max(MathF.Min(GameObject.Transform.Position.Y, High), Low));
|
GameObject.Transform.Position = new Vector2D(GameObject.Transform.Position.X, MathF.Max(MathF.Min(GameObject.Transform.Position.Y, High), Low));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void MovePaddle(Vector2D vectorToAdd)
|
|
||||||
{
|
|
||||||
GameObject.Transform.Position += vectorToAdd;
|
|
||||||
|
|
||||||
NetDataWriter dataWriter = communicator.GetEntityWriter(this);
|
|
||||||
dataWriter.Put(Transform.Position);
|
|
||||||
communicator.Manager.SendToAll(dataWriter, LiteNetLib.DeliveryMethod.Unreliable);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnFirstActiveFrame()
|
protected override void OnFirstActiveFrame()
|
||||||
{
|
{
|
||||||
if (!BehaviourController.TryGetBehaviour<IButtonInputs<Keys>>(out var behaviourResult))
|
if (!BehaviourController.TryGetBehaviour<IButtonInputs<Keys>>(out var behaviourResult))
|
||||||
inputs = behaviourResult ?? BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
|
inputs = behaviourResult ?? BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
|
||||||
|
|
||||||
if (!GameObject.GameManager.TryFindBehaviour(out INetworkCommunicator? foundCommunicator))
|
|
||||||
throw new Exception($"{nameof(INetworkCommunicator)} is missing on GameManager.");
|
|
||||||
|
|
||||||
inputs.RegisterOnPress(Up, OnUpPressed);
|
inputs.RegisterOnPress(Up, OnUpPressed);
|
||||||
inputs.RegisterOnRelease(Up, OnUpReleased);
|
inputs.RegisterOnRelease(Up, OnUpReleased);
|
||||||
|
|
||||||
inputs.RegisterOnPress(Down, OnDownPressed);
|
inputs.RegisterOnPress(Down, OnDownPressed);
|
||||||
inputs.RegisterOnRelease(Down, OnDownReleased);
|
inputs.RegisterOnRelease(Down, OnDownReleased);
|
||||||
|
|
||||||
communicator = foundCommunicator;
|
|
||||||
communicator.RegisterEntityListener(this, OnDataReceived);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnDataReceived(NetPacketReader reader)
|
|
||||||
{
|
|
||||||
Transform.Position = reader.GetVector2D();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnFinalize()
|
protected override void OnFinalize()
|
||||||
|
@ -2,10 +2,6 @@ using System;
|
|||||||
|
|
||||||
using Microsoft.Xna.Framework.Input;
|
using Microsoft.Xna.Framework.Input;
|
||||||
|
|
||||||
using LiteNetLib;
|
|
||||||
|
|
||||||
using Pong.Network;
|
|
||||||
|
|
||||||
using Syntriax.Engine.Core;
|
using Syntriax.Engine.Core;
|
||||||
|
|
||||||
namespace Pong.Behaviours;
|
namespace Pong.Behaviours;
|
||||||
@ -16,8 +12,6 @@ public class PongManagerBehaviour : BehaviourOverride
|
|||||||
public Action<PongManagerBehaviour>? OnFinished { get; set; } = null;
|
public Action<PongManagerBehaviour>? OnFinished { get; set; } = null;
|
||||||
public Action<PongManagerBehaviour>? OnScored { get; set; } = null;
|
public Action<PongManagerBehaviour>? OnScored { get; set; } = null;
|
||||||
|
|
||||||
private INetworkCommunicator communicator = null!;
|
|
||||||
|
|
||||||
public int ScoreLeft { get; private set; } = 0;
|
public int ScoreLeft { get; private set; } = 0;
|
||||||
public int ScoreRight { get; private set; } = 0;
|
public int ScoreRight { get; private set; } = 0;
|
||||||
public int ScoreSum => ScoreLeft + ScoreRight;
|
public int ScoreSum => ScoreLeft + ScoreRight;
|
||||||
@ -34,31 +28,15 @@ public class PongManagerBehaviour : BehaviourOverride
|
|||||||
if (!BehaviourController.TryGetBehaviour(out buttonInputs))
|
if (!BehaviourController.TryGetBehaviour(out buttonInputs))
|
||||||
buttonInputs = BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
|
buttonInputs = BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
|
||||||
|
|
||||||
if (!GameObject.GameManager.TryFindBehaviour(out INetworkCommunicator? foundCommunicator))
|
|
||||||
throw new Exception($"{nameof(INetworkCommunicator)} is missing on GameManager.");
|
|
||||||
|
|
||||||
buttonInputs.RegisterOnRelease(Keys.Space, (_, _1) => Reset());
|
buttonInputs.RegisterOnRelease(Keys.Space, (_, _1) => Reset());
|
||||||
|
|
||||||
communicator = foundCommunicator;
|
|
||||||
communicator.RegisterEntityListener(this, OnMessageReceived);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnMessageReceived(NetPacketReader reader)
|
|
||||||
{
|
|
||||||
ScoreLeft = reader.GetInt();
|
|
||||||
ScoreRight = reader.GetInt();
|
|
||||||
OnScored?.Invoke(this);
|
|
||||||
|
|
||||||
CheckFinish();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ScoreToLeft()
|
public void ScoreToLeft()
|
||||||
{
|
{
|
||||||
ScoreLeft++;
|
ScoreLeft++;
|
||||||
OnScored?.Invoke(this);
|
OnScored?.Invoke(this);
|
||||||
|
|
||||||
SendData();
|
|
||||||
|
|
||||||
CheckFinish();
|
CheckFinish();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,8 +45,6 @@ public class PongManagerBehaviour : BehaviourOverride
|
|||||||
ScoreRight++;
|
ScoreRight++;
|
||||||
OnScored?.Invoke(this);
|
OnScored?.Invoke(this);
|
||||||
|
|
||||||
SendData();
|
|
||||||
|
|
||||||
CheckFinish();
|
CheckFinish();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,8 +52,6 @@ public class PongManagerBehaviour : BehaviourOverride
|
|||||||
{
|
{
|
||||||
ScoreLeft = ScoreRight = 0;
|
ScoreLeft = ScoreRight = 0;
|
||||||
OnReset?.Invoke(this);
|
OnReset?.Invoke(this);
|
||||||
|
|
||||||
SendData();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CheckFinish()
|
private void CheckFinish()
|
||||||
@ -87,15 +61,4 @@ public class PongManagerBehaviour : BehaviourOverride
|
|||||||
if (ScoreLeft > halfwayScore || ScoreRight > halfwayScore)
|
if (ScoreLeft > halfwayScore || ScoreRight > halfwayScore)
|
||||||
OnFinished?.Invoke(this);
|
OnFinished?.Invoke(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SendData()
|
|
||||||
{
|
|
||||||
if (communicator is not INetworkServer server)
|
|
||||||
return;
|
|
||||||
|
|
||||||
LiteNetLib.Utils.NetDataWriter dataWriter = communicator.GetEntityWriter(this);
|
|
||||||
dataWriter.Put(ScoreLeft);
|
|
||||||
dataWriter.Put(ScoreRight);
|
|
||||||
server.Manager.SendToAll(dataWriter, LiteNetLib.DeliveryMethod.ReliableOrdered);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,6 @@ public class TextScoreBehaviour : TextBehaviour
|
|||||||
if (!GameObject.GameManager.TryFindBehaviour(out pongManager))
|
if (!GameObject.GameManager.TryFindBehaviour(out pongManager))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
pongManager.OnFinished += UpdateScores;
|
|
||||||
pongManager.OnScored += UpdateScores;
|
pongManager.OnScored += UpdateScores;
|
||||||
pongManager.OnReset += UpdateScores;
|
pongManager.OnReset += UpdateScores;
|
||||||
|
|
||||||
|
@ -1,19 +1,16 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using Microsoft.Xna.Framework.Input;
|
using Microsoft.Xna.Framework.Input;
|
||||||
|
|
||||||
using Apos.Shapes;
|
|
||||||
|
|
||||||
using Pong.Behaviours;
|
using Pong.Behaviours;
|
||||||
using Pong.Network;
|
using Apos.Shapes;
|
||||||
|
|
||||||
using Syntriax.Engine.Core;
|
using Syntriax.Engine.Core;
|
||||||
using Syntriax.Engine.Core.Abstract;
|
using Syntriax.Engine.Core.Abstract;
|
||||||
using Syntriax.Engine.Physics2D;
|
using Syntriax.Engine.Physics2D;
|
||||||
using Syntriax.Engine.Physics2D.Abstract;
|
|
||||||
using Syntriax.Engine.Physics2D.Primitives;
|
using Syntriax.Engine.Physics2D.Primitives;
|
||||||
|
using Syntriax.Engine.Physics2D.Abstract;
|
||||||
|
|
||||||
namespace Pong;
|
namespace Pong;
|
||||||
|
|
||||||
@ -82,7 +79,7 @@ public class GamePong : Game
|
|||||||
gameObjectBall.Transform.SetTransform(position: new Vector2D(0, 0f), scale: new Vector2D(10f, 10f));
|
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<CircleBehaviour>(new Circle(Vector2D.Zero, 1f));
|
||||||
gameObjectBall.BehaviourController.AddBehaviour<BallBehaviour>().Id = "ball";
|
gameObjectBall.BehaviourController.AddBehaviour<BallBehaviour>();
|
||||||
gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>();
|
gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>();
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////
|
||||||
@ -90,13 +87,13 @@ public class GamePong : Game
|
|||||||
IGameObject gameObjectLeftPaddle = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Left Paddle");
|
IGameObject gameObjectLeftPaddle = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Left Paddle");
|
||||||
gameObjectLeftPaddle.Transform.SetTransform(position: new Vector2D(-468f, 0f), scale: new Vector2D(15f, 60f));
|
gameObjectLeftPaddle.Transform.SetTransform(position: new Vector2D(-468f, 0f), scale: new Vector2D(15f, 60f));
|
||||||
|
|
||||||
gameObjectLeftPaddle.BehaviourController.AddBehaviour<PaddleBehaviour>(Keys.W, Keys.S, 228f, -228f, 400f).Id = "leftPaddle"; ;
|
gameObjectLeftPaddle.BehaviourController.AddBehaviour<PaddleBehaviour>(Keys.W, Keys.S, 228f, -228f, 400f);
|
||||||
gameObjectLeftPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
gameObjectLeftPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
||||||
gameObjectLeftPaddle.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
|
gameObjectLeftPaddle.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
|
||||||
|
|
||||||
IGameObject gameObjectRightPaddle = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Right Paddle");
|
IGameObject gameObjectRightPaddle = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Right Paddle");
|
||||||
gameObjectRightPaddle.Transform.SetTransform(position: new Vector2D(468f, 0f), scale: new Vector2D(15f, 60f));
|
gameObjectRightPaddle.Transform.SetTransform(position: new Vector2D(468f, 0f), scale: new Vector2D(15f, 60f));
|
||||||
gameObjectRightPaddle.BehaviourController.AddBehaviour<PaddleBehaviour>(Keys.Up, Keys.Down, 228f, -228f, 400f).Id = "rightPaddle"; ;
|
gameObjectRightPaddle.BehaviourController.AddBehaviour<PaddleBehaviour>(Keys.Up, Keys.Down, 228f, -228f, 400f);
|
||||||
gameObjectRightPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
gameObjectRightPaddle.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Box);
|
||||||
gameObjectRightPaddle.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
|
gameObjectRightPaddle.BehaviourController.AddBehaviour<RigidBody2D>().IsStatic = true;
|
||||||
|
|
||||||
@ -133,18 +130,6 @@ public class GamePong : Game
|
|||||||
IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Score Right");
|
IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject<GameObject>().SetGameObject("Score Right");
|
||||||
gameObjectRightScoreText.Transform.SetTransform(position: new Vector2D(250f, 250f), scale: Vector2D.One * .25f);
|
gameObjectRightScoreText.Transform.SetTransform(position: new Vector2D(250f, 250f), scale: Vector2D.One * .25f);
|
||||||
gameObjectRightScoreText.BehaviourController.AddBehaviour<TextScoreBehaviour>(false, spriteFont);
|
gameObjectRightScoreText.BehaviourController.AddBehaviour<TextScoreBehaviour>(false, spriteFont);
|
||||||
|
|
||||||
string[] commandLineArguments = Environment.GetCommandLineArgs();
|
|
||||||
if (commandLineArguments.Length != 1)
|
|
||||||
{
|
|
||||||
if (commandLineArguments[1].Equals("server", StringComparison.OrdinalIgnoreCase))
|
|
||||||
pongManager.BehaviourController.AddBehaviour<NetworkServer>().Start(8888, 2);
|
|
||||||
else
|
|
||||||
pongManager.BehaviourController.AddBehaviour<NetworkClient>().Connect(commandLineArguments[1], 8888);
|
|
||||||
|
|
||||||
}
|
|
||||||
else
|
|
||||||
pongManager.BehaviourController.AddBehaviour<NetworkClient>().Connect("127.0.0.1", 8888);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Update(GameTime gameTime)
|
protected override void Update(GameTime gameTime)
|
||||||
|
@ -1,6 +0,0 @@
|
|||||||
namespace Pong.Network;
|
|
||||||
|
|
||||||
public interface INetworkClient : INetworkCommunicator
|
|
||||||
{
|
|
||||||
void Connect(string address, int port, string? password = null);
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
using System;
|
|
||||||
using LiteNetLib;
|
|
||||||
using LiteNetLib.Utils;
|
|
||||||
using Syntriax.Engine.Core.Abstract;
|
|
||||||
|
|
||||||
namespace Pong.Network;
|
|
||||||
|
|
||||||
public interface INetworkCommunicator
|
|
||||||
{
|
|
||||||
EventBasedNetListener Listener { get; }
|
|
||||||
NetManager Manager { get; }
|
|
||||||
|
|
||||||
void PollEvents();
|
|
||||||
void Stop();
|
|
||||||
|
|
||||||
void RegisterEntityListener(IEntity entity, Action<NetPacketReader> onDataReceived);
|
|
||||||
void UnregisterEntityListener(IEntity entity);
|
|
||||||
NetDataWriter GetEntityWriter(IEntity entity);
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
namespace Pong.Network;
|
|
||||||
|
|
||||||
public interface INetworkServer : INetworkCommunicator
|
|
||||||
{
|
|
||||||
string Password { get; }
|
|
||||||
int MaxConnectionCount { get; }
|
|
||||||
int Port { get; }
|
|
||||||
|
|
||||||
void Start(int port, int maxConnectionCount, string? password = null);
|
|
||||||
}
|
|
@ -1,73 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
using LiteNetLib;
|
|
||||||
using LiteNetLib.Utils;
|
|
||||||
|
|
||||||
using Syntriax.Engine.Core;
|
|
||||||
using Syntriax.Engine.Core.Abstract;
|
|
||||||
|
|
||||||
namespace Pong.Network;
|
|
||||||
|
|
||||||
public abstract class NetworkBase : BehaviourOverride, INetworkCommunicator
|
|
||||||
{
|
|
||||||
public EventBasedNetListener Listener { get; private set; } = null!;
|
|
||||||
public NetManager Manager { get; private set; } = null!;
|
|
||||||
|
|
||||||
public NetworkBase()
|
|
||||||
{
|
|
||||||
Priority = 10;
|
|
||||||
|
|
||||||
Listener = new EventBasedNetListener();
|
|
||||||
Manager = new NetManager(Listener);
|
|
||||||
|
|
||||||
Listener.NetworkReceiveEvent += NetworkReceiveEvent;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void PollEvents() => Manager.PollEvents();
|
|
||||||
public void Stop() => Manager.Stop();
|
|
||||||
|
|
||||||
protected override void OnUpdate() => PollEvents();
|
|
||||||
protected override void OnFinalize() => Stop();
|
|
||||||
|
|
||||||
private Dictionary<string, Action<NetPacketReader>> callbacks = new(32);
|
|
||||||
|
|
||||||
private void NetworkReceiveEvent(NetPeer peer, NetPacketReader reader, byte channel, DeliveryMethod deliveryMethod)
|
|
||||||
{
|
|
||||||
if (callbacks.TryGetValue(reader.GetString(), out var action))
|
|
||||||
action?.Invoke(reader);
|
|
||||||
|
|
||||||
reader.Recycle();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void RegisterEntityListener(IEntity entity, Action<NetPacketReader> onDataReceived)
|
|
||||||
{
|
|
||||||
if (callbacks.ContainsKey(entity.Id))
|
|
||||||
return;
|
|
||||||
|
|
||||||
callbacks.Add(entity.Id, onDataReceived);
|
|
||||||
entity.OnIdChanged += OnEntityIdChanged;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void UnregisterEntityListener(IEntity entity)
|
|
||||||
{
|
|
||||||
if (!callbacks.Remove(entity.Id))
|
|
||||||
return;
|
|
||||||
|
|
||||||
entity.OnIdChanged -= OnEntityIdChanged;
|
|
||||||
}
|
|
||||||
|
|
||||||
public NetDataWriter GetEntityWriter(IEntity entity)
|
|
||||||
{
|
|
||||||
NetDataWriter netDataWriter = new();
|
|
||||||
netDataWriter.Put(entity.Id);
|
|
||||||
return netDataWriter;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnEntityIdChanged(IEntity entity, string previousId)
|
|
||||||
{
|
|
||||||
var action = callbacks[previousId];
|
|
||||||
callbacks.Remove(previousId);
|
|
||||||
callbacks.Add(entity.Id, action);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +1,21 @@
|
|||||||
namespace Pong.Network;
|
using LiteNetLib;
|
||||||
|
|
||||||
public class NetworkClient : NetworkBase, INetworkClient
|
namespace Game.Network;
|
||||||
|
|
||||||
|
public class NetworkClient
|
||||||
{
|
{
|
||||||
public void Connect(string address, int port, string? password = null)
|
public readonly EventBasedNetListener Listener = null!;
|
||||||
|
public readonly NetManager Client = null!;
|
||||||
|
|
||||||
|
public NetworkClient()
|
||||||
{
|
{
|
||||||
Manager.Start();
|
Listener = new EventBasedNetListener();
|
||||||
Manager.Connect(address, port, password ?? string.Empty);
|
Client = new NetManager(Listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Connect(string address, int port, string? password = null)
|
||||||
|
=> Client.Connect(address, port, password ?? string.Empty);
|
||||||
|
|
||||||
|
public void PollEvents() => Client.PollEvents();
|
||||||
|
public void Stop() => Client.Stop();
|
||||||
}
|
}
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
using LiteNetLib;
|
|
||||||
using LiteNetLib.Utils;
|
|
||||||
using Syntriax.Engine.Core;
|
|
||||||
|
|
||||||
namespace Pong.Network;
|
|
||||||
|
|
||||||
public static class NetworkExtensions
|
|
||||||
{
|
|
||||||
public static Vector2D GetVector2D(this NetPacketReader reader)
|
|
||||||
=> new(reader.GetFloat(), reader.GetFloat());
|
|
||||||
|
|
||||||
public static void Put(this NetDataWriter writer, Vector2D vector)
|
|
||||||
{
|
|
||||||
writer.Put(vector.X);
|
|
||||||
writer.Put(vector.Y);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,16 +1,24 @@
|
|||||||
namespace Pong.Network;
|
using LiteNetLib;
|
||||||
|
|
||||||
public class NetworkServer : NetworkBase, INetworkServer
|
namespace Game.Network;
|
||||||
|
|
||||||
|
public class NetworkServer
|
||||||
{
|
{
|
||||||
public string Password { get; private set; } = string.Empty;
|
public string Password { get; private set; } = string.Empty;
|
||||||
public int MaxConnectionCount { get; private set; } = 0;
|
public int MaxConnectionCount { get; private set; } = 0;
|
||||||
public int Port { get; private set; } = 8888;
|
public int Port { get; private set; } = 8888;
|
||||||
|
|
||||||
public NetworkServer() : base()
|
public readonly EventBasedNetListener Listener = null!;
|
||||||
|
public readonly NetManager Server = null!;
|
||||||
|
|
||||||
|
public NetworkServer()
|
||||||
{
|
{
|
||||||
|
Listener = new EventBasedNetListener();
|
||||||
|
Server = new NetManager(Listener);
|
||||||
|
|
||||||
Listener.ConnectionRequestEvent += request =>
|
Listener.ConnectionRequestEvent += request =>
|
||||||
{
|
{
|
||||||
if (Manager.ConnectedPeersCount < MaxConnectionCount)
|
if (Server.ConnectedPeersCount < MaxConnectionCount)
|
||||||
request.AcceptIfKey(Password);
|
request.AcceptIfKey(Password);
|
||||||
else
|
else
|
||||||
request.Reject();
|
request.Reject();
|
||||||
@ -23,6 +31,9 @@ public class NetworkServer : NetworkBase, INetworkServer
|
|||||||
MaxConnectionCount = maxConnectionCount;
|
MaxConnectionCount = maxConnectionCount;
|
||||||
Port = port;
|
Port = port;
|
||||||
|
|
||||||
Manager.Start(port);
|
Server.Start(port);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void PollEvents() => Server.PollEvents();
|
||||||
|
public void Stop() => Server.Stop();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user