/// <summary> /// On broadcasting data, send all connected players to the new peer /// </summary> /// <param name="peerInstance"></param> public override void OnInstanceBroadcastingData(NetPeerInstance peerInstance) { if (!NetInstance.SelfHost) { return; } var chanManager = NetInstance.GetChannelManager(); var plBank = MainWorld.GetOrCreateManager <GamePlayerBank>(); var gpSystem = MainWorld.GetOrCreateManager <GamePlayerSystem>(); var em = MainWorld.GetOrCreateManager <EntityManager>(); var connectedPlayers = gpSystem.SlowGetAllConnectedPlayers(); for (int i = 0; i != connectedPlayers.Length; i++) { var entity = connectedPlayers.Entities[i]; if (em.HasComponent <PlayerPeerLink>(entity)) { var dataWriter = CreatePlayerDataPacket(entity, (NetPeer)peerInstance); chanManager.DefaultChannel.Manager.SendToAll(dataWriter, DeliveryMethod.ReliableOrdered); } } }
void EventUserStatusChange.IEv.Callback(EventUserStatusChange.Arguments args) { var caller = args.Caller; var user = args.User; var change = args.Change; if (!NetInstance.SelfHost || caller.Global.Main != NetInstance) { return; } var chanManager = NetInstance.GetChannelManager(); var plBank = MainWorld.GetOrCreateManager <GamePlayerBank>(); var gpSystem = MainWorld.GetOrCreateManager <GamePlayerSystem>(); var em = MainWorld.GetOrCreateManager <EntityManager>(); var player = new GamePlayer(); foreach (var bankPlayer in plBank.AllPlayers) { if (em.HasComponent <PlayerPeerLink>(bankPlayer.WorldPointer)) { var target = em.GetSharedComponentData <PlayerPeerLink>(bankPlayer.WorldPointer).Target; if (target == (NetPeer)user.GetPeerInstance()) { player = bankPlayer; break; } } } if (!player.IsCreated) { Debug.Log("Player wasn't found"); return; } if ((change & StatusChange.Removed) != 0) { if (player.WorldPointer.HasComponent <PlayerUserLink>(MainWorld)) { em.RemoveComponent <PlayerUserLink>(player.WorldPointer); } if (player.WorldPointer.HasComponent <PlayerPeerLink>(MainWorld)) { em.RemoveComponent <PlayerPeerLink>(player.WorldPointer); } if (player.WorldPointer.HasComponent <ConnectedPlayerEntity>(MainWorld)) { em.RemoveComponent <ConnectedPlayerEntity>(player.WorldPointer); } var manager = chanManager.DefaultChannel.Manager; var dataWriter = CreateRemovePlayerPacket(player.WorldPointer); foreach (var otherPeer in manager) { otherPeer.Send(dataWriter, DeliveryMethod.ReliableOrdered); } } if ((change & StatusChange.Added) != 0) { player.WorldPointer.SetOrAddSharedComponentData(new PlayerUserLink(user), MainWorld); player.WorldPointer.SetOrAddComponentData(new ConnectedPlayerEntity(), MainWorld); var dataWriter = CreatePlayerDataPacket(player.WorldPointer, (NetPeer)user.GetPeerInstance()); var manager = chanManager.DefaultChannel.Manager; foreach (var otherPeer in manager) { if (otherPeer == caller.Peer) { continue; } otherPeer.Send(dataWriter, DeliveryMethod.ReliableOrdered); } } }