Esempio n. 1
0
        public void SetArchetype(ArchetypeManager typeMan, Entity entity, Archetype *archetype,
                                 int *sharedComponentDataIndices)
        {
            var chunk      = typeMan.GetChunkWithEmptySlots(archetype, sharedComponentDataIndices);
            var chunkIndex = typeMan.AllocateIntoChunk(chunk);

            var oldArchetype  = m_Entities[entity.Index].Archetype;
            var oldChunk      = m_Entities[entity.Index].Chunk;
            var oldChunkIndex = m_Entities[entity.Index].IndexInChunk;

            ChunkDataUtility.Convert(oldChunk, oldChunkIndex, chunk, chunkIndex);
            if (chunk->ManagedArrayIndex >= 0 && oldChunk->ManagedArrayIndex >= 0)
            {
                ChunkDataUtility.CopyManagedObjects(typeMan, oldChunk, oldChunkIndex, chunk, chunkIndex, 1);
            }

            m_Entities[entity.Index].Archetype    = archetype;
            m_Entities[entity.Index].Chunk        = chunk;
            m_Entities[entity.Index].IndexInChunk = chunkIndex;

            var lastIndex = oldChunk->Count - 1;

            // No need to replace with ourselves
            if (lastIndex != oldChunkIndex)
            {
                var lastEntity = (Entity *)ChunkDataUtility.GetComponentData(oldChunk, lastIndex, 0);
                m_Entities[lastEntity->Index].IndexInChunk = oldChunkIndex;

                ChunkDataUtility.Copy(oldChunk, lastIndex, oldChunk, oldChunkIndex, 1);
                if (oldChunk->ManagedArrayIndex >= 0)
                {
                    ChunkDataUtility.CopyManagedObjects(typeMan, oldChunk, lastIndex, oldChunk, oldChunkIndex, 1);
                }
            }

            if (oldChunk->ManagedArrayIndex >= 0)
            {
                ChunkDataUtility.ClearManagedObjects(typeMan, oldChunk, lastIndex, 1);
            }

            --oldArchetype->EntityCount;
            typeMan.SetChunkCount(oldChunk, lastIndex);
        }
Esempio n. 2
0
        public void MoveEntityToChunk(ArchetypeManager typeMan, Entity entity, Chunk *newChunk, int newChunkIndex)
        {
            var oldChunk = m_Entities[entity.Index].Chunk;

            Assert.IsTrue(oldChunk->Archetype == newChunk->Archetype);

            var oldChunkIndex = m_Entities[entity.Index].IndexInChunk;

            ChunkDataUtility.Copy(oldChunk, oldChunkIndex, newChunk, newChunkIndex, 1);

            if (oldChunk->ManagedArrayIndex >= 0)
            {
                ChunkDataUtility.CopyManagedObjects(typeMan, oldChunk, oldChunkIndex, newChunk, newChunkIndex, 1);
            }

            m_Entities[entity.Index].Chunk        = newChunk;
            m_Entities[entity.Index].IndexInChunk = newChunkIndex;

            var lastIndex = oldChunk->Count - 1;

            // No need to replace with ourselves
            if (lastIndex != oldChunkIndex)
            {
                var lastEntity = (Entity *)ChunkDataUtility.GetComponentData(oldChunk, lastIndex, 0);
                m_Entities[lastEntity->Index].IndexInChunk = oldChunkIndex;

                ChunkDataUtility.Copy(oldChunk, lastIndex, oldChunk, oldChunkIndex, 1);
                if (oldChunk->ManagedArrayIndex >= 0)
                {
                    ChunkDataUtility.CopyManagedObjects(typeMan, oldChunk, lastIndex, oldChunk, oldChunkIndex, 1);
                }
            }

            if (oldChunk->ManagedArrayIndex >= 0)
            {
                ChunkDataUtility.ClearManagedObjects(typeMan, oldChunk, lastIndex, 1);
            }

            newChunk->Archetype->EntityCount--;
            typeMan.SetChunkCount(oldChunk, oldChunk->Count - 1);
        }
Esempio n. 3
0
        public void DeallocateEnties(ArchetypeManager typeMan, SharedComponentDataManager sharedComponentDataManager, Entity *entities, int count)
        {
            while (count != 0)
            {
                int    indexInChunk;
                int    batchCount;
                Chunk *chunk;

                //Profiler.BeginSample("DeallocateDataEntitiesInChunk");
                fixed(EntityDataManager *manager = &this)
                {
#if USE_BURST_DESTROY
                    chunk = ms_DeallocateDataEntitiesInChunkDelegate(manager, entities, count, out indexInChunk, out batchCount);
#else
                    chunk = DeallocateDataEntitiesInChunk(manager, entities, count, out indexInChunk, out batchCount);
#endif
                }

                //Profiler.EndSample();

                IncrementComponentOrderVersion(chunk->Archetype, chunk, sharedComponentDataManager);

                if (chunk->ManagedArrayIndex >= 0)
                {
                    // We can just chop-off the end, no need to copy anything
                    if (chunk->Count != indexInChunk + batchCount)
                    {
                        ChunkDataUtility.CopyManagedObjects(typeMan, chunk, chunk->Count - batchCount, chunk,
                                                            indexInChunk, batchCount);
                    }

                    ChunkDataUtility.ClearManagedObjects(typeMan, chunk, chunk->Count - batchCount, batchCount);
                }

                chunk->Archetype->EntityCount -= batchCount;
                typeMan.SetChunkCount(chunk, chunk->Count - batchCount);

                entities += batchCount;
                count    -= batchCount;
            }
        }
Esempio n. 4
0
        public void TryRemoveEntityId(Entity *entities, int count, ArchetypeManager archetypeManager,
                                      SharedComponentDataManager sharedComponentDataManager,
                                      EntityGroupManager groupManager, ComponentTypeInArchetype *componentTypeInArchetypeArray)
        {
            var entityIndex = 0;

            while (entityIndex != count)
            {
                int indexInChunk;
                int batchCount;
                fixed(EntityDataManager *manager = &this)
                {
                    var chunk = EntityChunkBatch(manager, entities + entityIndex, count - entityIndex, out indexInChunk,
                                                 out batchCount);
                    var archetype = GetArchetype(entities[entityIndex]);

                    if (!archetype->SystemStateCleanupNeeded)
                    {
                        DeallocateDataEntitiesInChunk(manager, entities + entityIndex, chunk, indexInChunk, batchCount);
                        IncrementComponentOrderVersion(chunk->Archetype, chunk, sharedComponentDataManager);

                        if (chunk->ManagedArrayIndex >= 0)
                        {
                            // We can just chop-off the end, no need to copy anything
                            if (chunk->Count != indexInChunk + batchCount)
                            {
                                ChunkDataUtility.CopyManagedObjects(archetypeManager, chunk, chunk->Count - batchCount,
                                                                    chunk,
                                                                    indexInChunk, batchCount);
                            }

                            ChunkDataUtility.ClearManagedObjects(archetypeManager, chunk, chunk->Count - batchCount,
                                                                 batchCount);
                        }

                        chunk->Archetype->EntityCount -= batchCount;
                        archetypeManager.SetChunkCount(chunk, chunk->Count - batchCount);
                    }
                    else
                    {
                        for (var batchEntityIndex = 0; batchEntityIndex < batchCount; batchEntityIndex++)
                        {
                            var entity                   = entities[entityIndex + batchEntityIndex];
                            var removedTypes             = 0;
                            var removedComponentIsShared = false;
                            for (var t = 1; t < archetype->TypesCount; ++t)
                            {
                                var type = archetype->Types[t];

                                if (!(type.IsSystemStateComponent || type.IsSystemStateSharedComponent))
                                {
                                    ++removedTypes;
                                    removedComponentIsShared |= type.IsSharedComponent;
                                }
                                else
                                {
                                    componentTypeInArchetypeArray[t - removedTypes] = archetype->Types[t];
                                }
                            }

                            componentTypeInArchetypeArray[archetype->TypesCount - removedTypes] =
                                new ComponentTypeInArchetype(ComponentType.Create <CleanupEntity>());

                            var newType = archetypeManager.GetOrCreateArchetype(componentTypeInArchetypeArray,
                                                                                archetype->TypesCount - removedTypes + 1, groupManager);

                            int *sharedComponentDataIndices = null;
                            if (newType->NumSharedComponents > 0)
                            {
                                var oldSharedComponentDataIndices =
                                    GetComponentChunk(entity)->SharedComponentValueArray;
                                if (removedComponentIsShared)
                                {
                                    int *tempAlloc = stackalloc int[newType->NumSharedComponents];
                                    sharedComponentDataIndices = tempAlloc;

                                    var srcIndex = 0;
                                    var dstIndex = 0;
                                    for (var t = 0; t < archetype->TypesCount; ++t)
                                    {
                                        if (archetype->SharedComponentOffset[t] != -1)
                                        {
                                            var typeIndex             = archetype->Types[t].TypeIndex;
                                            var systemStateType       = typeof(ISystemStateComponentData).IsAssignableFrom(TypeManager.GetType(typeIndex));
                                            var systemStateSharedType = typeof(ISystemStateSharedComponentData).IsAssignableFrom(TypeManager.GetType(typeIndex));
                                            if (!(systemStateType || systemStateSharedType))
                                            {
                                                srcIndex++;
                                            }
                                            else
                                            {
                                                sharedComponentDataIndices[dstIndex] =
                                                    oldSharedComponentDataIndices[srcIndex];
                                                srcIndex++;
                                                dstIndex++;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    // reuse old sharedComponentDataIndices
                                    sharedComponentDataIndices = oldSharedComponentDataIndices;
                                }
                            }

                            IncrementComponentOrderVersion(archetype, GetComponentChunk(entity),
                                                           sharedComponentDataManager);
                            SetArchetype(archetypeManager, entity, newType, sharedComponentDataIndices);
                        }
                    }
                }

                entityIndex += batchCount;
            }
        }
Esempio n. 5
0
        public void TryRemoveEntityId(Entity *entities, int count, ArchetypeManager archetypeManager,
                                      SharedComponentDataManager sharedComponentDataManager,
                                      EntityGroupManager groupManager, ComponentTypeInArchetype *componentTypeInArchetypeArray)
        {
            var entityIndex = 0;

            while (entityIndex != count)
            {
                int indexInChunk;
                int batchCount;
                fixed(EntityDataManager *manager = &this)
                {
                    var chunk = EntityChunkBatch(manager, entities + entityIndex, count - entityIndex, out indexInChunk,
                                                 out batchCount);
                    var archetype = GetArchetype(entities[entityIndex]);

                    if (!archetype->SystemStateCleanupNeeded)
                    {
                        DeallocateDataEntitiesInChunk(manager, entities + entityIndex, chunk, indexInChunk, batchCount);
                        IncrementComponentOrderVersion(chunk->Archetype, chunk, sharedComponentDataManager);

                        if (chunk->ManagedArrayIndex >= 0)
                        {
                            // We can just chop-off the end, no need to copy anything
                            if (chunk->Count != indexInChunk + batchCount)
                            {
                                ChunkDataUtility.CopyManagedObjects(archetypeManager, chunk, chunk->Count - batchCount,
                                                                    chunk,
                                                                    indexInChunk, batchCount);
                            }

                            ChunkDataUtility.ClearManagedObjects(archetypeManager, chunk, chunk->Count - batchCount,
                                                                 batchCount);
                        }

                        chunk->Archetype->EntityCount -= batchCount;
                        archetypeManager.SetChunkCount(chunk, chunk->Count - batchCount);
                    }
                    else
                    {
                        var newType = archetype->SystemStateResidueArchetype;

                        int *sharedComponentDataIndices = chunk->SharedComponentValueArray;
                        if ((newType->NumSharedComponents > 0) && (newType->NumSharedComponents != archetype->NumSharedComponents))
                        {
                            var  oldSharedComponentDataIndices = sharedComponentDataIndices;
                            int *tempAlloc = stackalloc int[newType->NumSharedComponents];
                            sharedComponentDataIndices = tempAlloc;

                            int oldIndex = 0;
                            for (int newIndex = 0; newIndex < newType->TypesCount; ++newIndex, ++oldIndex)
                            {
                                if (newType->SharedComponentOffset[newIndex] != -1)
                                {
                                    var t = newType->Types[newIndex];
                                    while (t != archetype->Types[oldIndex])
                                    {
                                        ++oldIndex;
                                    }
                                    var newOffset = newType->SharedComponentOffset[newIndex];
                                    var oldOffset = archetype->SharedComponentOffset[oldIndex];
                                    sharedComponentDataIndices[newOffset] = oldSharedComponentDataIndices[oldOffset];
                                }
                            }
                        }

                        for (var batchEntityIndex = 0; batchEntityIndex < batchCount; batchEntityIndex++)
                        {
                            var entity = entities[entityIndex + batchEntityIndex];
                            IncrementComponentOrderVersion(archetype, GetComponentChunk(entity),
                                                           sharedComponentDataManager);
                            SetArchetype(archetypeManager, entity, newType, sharedComponentDataIndices);
                        }
                    }
                }

                entityIndex += batchCount;
            }
        }