Beispiel #1
0
        public ComponentGroup CreateEntityGroup(ArchetypeManager typeMan, EntityDataManager *entityDataManager,
                                                ArchetypeQuery *archetypeQueries, int archetypeFiltersCount, ComponentType *requiredComponents, int requiredComponentsCount)
        {
            //@TODO: Validate that required types is subset of archetype filters all...

            var grp = (EntityGroupData *)m_GroupDataChunkAllocator.Allocate(sizeof(EntityGroupData), 8);

            grp->PrevGroup  = m_LastGroupData;
            m_LastGroupData = grp;
            grp->RequiredComponentsCount = requiredComponentsCount;
            grp->RequiredComponents      = requiredComponents;
            InitializeReaderWriter(grp, requiredComponents, requiredComponentsCount);

            grp->ArchetypeQuery         = archetypeQueries;
            grp->ArchetypeQueryCount    = archetypeFiltersCount;
            grp->FirstMatchingArchetype = null;
            grp->LastMatchingArchetype  = null;
            for (var i = typeMan.m_Archetypes.Count - 1; i >= 0; --i)
            {
                var archetype = typeMan.m_Archetypes.p[i];
                AddArchetypeIfMatching(archetype, grp);
            }

            return(new ComponentGroup(grp, m_JobSafetyManager, typeMan, entityDataManager));
        }
        public EntityQuery CreateEntityQuery(EntityComponentStore *entityComponentStore, ManagedComponentStore managedComponentStore,
                                             ArchetypeQuery *query, int queryCount, ComponentType *component, int componentCount)
        {
            //@TODO: Validate that required types is subset of archetype filters all...

            int hash = (int)math.hash(component, componentCount * sizeof(ComponentType));

            for (var i = 0; i < queryCount; ++i)
            {
                hash = hash * 397 ^ query[i].GetHashCode();
            }
            EntityQueryData *cachedQuery = null;

            if (m_EntityGroupDataCache.TryGetFirstValue(hash, out var entityGroupDataIndex, out var iterator))
            {
                do
                {
                    var possibleMatch = m_EntityGroupDatas.Ptr[entityGroupDataIndex];
                    if (Matches(possibleMatch, query, queryCount, component, componentCount))
                    {
                        cachedQuery = possibleMatch;
                        break;
                    }
                } while (m_EntityGroupDataCache.TryGetNextValue(out entityGroupDataIndex, ref iterator));
            }

            if (cachedQuery == null)
            {
                cachedQuery = (EntityQueryData *)ChunkAllocate <EntityQueryData>();
                cachedQuery->RequiredComponentsCount = componentCount;
                cachedQuery->RequiredComponents      = (ComponentType *)ChunkAllocate <ComponentType>(componentCount, component);
                InitializeReaderWriter(cachedQuery, component, componentCount);
                cachedQuery->ArchetypeQueryCount = queryCount;
                cachedQuery->ArchetypeQuery      = (ArchetypeQuery *)ChunkAllocate <ArchetypeQuery>(queryCount, query);
                for (var i = 0; i < queryCount; ++i)
                {
                    cachedQuery->ArchetypeQuery[i].All            = (int *)ChunkAllocate <int>(cachedQuery->ArchetypeQuery[i].AllCount, query[i].All);
                    cachedQuery->ArchetypeQuery[i].Any            = (int *)ChunkAllocate <int>(cachedQuery->ArchetypeQuery[i].AnyCount, query[i].Any);
                    cachedQuery->ArchetypeQuery[i].None           = (int *)ChunkAllocate <int>(cachedQuery->ArchetypeQuery[i].NoneCount, query[i].None);
                    cachedQuery->ArchetypeQuery[i].AllAccessMode  = (byte *)ChunkAllocate <byte>(cachedQuery->ArchetypeQuery[i].AllCount, query[i].AllAccessMode);
                    cachedQuery->ArchetypeQuery[i].AnyAccessMode  = (byte *)ChunkAllocate <byte>(cachedQuery->ArchetypeQuery[i].AnyCount, query[i].AnyAccessMode);
                    cachedQuery->ArchetypeQuery[i].NoneAccessMode = (byte *)ChunkAllocate <byte>(cachedQuery->ArchetypeQuery[i].NoneCount, query[i].NoneAccessMode);
                }
                cachedQuery->MatchingArchetypes = new UnsafeMatchingArchetypePtrList(entityComponentStore);

                cachedQuery->EntityQueryMask = new EntityQueryMask();

                for (var i = 0; i < entityComponentStore->m_Archetypes.Length; ++i)
                {
                    var archetype = entityComponentStore->m_Archetypes.Ptr[i];
                    AddArchetypeIfMatching(archetype, cachedQuery);
                }

                m_EntityGroupDataCache.Add(hash, m_EntityGroupDatas.Length);
                m_EntityGroupDatas.Add(cachedQuery);
            }

            return(new EntityQuery(cachedQuery, m_JobSafetyManager, entityComponentStore, managedComponentStore));
        }
        private unsafe ArchetypeQuery *CreateQuery(EntityArchetypeQuery[] query)
        {
            ArchetypeQuery *queryPtr = (ArchetypeQuery *)this.m_GroupDataChunkAllocator.Allocate(sizeof(ArchetypeQuery) * query.Length, UnsafeUtility.AlignOf <ArchetypeQuery>());

            for (int i = 0; i != query.Length; i++)
            {
                this.ConstructTypeArray(query[i].None, out queryPtr[i].None, out queryPtr[i].NoneCount);
                this.ConstructTypeArray(query[i].All, out queryPtr[i].All, out queryPtr[i].AllCount);
                this.ConstructTypeArray(query[i].Any, out queryPtr[i].Any, out queryPtr[i].AnyCount);
            }
            return(queryPtr);
        }
        static bool IsMatchingArchetype(Archetype *archetype, ArchetypeQuery *query)
        {
            if (!TestMatchingArchetypeAll(archetype, query->All, query->AllCount, query->Options))
            {
                return(false);
            }
            if (!TestMatchingArchetypeNone(archetype, query->None, query->NoneCount))
            {
                return(false);
            }
            if (!TestMatchingArchetypeAny(archetype, query->Any, query->AnyCount))
            {
                return(false);
            }

            return(true);
        }
        private unsafe ArchetypeQuery *CreateQuery(ComponentType[] requiredTypes)
        {
            ArchetypeQuery *queryPtr = (ArchetypeQuery *)ref this.m_GroupDataChunkAllocator.Allocate(sizeof(ArchetypeQuery), UnsafeUtility.AlignOf <ArchetypeQuery>());
            int             index    = 0;
            int             num2     = 0;
            int             num3     = 0;

            while (true)
            {
                if (num3 == requiredTypes.Length)
                {
                    queryPtr->All       = (int *)this.m_GroupDataChunkAllocator.Allocate(4 * num2, UnsafeUtility.AlignOf <int>());
                    queryPtr->AllCount  = num2;
                    queryPtr->None      = (int *)this.m_GroupDataChunkAllocator.Allocate(4 * index, UnsafeUtility.AlignOf <int>());
                    queryPtr->NoneCount = index;
                    queryPtr->Any       = null;
                    queryPtr->AnyCount  = 0;
                    index = 0;
                    num2  = 0;
                    for (int i = 0; i != requiredTypes.Length; i++)
                    {
                        if (requiredTypes[i].AccessModeType == ComponentType.AccessMode.Subtractive)
                        {
                            index++;
                            queryPtr->None[index] = requiredTypes[i].TypeIndex;
                        }
                        else
                        {
                            num2++;
                            queryPtr->All[num2] = requiredTypes[i].TypeIndex;
                        }
                    }
                    return(queryPtr);
                }
                if (requiredTypes[num3].AccessModeType == ComponentType.AccessMode.Subtractive)
                {
                    index++;
                }
                else
                {
                    num2++;
                }
                num3++;
            }
        }
        public unsafe ComponentGroup CreateEntityGroup(ArchetypeManager typeMan, EntityDataManager *entityDataManager, ArchetypeQuery *archetypeQueries, int archetypeFiltersCount, ComponentType *requiredComponents, int requiredComponentsCount)
        {
            EntityGroupData *grp = (EntityGroupData *)ref this.m_GroupDataChunkAllocator.Allocate(sizeof(EntityGroupData), 8);

            grp->PrevGroup               = this.m_LastGroupData;
            this.m_LastGroupData         = grp;
            grp->RequiredComponentsCount = requiredComponentsCount;
            grp->RequiredComponents      = requiredComponents;
            this.InitializeReaderWriter(grp, requiredComponents, requiredComponentsCount);
            grp->ArchetypeQuery         = archetypeQueries;
            grp->ArchetypeQueryCount    = archetypeFiltersCount;
            grp->FirstMatchingArchetype = null;
            grp->LastMatchingArchetype  = null;
            for (Archetype *archetypePtr = typeMan.m_LastArchetype; archetypePtr != null; archetypePtr = archetypePtr->PrevArchetype)
            {
                this.AddArchetypeIfMatching(archetypePtr, grp);
            }
            return(new ComponentGroup(grp, this.m_JobSafetyManager, typeMan, entityDataManager));
        }
        private static unsafe bool IsMatchingArchetype(Archetype *archetype, ArchetypeQuery *query)
        {
            bool flag2;

            if (!TestMatchingArchetypeAll(archetype, query.All, query.AllCount))
            {
                flag2 = false;
            }
            else if (!TestMatchingArchetypeNone(archetype, query.None, query.NoneCount))
            {
                flag2 = false;
            }
            else if (!TestMatchingArchetypeAny(archetype, query.Any, query.AnyCount))
            {
                flag2 = false;
            }
            else
            {
                flag2 = true;
            }
            return(flag2);
        }
 bool Matches(EntityQueryData *grp, ArchetypeQuery *archetypeQueries, int archetypeFiltersCount,
              ComponentType *requiredComponents, int requiredComponentsCount)
 {
     if (requiredComponentsCount != grp->RequiredComponentsCount)
     {
         return(false);
     }
     if (archetypeFiltersCount != grp->ArchetypeQueryCount)
     {
         return(false);
     }
     if (requiredComponentsCount > 0 && UnsafeUtility.MemCmp(requiredComponents, grp->RequiredComponents, sizeof(ComponentType) * requiredComponentsCount) != 0)
     {
         return(false);
     }
     for (var i = 0; i < archetypeFiltersCount; ++i)
     {
         if (!archetypeQueries[i].Equals(grp->ArchetypeQuery[i]))
         {
             return(false);
         }
     }
     return(true);
 }
        // Calculates the intersection of "All" queriesDesc
        private ComponentType *CalculateRequiredComponentsFromQuery(ref UnsafeScratchAllocator allocator, ArchetypeQuery *queries, int queryCount, out int outRequiredComponentsCount)
        {
            var maxIntersectionCount = 0;

            for (int queryIndex = 0; queryIndex < queryCount; ++queryIndex)
            {
                maxIntersectionCount = math.max(maxIntersectionCount, queries[queryIndex].AllCount);
            }

            var intersection = (int *)allocator.Allocate <int>(maxIntersectionCount);

            UnsafeUtility.MemCpy(intersection, queries[0].All, sizeof(int) * queries[0].AllCount);

            var intersectionCount = maxIntersectionCount;

            for (int i = 1; i < queryCount; ++i)
            {
                intersectionCount = IntersectSortedComponentIndexArrays(intersection, intersectionCount, queries[i].All,
                                                                        queries[i].AllCount, intersection);
            }

            var outRequiredComponents = (ComponentType *)allocator.Allocate <ComponentType>(intersectionCount + 1);

            outRequiredComponents[0] = ComponentType.ReadWrite <Entity>();
            for (int i = 0; i < intersectionCount; ++i)
            {
                outRequiredComponents[i + 1] = ComponentType.FromTypeIndex(intersection[i]);
            }

            outRequiredComponentsCount = intersectionCount + 1;
            return(outRequiredComponents);
        }