Ejemplo n.º 1
0
    private void AddedEntities()
    {
        EntityArray entities = addedSyncEntities.entities;
        ComponentDataArray <NetworkSync> networkSyncs = addedSyncEntities.networkSyncComponents;

        for (int i = 0; i < entities.Length; i++)
        {
            NetworkSync      networkSync = networkSyncs[i];
            int              instanceId  = networkSync.instanceId;
            NetworkSyncState component   = new NetworkSyncState()
            {
                actorId   = networkManager.LocalPlayerID,
                networkId = networkManager.GetNetworkId(),
            };
            Entity entity = entities[i];
            PostUpdateCommands.AddComponent(entity, component);

            if (NetworkUtility.CanAssignAuthority(networkManager, networkSync.authority))
            {
                PostUpdateCommands.AddComponent(entity, new NetworktAuthority());
            }

            NetworkEntityData networkEntityData = new NetworkEntityData {
                InstanceId = networkSync.instanceId,

                NetworkSyncEntity = new NetworkSyncEntity {
                    ActorId   = component.actorId,
                    NetworkId = component.networkId,
                }
            };

            for (int j = 0; j < AddComponentDataOnEntityAddedMethods.Count; j++)
            {
                if (AddComponentDataOnEntityAddedMethods[j].Invoke(this, ref entity, out ComponentDataContainer componentData))
                {
                    networkEntityData.ComponentData.Add(componentData);
                }
            }

            ownNetworkSendMessageUtility.AddEntity(networkEntityData);
            AllNetworkSendMessageUtility.AddEntity(networkEntityData);
        }
    }
    private void ReceiveNetworkUpdate(byte[] data)
    {
        NetworkSyncDataContainer networkSyncDataContainer = messageSerializer.Deserialize(data);

        if (LogReceivedMessages && (networkSyncDataContainer.AddedNetworkSyncEntities.Any() ||
                                    networkSyncDataContainer.RemovedNetworkSyncEntities.Any() ||
                                    networkSyncDataContainer.NetworkSyncDataEntities.Any()))
        {
            Debug.Log("ReceiveNetworkUpdate: " + NetworkMessageUtility.ToString(networkSyncDataContainer));
        }


        // added Entities
        List <NetworkEntityData> addedNetworkSyncEntities = networkSyncDataContainer.AddedNetworkSyncEntities;

        for (int i = 0; i < addedNetworkSyncEntities.Count; i++)
        {
            if (addedNetworkSyncEntities[i].NetworkSyncEntity.ActorId == networkManager.LocalPlayerID)
            {
                continue;
            }

            Entity      entity      = reflectionUtility.GetEntityFactoryMethod(addedNetworkSyncEntities[i].InstanceId).Invoke(EntityManager);
            NetworkSync networkSync = EntityManager.GetComponentData <NetworkSync>(entity);
            if (NetworkUtility.CanAssignAuthority(networkManager, networkSync.authority))
            {
                PostUpdateCommands.AddComponent(entity, new NetworktAuthority());
            }

            PostUpdateCommands.AddComponent(entity, new NetworkSyncState {
                actorId   = addedNetworkSyncEntities[i].NetworkSyncEntity.ActorId,
                networkId = addedNetworkSyncEntities[i].NetworkSyncEntity.NetworkId,
            });

            var componentData = addedNetworkSyncEntities[i].ComponentData;
            for (int j = 0; j < componentData.Count; j++)
            {
                ComponentType componentType = reflectionUtility.GetComponentType(componentData[j].ComponentTypeId);
                AddComponentsMethods[componentType].Invoke(this, entity, componentData[j].MemberData);
            }

            if (addedNetworkSyncEntities[i].NetworkSyncEntity.ActorId != networkManager.LocalPlayerID)
            {
                NetworkSendSystem.AllNetworkSendMessageUtility.AddEntity(addedNetworkSyncEntities[i]);
            }
        }

        // removed Entities
        List <NetworkSyncEntity> removedNetworkSyncEntities = networkSyncDataContainer.RemovedNetworkSyncEntities;

        for (int i = 0; i < removedNetworkSyncEntities.Count; i++)
        {
            if (removedNetworkSyncEntities[i].ActorId == networkManager.LocalPlayerID)
            {
                continue;
            }
            NetworkSyncEntity networkSyncEntity = removedNetworkSyncEntities[i];
            int hash = NetworkUtility.GetNetworkEntityHash(networkSyncEntity.ActorId, networkSyncEntity.NetworkId);
            if (networkEntityMap.TryGetValue(hash, out Entity entity))
            {
                PostUpdateCommands.RemoveComponent <NetworkSyncState>(entity);
                PostUpdateCommands.DestroyEntity(entity);

                if (EntityManager.HasComponent <Transform>(entity))
                {
                    gameObjectsToDestroy.Add(EntityManager.GetComponentObject <Transform>(entity).gameObject);
                }
                for (int j = 0; j < RemoveComponentOnDestroyEntityMethods.Count; j++)
                {
                    RemoveComponentOnDestroyEntityMethods[j].Invoke(this, entity);
                }
            }

            if (removedNetworkSyncEntities[i].ActorId != networkManager.LocalPlayerID)
            {
                NetworkSendSystem.AllNetworkSendMessageUtility.RemoveEntity(removedNetworkSyncEntities[i]);
            }
        }

        // update components
        List <NetworkSyncDataEntityContainer> networkSyncDataEntities = networkSyncDataContainer.NetworkSyncDataEntities;

        for (int i = 0; i < networkSyncDataEntities.Count; i++)
        {
            NetworkSyncEntity networkSyncEntity = networkSyncDataEntities[i].NetworkSyncEntity;
            if (networkSyncEntity.ActorId == networkManager.LocalPlayerID)
            {
                continue;
            }

            int hash = NetworkUtility.GetNetworkEntityHash(networkSyncEntity.ActorId, networkSyncEntity.NetworkId);
            if (!networkEntityMap.TryGetValue(hash, out Entity entity))
            {
                continue;
            }

            List <ComponentDataContainer> addedComponents = networkSyncDataEntities[i].AddedComponents;
            List <int> removedComponents = networkSyncDataEntities[i].RemovedComponents;
            List <ComponentDataContainer> componentData = networkSyncDataEntities[i].ComponentData;

            for (int j = 0; j < addedComponents.Count; j++)
            {
                ComponentType componentType = reflectionUtility.GetComponentType(addedComponents[j].ComponentTypeId);
                AddComponentsMethods[componentType].Invoke(this, entity, addedComponents[j].MemberData);
            }

            for (int j = 0; j < componentData.Count; j++)
            {
                ComponentType componentType = reflectionUtility.GetComponentType(componentData[j].ComponentTypeId);
                SetComponentsMethods[componentType].Invoke(this, entity, componentData[j].MemberData);
            }

            for (int j = 0; j < removedComponents.Count; j++)
            {
                ComponentType componentType = reflectionUtility.GetComponentType(removedComponents[j]);
                RemoveComponentsMethods[componentType].Invoke(this, entity);
            }


            if (networkSyncEntity.ActorId == networkManager.LocalPlayerID)
            {
                continue;
            }

            NetworkSendSystem.AllNetworkSendMessageUtility.AddComponents(entity, networkSyncEntity.ActorId, networkSyncEntity.NetworkId, addedComponents);
            NetworkSendSystem.AllNetworkSendMessageUtility.RemoveComponents(entity, networkSyncEntity.ActorId, networkSyncEntity.NetworkId, removedComponents);
            NetworkSendSystem.AllNetworkSendMessageUtility.SetComponentData(entity, networkSyncEntity.ActorId, networkSyncEntity.NetworkId, componentData);
        }
    }