Exemple #1
0
        public EntityArchetype CreateArchetype(ReadOnlySpan <Type> types)
        {
            if (types.Length == 0)
            {
                return(CreateArchetype());
            }

            // The allocation > 16 is not cool. Is it possible to avoid this?
            Span <int> componentData = types.Length > 16
                                ? new int[types.Length * 2]
                                : stackalloc int[types.Length * 2];

            Span <int> componentTypes = componentData.Slice(0, types.Length);
            Span <int> componentSizes = componentData.Slice(types.Length);
            int        blockSize      = 0;

            for (int i = 0; i < types.Length; ++i)
            {
                componentTypes[i] = TypeRegistry.ToTypeIndex(types[i]);
                componentSizes[i] = Marshal.SizeOf(types[i]);
                blockSize        += componentSizes[i];
            }

            SortUtility.Sort(componentTypes, componentSizes);

            return(CreateArchetypeInternal(componentTypes, componentSizes, blockSize));
        }
Exemple #2
0
        public EntityQuery CreateQuery(Span <Type> includeTypes, Span <Type> excludeTypes)
        {
            var typeCount = includeTypes.Length + excludeTypes.Length;

            Span <int> types = typeCount > 16
                                ? new int[typeCount]
                                : stackalloc int[typeCount];

            for (int i = 0; i < includeTypes.Length; ++i)
            {
                types[i] = TypeRegistry.ToTypeIndex(includeTypes[i]);
            }

            for (int i = 0; i < excludeTypes.Length; ++i)
            {
                types[i + includeTypes.Length] = TypeRegistry.ToTypeIndex(excludeTypes[i]);
            }

            SortUtility.Sort(types.Slice(0, includeTypes.Length));
            SortUtility.Sort(types.Slice(includeTypes.Length));

            // TODO: Same as archetypes: Allow only distinct types.

            return(CreateQueryInternal(types, includeTypes.Length, excludeTypes.Length));
        }
Exemple #3
0
        internal EntityArchetype AddTypeToArchetype <T>(Archetype *archetype) where T : unmanaged
        {
            // TODO: Needs a check if the archetypes contains typeof(T).

            var srcCount = archetype->componentCount;

            if (srcCount == 0)
            {
                return(CreateArchetype <T>());
            }

            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);

            sourceTypes.CopyTo(destTypes);
            sourceSizes.CopyTo(destSizes);

            destTypes[srcCount] = TypeRegistry <T> .typeIndex;
            destSizes[srcCount] = sizeof(T);

            // Sometimes the added type is a new type, therefore the typeIndex is
            // higher than the indices in the array. In this case skip the sorting.
            if (destTypes[srcCount] < destTypes[srcCount - 1])
            {
                SortUtility.Sort(destTypes, destSizes);
            }

            int blockSize = 0;

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

            return(CreateArchetypeInternal(destTypes, destSizes, blockSize));
        }