Exemplo n.º 1
0
        public static void CopyComponentData(Chunk *source, Chunk *destination, int sourceIndex, int destinationIndex)
        {
            // TODO: I've no clue what this is about...

            var types = source->archetype->componentTypes;
            var count = source->archetype->componentCount;
            var sizes = source->archetype->componentSizes;

            var sourceOffsets = source->archetype->componentOffsets;
            var destOffsets   = destination->archetype->componentOffsets;

            for (int i = 0; i < count; ++i)
            {
                int indexInChunk = ArchetypeUtility.GetTypeIndex(destination->archetype, types[i]);

                if (indexInChunk == -1)
                {
                    continue;
                }

                var offsetInSourceBuffer = source->buffer + sourceOffsets[i] + (sourceIndex * sizes[i]);
                var offsetInDestBuffer   = destination->buffer + destOffsets[indexInChunk] + (destinationIndex * sizes[i]);

                Buffer.MemoryCopy(offsetInDestBuffer, offsetInSourceBuffer, (uint)sizes[indexInChunk], (uint)sizes[indexInChunk]);
                // Unsafe.CopyBlock((void*)offsetInDestBuffer, (void*)offsetInSourceBuffer, (uint)sizes[indexInChunk]);
            }
        }
Exemplo n.º 2
0
        public static int CopyComponentData <T>(Chunk *chunk, int typeIndex, T *buffer, int count) where T : unmanaged
        {
            count = (count <= chunk->count) ? count : chunk->count;

            var indexInArchetype = ArchetypeUtility.GetTypeIndex(chunk->archetype, typeIndex);             // Make sure that the type is in the archetype beforehand...
            var size             = count * sizeof(T);

            Buffer.MemoryCopy(ChunkUtility.GetComponentPtrInBuffer <T>(chunk, indexInArchetype), buffer, size, size);

            return(count);
        }
Exemplo n.º 3
0
        public static T *GetComponentDataPtr <T>(Chunk *chunk, int index, int typeIndex) where T : unmanaged
        {
            var indexInArchetype = ArchetypeUtility.GetTypeIndex(chunk->archetype, typeIndex);

            if (indexInArchetype < 0)
            {
                return(null);
            }

            return(&GetComponentPtrInBuffer <T>(chunk, indexInArchetype)[index]);
        }
Exemplo n.º 4
0
        public static Span <T> GetComponentData <T>(Chunk *chunk, int typeIndex) where T : unmanaged
        {
            var indexInArchetype = ArchetypeUtility.GetTypeIndex(chunk->archetype, typeIndex);

            if (indexInArchetype < 0)
            {
                return(Span <T> .Empty);
            }

            return(new Span <T>(GetComponentPtrInBuffer <T>(chunk, indexInArchetype), chunk->count));
        }
Exemplo n.º 5
0
        public bool HasComponentData <T>(Entity entity) where T : unmanaged, IComponentData
        {
            if (!IsAlive(entity))
            {
                return(false);
            }

            int typeIndex = TypeRegistry <T> .typeIndex;

            return(ArchetypeUtility.GetTypeIndex(GetArchetypeInternal(entity), typeIndex) >= 0);
        }
Exemplo n.º 6
0
        public static bool TryGetComponentData <T>(Chunk *chunk, int index, int typeIndex, out T value) where T : unmanaged
        {
            var indexInArchetype = ArchetypeUtility.GetTypeIndex(chunk->archetype, typeIndex);

            if (indexInArchetype < 0)
            {
                value = default;
                return(false);
            }

            value = GetComponentPtrInBuffer <T>(chunk, indexInArchetype)[index];
            return(true);
        }
Exemplo n.º 7
0
        public static bool SetComponentData <T>(Chunk *chunk, int index, int typeIndex, T value) where T : unmanaged
        {
            var indexInArchetype = ArchetypeUtility.GetTypeIndex(chunk->archetype, typeIndex);

            if (indexInArchetype < 0)
            {
                return(false);
            }

            T *data = GetComponentPtrInBuffer <T>(chunk, indexInArchetype);

            data[index] = value;

            return(true);
        }
Exemplo n.º 8
0
        internal EntityArchetype RemoveTypeFromArchetype <T>(Archetype *archetype) where T : unmanaged
        {
            // TODO: Needs a check if the archetypes contains typeof(T).

            int srcCount = archetype->componentCount;

            if (srcCount <= 1)
            {
                return(CreateArchetype());
            }

            int        destCount     = srcCount - 1;
            Span <int> componentData = destCount > 16
                                ? new int[destCount * 2]
                                : stackalloc int[destCount * 2];

            var destTypes = componentData.Slice(0, destCount);
            var destSizes = componentData.Slice(destCount);

            var sourceTypes = new Span <int>(archetype->componentTypes, srcCount);
            var sourceSizes = new Span <int>(archetype->componentSizes, srcCount);

            int indexInArchetype = ArchetypeUtility.GetTypeIndex(archetype, TypeRegistry <T> .typeIndex);

            // Is it somehow possible to remove the branches here?

            if (indexInArchetype > 0)
            {
                sourceTypes.Slice(0, indexInArchetype).CopyTo(destTypes);
                sourceSizes.Slice(0, indexInArchetype).CopyTo(destSizes);
            }

            if (indexInArchetype < destCount)
            {
                sourceTypes.Slice(indexInArchetype + 1).CopyTo(destTypes.Slice(indexInArchetype));
                sourceSizes.Slice(indexInArchetype + 1).CopyTo(destSizes.Slice(indexInArchetype));
            }

            int blockSize = 0;

            for (int i = 0; i < destSizes.Length; ++i)            // TODO: Use CalculateBlockSize instead.
            {
                blockSize += destSizes[i];
            }

            return(CreateArchetypeInternal(destTypes, destSizes, blockSize));
        }
Exemplo n.º 9
0
        public bool HasComponentData <T>() where T : unmanaged
        {
            int typeIndex = TypeRegistry <T> .typeIndex;

            return(ArchetypeUtility.GetTypeIndex(this.chunk->archetype, typeIndex) != -1);
        }