///--------------------------------------------
        void Preallocate <T>(ExclusiveGroupStruct groupID, uint size) where T : IEntityDescriptor, new()
        {
            using (var profiler = new PlatformProfiler("Preallocate"))
            {
                var entityComponentsToBuild  = EntityDescriptorTemplate <T> .descriptor.componentsToBuild;
                var numberOfEntityComponents = entityComponentsToBuild.Length;

                FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary> group = GetOrCreateGroup(groupID, profiler);

                for (var index = 0; index < numberOfEntityComponents; index++)
                {
                    var entityComponentBuilder = entityComponentsToBuild[index];
                    var entityComponentType    = entityComponentBuilder.GetEntityComponentType();

                    var refWrapper = new RefWrapper <Type>(entityComponentType);
                    if (group.TryGetValue(refWrapper, out var dbList) == false)
                    {
                        group[refWrapper] = entityComponentBuilder.Preallocate(ref dbList, size);
                    }
                    else
                    {
                        dbList.SetCapacity(size);
                    }

                    if (_groupsPerEntity.TryGetValue(refWrapper, out var groupedGroup) == false)
                    {
                        groupedGroup = _groupsPerEntity[refWrapper] = new FasterDictionary <uint, ITypeSafeDictionary>();
                    }

                    groupedGroup[groupID] = dbList;
                }
            }
        }
Example #2
0
        public static ExclusiveGroupStruct AddTag <T>(this ExclusiveGroupStruct group) where T : GroupTag <T>
        {
            if (_addTransitions.TryGetValue(group, out var transitions))
            {
                var type = new RefWrapper <Type>(typeof(T));
                if (transitions.TryGetValue(type, out var result))
                {
                    return(result);
                }
            }

            throw new ECSException("No add transition found for type "
                                   .FastConcat(typeof(T).ToString())
                                   .FastConcat(" in group ").FastConcat(group)
                                   );
        }
        void CopyEntityToDictionary(EGID entityGID, EGID toEntityGID,
                                    FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary> fromGroup,
                                    FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary> toGroup, Type entityViewType)
        {
            var wrapper = new RefWrapper <Type>(entityViewType);

            if (fromGroup.TryGetValue(wrapper, out var fromTypeSafeDictionary) == false)
            {
                throw new ECSException("no entities in from group eid: ".FastConcat(entityGID.entityID)
                                       .FastConcat(" group: ").FastConcat(entityGID.groupID));
            }

#if DEBUG && !PROFILER
            if (fromTypeSafeDictionary.Has(entityGID.entityID) == false)
            {
                throw new EntityNotFoundException(entityGID, entityViewType);
            }
#endif
            if (toGroup.TryGetValue(wrapper, out var toEntitiesDictionary) == false)
            {
                toEntitiesDictionary = fromTypeSafeDictionary.Create();
                toGroup.Add(wrapper, toEntitiesDictionary);
            }

            //todo: this must be unit tested properly
            if (_groupsPerEntity.TryGetValue(wrapper, out var groupedGroup) == false)
            {
                groupedGroup = _groupsPerEntity[wrapper] =
                    new FasterDictionary <uint, ITypeSafeDictionary>();
            }

            groupedGroup[toEntityGID.groupID] = toEntitiesDictionary;

            fromTypeSafeDictionary.AddEntityToDictionary(entityGID, toEntityGID, toEntitiesDictionary);
        }
        ///--------------------------------------------
        void Preallocate <T>(uint groupID, uint size) where T : IEntityDescriptor, new()
        {
            var entityViewsToBuild  = EntityDescriptorTemplate <T> .descriptor.entitiesToBuild;
            var numberOfEntityViews = entityViewsToBuild.Length;

            FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary> group = GetOrCreateGroup(groupID);

            for (var index = 0; index < numberOfEntityViews; index++)
            {
                var entityViewBuilder = entityViewsToBuild[index];
                var entityViewType    = entityViewBuilder.GetEntityType();

                var refWrapper = new RefWrapper <Type>(entityViewType);
                if (group.TryGetValue(refWrapper, out var dbList) == false)
                {
                    group[refWrapper] = entityViewBuilder.Preallocate(ref dbList, size);
                }
                else
                {
                    dbList.SetCapacity(size);
                }

                if (_groupsPerEntity.TryGetValue(refWrapper, out var groupedGroup) == false)
                {
                    groupedGroup = _groupsPerEntity[refWrapper] = new FasterDictionary <uint, ITypeSafeDictionary>();
                }

                groupedGroup[groupID] = dbList;
            }
        }
        static void AddEngine <T>(T engine, FasterDictionary <RefWrapper <Type>, FasterList <IEngine> > engines, Type type)
            where T : class, IEngine
        {
            if (engines.TryGetValue(new RefWrapper <Type>(type), out var list) == false)
            {
                list = new FasterList <IEngine>();

                engines.Add(new RefWrapper <Type>(type), list);
            }

            list.Add(engine);
        }
Example #6
0
        static Dictionary <Type, ITypeSafeDictionary> FetchEntityGroup(int groupID,
                                                                       FasterDictionary <int, Dictionary <Type, ITypeSafeDictionary> > groupEntityViewsByType)
        {
            Dictionary <Type, ITypeSafeDictionary> group;

            if (groupEntityViewsByType.TryGetValue(groupID, out @group) == false)
            {
                @group = new Dictionary <Type, ITypeSafeDictionary>();
                groupEntityViewsByType.Add(groupID, @group);
            }

            return(@group);
        }
        static void BuildEntity(EGID entityID, FasterDictionary <RefWrapper <Type>, ITypeSafeDictionary> group,
                                Type entityViewType, IEntityBuilder entityBuilder, IEnumerable <object> implementors)
        {
            var entityViewsPoolWillBeCreated =
                group.TryGetValue(new RefWrapper <Type>(entityViewType), out var safeDictionary) == false;

            //passing the undefined entityViewsByType inside the entityViewBuilder will allow it to be created with the
            //correct type and casted back to the undefined list. that's how the list will be eventually of the target
            //type.
            entityBuilder.BuildEntityAndAddToList(ref safeDictionary, entityID, implementors);

            if (entityViewsPoolWillBeCreated)
            {
                group.Add(new RefWrapper <Type>(entityViewType), safeDictionary);
            }
        }
Example #8
0
        static void AddEngineToList <T>
            (T engine, Type[] entityComponentTypes, FasterDictionary <RefWrapperType, FasterList <IReactEngine> > engines)
            where T : class, IReactEngine
        {
            for (var i = 0; i < entityComponentTypes.Length; i++)
            {
                var type = entityComponentTypes[i];

                if (engines.TryGetValue(new RefWrapperType(type), out var list) == false)
                {
                    list = new FasterList <IReactEngine>();

                    engines.Add(new RefWrapperType(type), list);
                }

                list.Add(engine);
            }
        }