コード例 #1
0
        void SwapEntityGroup <T>(int entityID, int fromGroupID, int toGroupID) where T : IEntityDescriptor, new()
        {
            DesignByContract.Check.Require(fromGroupID != toGroupID, "can't move an entity to the same group where it already belongs to");

            var entityViewBuilders      = ((EntityDescriptorInfo)EntityDescriptorTemplate <T> .Default).entityViewsToBuild;
            int entityViewBuildersCount = entityViewBuilders.Length;

            var dictionary = _groupEntityViewsDB[fromGroupID];

            Dictionary <Type, ITypeSafeList> groupedEntityViewsTyped;

            if (_groupEntityViewsDB.TryGetValue(toGroupID, out groupedEntityViewsTyped) == false)
            {
                groupedEntityViewsTyped = new Dictionary <Type, ITypeSafeList>();

                _groupEntityViewsDB.Add(toGroupID, groupedEntityViewsTyped);
            }

            for (int i = 0; i < entityViewBuildersCount; i++)
            {
                IEntityViewBuilder entityViewBuilder = entityViewBuilders[i];
                Type entityViewType = entityViewBuilder.GetEntityViewType();

                ITypeSafeList fromSafeList = dictionary[entityViewType];
                ITypeSafeList toSafeList;

                if (groupedEntityViewsTyped.TryGetValue(entityViewType, out toSafeList) == false)
                {
                    toSafeList = fromSafeList.Create();
                }

                entityViewBuilder.MoveEntityView(entityID, fromSafeList, toSafeList);

                if (fromSafeList.UnorderedRemove(entityID) == false)
                {
                    dictionary.Remove(entityViewType);
                }
            }

            if (dictionary.Count == 0)
            {
                _groupEntityViewsDB.Remove(fromGroupID);
            }
        }