refactor: string id to IConnection parameters for sending network packets

This commit is contained in:
Syntriax 2025-06-22 20:35:00 +03:00
parent bc6aaa865a
commit 5cfed3ba56
4 changed files with 20 additions and 14 deletions

View File

@ -32,7 +32,7 @@ public class BallBehaviour : Behaviour2D, IFirstFrameUpdate, IPhysicsUpdate, INe
{ {
ResetBall(); ResetBall();
RigidBody.Velocity = launchDirection * Speed; RigidBody.Velocity = launchDirection * Speed;
networkServer?.SendToClient("*", new BallUpdatePacket(this)); networkServer?.SendToAll(new BallUpdatePacket(this));
} }
public void ResetBall() public void ResetBall()
@ -41,7 +41,7 @@ public class BallBehaviour : Behaviour2D, IFirstFrameUpdate, IPhysicsUpdate, INe
tweenManager.CancelTween(networkTween); tweenManager.CancelTween(networkTween);
Transform.Position = Vector2D.Zero; Transform.Position = Vector2D.Zero;
RigidBody.Velocity = Vector2D.Zero; RigidBody.Velocity = Vector2D.Zero;
networkServer?.SendToClient("*", new BallResetPacket()); networkServer?.SendToAll(new BallResetPacket());
} }
public void PhysicsUpdate(float delta) public void PhysicsUpdate(float delta)
@ -59,7 +59,7 @@ public class BallBehaviour : Behaviour2D, IFirstFrameUpdate, IPhysicsUpdate, INe
RigidBody.Velocity = information.Detected.Transform.Position.FromTo(information.Detector.Transform.Position).Normalized * RigidBody.Velocity.Magnitude; RigidBody.Velocity = information.Detected.Transform.Position.FromTo(information.Detector.Transform.Position).Normalized * RigidBody.Velocity.Magnitude;
else else
RigidBody.Velocity = RigidBody.Velocity.Reflect(information.Normal); RigidBody.Velocity = RigidBody.Velocity.Reflect(information.Normal);
networkServer?.SendToClient("*", new BallUpdatePacket(this)); networkServer?.SendToAll(new BallUpdatePacket(this));
} }
public BallBehaviour() { } public BallBehaviour() { }

View File

@ -75,7 +75,7 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
isUpPressed = packet.IsUpPressed; isUpPressed = packet.IsUpPressed;
isDownPressed = packet.IsDownPressed; isDownPressed = packet.IsDownPressed;
networkServer?.SendToClient("*", new PaddleKeyStatePacket(this)); networkServer?.SendToAll(new PaddleKeyStatePacket(this));
} }
public void OnClientPacketArrived(IConnection sender, PaddleKeyStatePacket packet) public void OnClientPacketArrived(IConnection sender, PaddleKeyStatePacket packet)

View File

@ -32,5 +32,6 @@ public interface INetworkCommunicatorServer : INetworkCommunicator
INetworkCommunicatorServer Start(int port, int maxConnectionCount, string? password = null); INetworkCommunicatorServer Start(int port, int maxConnectionCount, string? password = null);
INetworkCommunicatorServer SendToClient<T>(string to, T packet) where T : class, new(); INetworkCommunicatorServer SendToClient<T>(IConnection connection, T packet) where T : class, new();
INetworkCommunicatorServer SendToAll<T>(T packet) where T : class, new();
} }

View File

@ -50,19 +50,24 @@ public class LiteNetLibServer : LiteNetLibCommunicatorBase, INetworkCommunicator
return this; return this;
} }
public INetworkCommunicatorServer SendToClient<T>(string to, T packet) where T : class, new() public INetworkCommunicatorServer SendToClient<T>(IConnection connection, T packet) where T : class, new()
{ {
bool isBroadcastToAll = to.CompareTo("*") == 0;
netDataWriter.Reset(); netDataWriter.Reset();
netPacketProcessor.Write(netDataWriter, packet); netPacketProcessor.Write(netDataWriter, packet);
if (isBroadcastToAll)
Manager.SendToAll(netDataWriter, DeliveryMethod.ReliableOrdered);
else if (Manager.ConnectedPeerList.FirstOrDefault(p => p.Id.ToString().CompareTo(to) == 0) is NetPeer netPeer)
netPeer.Send(netDataWriter, DeliveryMethod.ReliableOrdered);
else
throw new($"Peer {to} couldn't be found.");
if (Manager.ConnectedPeerList.FirstOrDefault(p => p.Id.CompareTo(connection.Id) == 0) is not NetPeer netPeer)
throw new($"Peer {connection} couldn't be found.");
netPeer.Send(netDataWriter, DeliveryMethod.ReliableOrdered);
return this;
}
public INetworkCommunicatorServer SendToAll<T>(T packet) where T : class, new()
{
netDataWriter.Reset();
netPacketProcessor.Write(netDataWriter, packet);
Manager.SendToAll(netDataWriter, DeliveryMethod.ReliableOrdered);
return this; return this;
} }