diff --git a/Shared/Network/NetworkManager.cs b/Shared/Network/NetworkManager.cs index f1cdf0d..62b40e4 100644 --- a/Shared/Network/NetworkManager.cs +++ b/Shared/Network/NetworkManager.cs @@ -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 _networkEntities = []; public IReadOnlyDictionary 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 sender, INetworkEntity behaviourCollected) { if (!_networkEntities.TryAdd(behaviourCollected.Id, behaviourCollected))