feat: network manager listens to all received packets

This commit is contained in:
Syntriax 2025-05-12 19:18:02 +03:00
parent 0153d4cf69
commit 6591326b70

View File

@ -6,7 +6,22 @@ namespace Syntriax.Engine.Network;
public class NetworkManager : UniverseObject, INetworkManager
{
public INetworkCommunicator NetworkCommunicator { get; set; } = null!;
private INetworkCommunicator networkCommunicator = null!;
public INetworkCommunicator NetworkCommunicator
{
get => networkCommunicator;
set
{
if (networkCommunicator == value)
return;
var previousCommunicator = networkCommunicator;
networkCommunicator = value;
if (previousCommunicator is not null) previousCommunicator.OnPacketReceived -= OnPacketReceived;
if (networkCommunicator is not null) networkCommunicator.OnPacketReceived += OnPacketReceived;
}
}
private readonly Dictionary<string, INetworkEntity> _networkEntities = [];
public IReadOnlyDictionary<string, INetworkEntity> NetworkEntities => _networkEntities;
@ -20,6 +35,14 @@ public class NetworkManager : UniverseObject, INetworkManager
_networkEntityCollector.OnRemoved += OnRemoved;
}
private void OnPacketReceived(INetworkCommunicator sender, object packet, string from)
{
if (packet is not EntityDataPacket entityDataPacket)
return;
_networkEntities[entityDataPacket.Entity].ReceiveData(entityDataPacket.Data);
}
private void OnCollected(IBehaviourCollector<INetworkEntity> sender, INetworkEntity behaviourCollected)
{
if (!_networkEntities.TryAdd(behaviourCollected.Id, behaviourCollected))