コード例 #1
0
        public void PerformUpdate(EntityManager entityManager, AbsoluteSimulationFrame simulationFrame, IInBitStream bitStream)
        {
            var deserializeEntity = new Deserializator();

            while (deserializeEntity.ReadEntity(bitStream, out var entityWithMeta))
            {
                var entity = mapper.ToUnityEntity(entityWithMeta.EntityId);

                var wasSimulated = entity != default && entityManager.Exists(entity) && entityManager.HasComponent <Simulated>(entity);

                // Skip locally destroyed entities
                if (destroyedEntities.ContainsKey(entity))
                {
                    if (!entityWithMeta.IsDeleted)
                    {
                        DeserializeComponentSkip.SkipComponents(componentSkip, bitStream);
                    }
                    continue;
                }

                // Meta information concerns entity creation, destruction and ownership
                if (entityWithMeta.HasMeta)
                {
                    entity = PerformEntityMetaUpdate(entityManager, entityWithMeta, entity);
                }

                // Deserialize and apply component updates
                if (entity != default)
                {
                    var isSimulated    = entityManager.HasComponent <Simulated>(entity);
                    var wasTransferred = isSimulated && !wasSimulated;
                    // Only update components for non-simulated entities - unless it was transferred with this packet
                    if (!isSimulated || wasTransferred)
                    {
                        UpdateComponents(entityManager, entity, simulationFrame, bitStream);
                    }
                    else
                    {
                        DeserializeComponentSkip.SkipComponents(componentSkip, bitStream);
                        Log.Warning($"Trying to update owned entity {entityWithMeta.EntityId}");
                    }
                }
                else if (!entityWithMeta.IsDeleted)
                {
                    // An error has occurred if the entity is null unless it's because it was just deleted
                    Log.Warning($"Entity is missing {entityWithMeta.EntityId}");

                    DeserializeComponentSkip.SkipComponents(componentSkip, bitStream);
                }
            }
        }
コード例 #2
0
        private void UpdateComponents(EntityManager entityManager, Entity entity, AbsoluteSimulationFrame simulationFrame, IInBitStream bitStream)
        {
            var componentCount = Deserializator.ReadComponentCount(bitStream);

            for (var i = 0; i < componentCount; i++)
            {
                var componentState = Deserializator.ReadComponentState(bitStream);
                var componentId    = Deserializator.ReadComponentId(bitStream);
                switch (componentState)
                {
                case ComponentState.Construct:
                {
                    var componentTypeId = Deserializator.ReadComponentTypeId(bitStream);

                    componentDeserialize.CreateIfNeededAndReadComponentDataUpdate(entityManager,
                                                                                  entity, componentTypeId, simulationFrame, bitStream);
                }
                break;

                case ComponentState.Update:
                {
                    // TODO: lookup component ID from state.
                    var updateComponentTypeId = componentId;
                    componentDeserialize.ReadComponentDataUpdate(entityManager, entity,
                                                                 updateComponentTypeId, simulationFrame, bitStream);
                }
                break;

                case ComponentState.Destruct:
                {
                    var destroyComponentTypeId = componentId;
                    DestroyComponentData(entityManager, entity, destroyComponentTypeId);
                }
                break;
                }
            }
        }
        private void DeserializeLocalUser(EntityManager entityManager, Entity entity, bool componentOwnership, AbsoluteSimulationFrame simulationFrame, Coherence.Replication.Protocol.Definition.IInBitStream protocolStream, bool justCreated, IInBitStream bitStream)
        {
            // If we own the entity, don't overwrite with downstream data from server
            // TODO: Server should never send downstream to the simulating client
            if (componentOwnership)
            {
                // Read and discard data (the stream must always be read)
                var temp = new LocalUser();
                unityReaders.Read(ref temp, protocolStream);
                return;
            }


            // Overwrite components that don't use interpolation
            var componentData = entityManager.GetComponentData <LocalUser>(entity);

            unityReaders.Read(ref componentData, protocolStream);
            entityManager.SetComponentData(entity, componentData);
        }
        private void DeserializeWorldOrientation(EntityManager entityManager, Entity entity, bool componentOwnership, AbsoluteSimulationFrame simulationFrame, Coherence.Replication.Protocol.Definition.IInBitStream protocolStream, bool justCreated, IInBitStream bitStream)
        {
            // If we own the entity, don't overwrite with downstream data from server
            // TODO: Server should never send downstream to the simulating client
            if (componentOwnership)
            {
                // Read and discard data (the stream must always be read)
                var temp = new Rotation();
                unityReaders.Read(ref temp, protocolStream);
                return;
            }


            // Ensure entities with interpolation also have Interpolation components and Sample components
            if (!entityManager.HasComponent <InterpolationComponent_Rotation>(entity))
            {
                entityManager.AddComponent <InterpolationComponent_Rotation>(entity);
                entityManager.AddComponent <Sample_Rotation>(entity);
            }

            // Append buffer for components that use interpolation
            var tempComponentData = new Rotation();

            unityReaders.Read(ref tempComponentData, protocolStream);
            if (justCreated)             // Hack
            {
                entityManager.SetComponentData(entity, tempComponentData);
            }
            InterpolationSystem_Rotation.AppendBuffer(entity, tempComponentData, entityManager.World, (ulong)simulationFrame.Frame);
        }
 public void ReadComponentDataUpdate(EntityManager entityManager, Entity entity, uint componentType, AbsoluteSimulationFrame simulationFrame, IInBitStream bitStream)
 {
     deserializeComponentUpdateGenerated.ReadComponentDataUpdate(entityManager, entity, componentType, simulationFrame, bitStream);
 }
        public void CreateIfNeededAndReadComponentDataUpdate(EntityManager entityManager, Entity entity, uint componentType, AbsoluteSimulationFrame simulationFrame, IInBitStream bitStream)
        {
            #region Commands

            {
                var hasBuffer = entityManager.HasComponent <AuthorityTransfer>(entity);
                if (!hasBuffer)
                {
                    entityManager.AddBuffer <AuthorityTransfer>(entity);
                }

                var hasRequestBuffer = entityManager.HasComponent <AuthorityTransferRequest>(entity);
                if (!hasRequestBuffer)
                {
                    entityManager.AddBuffer <AuthorityTransferRequest>(entity);
                }
            }

            #endregion

            switch (componentType)
            {
            case TypeIds.InternalWorldPosition:
            {
                var justCreated             = false;
                var hasComponentData        = entityManager.HasComponent <Translation>(entity);
                var componentHasBeenRemoved = entityManager.HasComponent <WorldPosition_Sync>(entity) && entityManager.GetComponentData <WorldPosition_Sync>(entity).deletedAtTime > 0;
                if (!hasComponentData && !componentHasBeenRemoved)
                {
                    entityManager.AddComponentData(entity, new Translation());
                    justCreated = true;
                }

                ReadComponentDataUpdateEx(entityManager, entity, componentType, simulationFrame, bitStream, justCreated);
                break;
            }

            case TypeIds.InternalWorldOrientation:
            {
                var justCreated             = false;
                var hasComponentData        = entityManager.HasComponent <Rotation>(entity);
                var componentHasBeenRemoved = entityManager.HasComponent <WorldOrientation_Sync>(entity) && entityManager.GetComponentData <WorldOrientation_Sync>(entity).deletedAtTime > 0;
                if (!hasComponentData && !componentHasBeenRemoved)
                {
                    entityManager.AddComponentData(entity, new Rotation());
                    justCreated = true;
                }

                ReadComponentDataUpdateEx(entityManager, entity, componentType, simulationFrame, bitStream, justCreated);
                break;
            }

            case TypeIds.InternalLocalUser:
            {
                var justCreated             = false;
                var hasComponentData        = entityManager.HasComponent <LocalUser>(entity);
                var componentHasBeenRemoved = entityManager.HasComponent <LocalUser_Sync>(entity) && entityManager.GetComponentData <LocalUser_Sync>(entity).deletedAtTime > 0;
                if (!hasComponentData && !componentHasBeenRemoved)
                {
                    entityManager.AddComponentData(entity, new LocalUser());
                    justCreated = true;
                }

                ReadComponentDataUpdateEx(entityManager, entity, componentType, simulationFrame, bitStream, justCreated);
                break;
            }

            case TypeIds.InternalWorldPositionQuery:
            {
                var justCreated             = false;
                var hasComponentData        = entityManager.HasComponent <WorldPositionQuery>(entity);
                var componentHasBeenRemoved = entityManager.HasComponent <WorldPositionQuery_Sync>(entity) && entityManager.GetComponentData <WorldPositionQuery_Sync>(entity).deletedAtTime > 0;
                if (!hasComponentData && !componentHasBeenRemoved)
                {
                    entityManager.AddComponentData(entity, new WorldPositionQuery());
                    justCreated = true;
                }

                ReadComponentDataUpdateEx(entityManager, entity, componentType, simulationFrame, bitStream, justCreated);
                break;
            }

            case TypeIds.InternalArchetypeComponent:
            {
                var justCreated             = false;
                var hasComponentData        = entityManager.HasComponent <ArchetypeComponent>(entity);
                var componentHasBeenRemoved = entityManager.HasComponent <ArchetypeComponent_Sync>(entity) && entityManager.GetComponentData <ArchetypeComponent_Sync>(entity).deletedAtTime > 0;
                if (!hasComponentData && !componentHasBeenRemoved)
                {
                    entityManager.AddComponentData(entity, new ArchetypeComponent());
                    justCreated = true;
                }

                ReadComponentDataUpdateEx(entityManager, entity, componentType, simulationFrame, bitStream, justCreated);
                break;
            }

            case TypeIds.InternalPersistence:
            {
                var justCreated             = false;
                var hasComponentData        = entityManager.HasComponent <Persistence>(entity);
                var componentHasBeenRemoved = entityManager.HasComponent <Persistence_Sync>(entity) && entityManager.GetComponentData <Persistence_Sync>(entity).deletedAtTime > 0;
                if (!hasComponentData && !componentHasBeenRemoved)
                {
                    entityManager.AddComponentData(entity, new Persistence());
                    justCreated = true;
                }

                ReadComponentDataUpdateEx(entityManager, entity, componentType, simulationFrame, bitStream, justCreated);
                break;
            }

            case TypeIds.InternalPlayer:
            {
                var justCreated             = false;
                var hasComponentData        = entityManager.HasComponent <Player>(entity);
                var componentHasBeenRemoved = entityManager.HasComponent <Player_Sync>(entity) && entityManager.GetComponentData <Player_Sync>(entity).deletedAtTime > 0;
                if (!hasComponentData && !componentHasBeenRemoved)
                {
                    entityManager.AddComponentData(entity, new Player());
                    justCreated = true;
                }

                ReadComponentDataUpdateEx(entityManager, entity, componentType, simulationFrame, bitStream, justCreated);
                break;
            }

            default:
            {
                Log.Warning("can not create component type");
                break;
            }
            }
        }
        public void ReadComponentDataUpdateEx(EntityManager entityManager, Entity entity, uint componentType, AbsoluteSimulationFrame simulationFrame, IInBitStream bitStream, bool justCreated)
        {
            var componentOwnership = Deserializator.ReadComponentOwnership(bitStream); // Read bit from stream...

            componentOwnership = entityManager.HasComponent <Simulated>(entity);       // Then overwrite it with entity ownership.
            var inProtocolStream = new Coherence.FieldStream.Deserialize.Streams.InBitStream(bitStream);

            switch (componentType)
            {
            case TypeIds.InternalWorldPosition:
                DeserializeWorldPosition(entityManager, entity, componentOwnership, simulationFrame, inProtocolStream, justCreated, bitStream);
                break;

            case TypeIds.InternalWorldOrientation:
                DeserializeWorldOrientation(entityManager, entity, componentOwnership, simulationFrame, inProtocolStream, justCreated, bitStream);
                break;

            case TypeIds.InternalLocalUser:
                DeserializeLocalUser(entityManager, entity, componentOwnership, simulationFrame, inProtocolStream, justCreated, bitStream);
                break;

            case TypeIds.InternalWorldPositionQuery:
                DeserializeWorldPositionQuery(entityManager, entity, componentOwnership, simulationFrame, inProtocolStream, justCreated, bitStream);
                break;

            case TypeIds.InternalArchetypeComponent:
                DeserializeArchetypeComponent(entityManager, entity, componentOwnership, simulationFrame, inProtocolStream, justCreated, bitStream);
                break;

            case TypeIds.InternalPersistence:
                DeserializePersistence(entityManager, entity, componentOwnership, simulationFrame, inProtocolStream, justCreated, bitStream);
                break;

            case TypeIds.InternalPlayer:
                DeserializePlayer(entityManager, entity, componentOwnership, simulationFrame, inProtocolStream, justCreated, bitStream);
                break;


            default:
                Log.Warning("couldn't find component", "componentType", componentType);
                break;
            }
        }
 public void ReadComponentDataUpdate(EntityManager entityManager, Entity entity, uint componentType, AbsoluteSimulationFrame simulationFrame, IInBitStream bitStream)
 {
     ReadComponentDataUpdateEx(entityManager, entity, componentType, simulationFrame, bitStream, false);
 }
 private void DeserializePlayer(EntityManager entityManager, Entity entity, bool componentOwnership, AbsoluteSimulationFrame simulationFrame, Coherence.Replication.Protocol.Definition.IInBitStream protocolStream, bool justCreated, IInBitStream bitStream)
 {
     // No need to read empty components, just ensure that it's there
     if (!entityManager.HasComponent <Player>(entity))
     {
         entityManager.AddComponent <Player>(entity);
     }
 }