예제 #1
0
        ///--------------------------------------------
        ///
        void MoveEntity(IEntityBuilder[] entityBuilders, EGID entityGID, Type originalDescriptorType, EGID toEntityGID,
                        Dictionary <Type, ITypeSafeDictionary> toGroup = null)
        {
            var profiler = new PlatformProfiler();

            using (profiler.StartNewSession("Move Entity"))
            {
                //for each entity view generated by the entity descriptor
                Dictionary <Type, ITypeSafeDictionary> fromGroup;

                if (_groupEntityDB.TryGetValue(entityGID.groupID, out fromGroup) == false)
                {
                    throw new ECSException("from group not found eid: "
                                           .FastConcat(entityGID.entityID).FastConcat(" group: ")
                                           .FastConcat(entityGID.groupID));
                }

                ITypeSafeDictionary entityInfoViewDic;
                EntityInfoView      entityInfoView = default;

                //Check if there is an EntityInfoView linked to this entity, if so it's a DynamicEntityDescriptor!
                bool correctEntityDescriptorFound = true;
                if (fromGroup.TryGetValue(_entityInfoView, out entityInfoViewDic) &&
                    (entityInfoViewDic as TypeSafeDictionary <EntityInfoView>).TryGetValue
                        (entityGID.entityID, out entityInfoView) &&
                    (correctEntityDescriptorFound = entityInfoView.type == originalDescriptorType))
                {
                    var entitiesToMove = entityInfoView.entitiesToBuild;

                    for (int i = 0; i < entitiesToMove.Length; i++)
                    {
                        MoveEntityView(entityGID, toEntityGID, toGroup, fromGroup, entitiesToMove[i].GetEntityType(), profiler);
                    }
                }
                //otherwise it's a normal static entity descriptor
                else
                {
#if DEBUG && !PROFILER
                    if (correctEntityDescriptorFound == false)
#if RELAXED_ECS
                    { Console.LogError(INVALID_DYNAMIC_DESCRIPTOR_ERROR
                                       .FastConcat(" ID ").FastConcat(entityGID.entityID)
                                       .FastConcat(" group ID ").FastConcat(entityGID.groupID).FastConcat(
                                           " descriptor found: ", entityInfoView.type.Name,
                                           " descriptor Excepted ", originalDescriptorType.Name)); }
#else
                    { throw new ECSException(INVALID_DYNAMIC_DESCRIPTOR_ERROR.FastConcat(" ID ").FastConcat(entityGID.entityID)
                                             .FastConcat(" group ID ").FastConcat(entityGID.groupID).FastConcat(
                                                 " descriptor found: ", entityInfoView.type.Name, " descriptor Excepted ",
                                                 originalDescriptorType.Name)); }
#endif
#endif

                    for (var i = 0; i < entityBuilders.Length; i++)
                    {
                        MoveEntityView(entityGID, toEntityGID, toGroup, fromGroup, entityBuilders[i].GetEntityType(), profiler);
                    }
                }
            }
        }
        ///--------------------------------------------
        ///
        void MoveEntity(IEntityBuilder[] entityBuilders, EGID fromEntityGID, Type originalDescriptorType, EGID?toEntityGID)
        {
            using (var sampler = new PlatformProfiler("Move Entity"))
            {
                //for each entity view generated by the entity descriptor
                if (_groupEntityDB.TryGetValue(fromEntityGID.groupID, out var fromGroup) == false)
                {
                    throw new ECSException("from group not found eid: ".FastConcat(fromEntityGID.entityID)
                                           .FastConcat(" group: ").FastConcat(fromEntityGID.groupID));
                }

                //Check if there is an EntityInfoView linked to this entity, if so it's a DynamicEntityDescriptor!
#if DEBUG && !PROFILER
                bool correctEntityDescriptorFound = true;
#endif
                EntityStructInfoView entityInfoView = default;
                if (fromGroup.TryGetValue(ENTITY_INFO_VIEW_TYPE, out var entityInfoViewDic) &&
                    (entityInfoViewDic as TypeSafeDictionary <EntityStructInfoView>).TryGetValue(
                        fromEntityGID.entityID, out entityInfoView) &&
                    (
#if DEBUG && !PROFILER
                        correctEntityDescriptorFound =
#endif
                        entityInfoView.type == originalDescriptorType))
                {
                    var entitiesToMove = entityInfoView.entitiesToBuild;

                    Dictionary <Type, ITypeSafeDictionary> toGroup = null;

                    if (toEntityGID != null)
                    {
                        var toGroupID = toEntityGID.Value.groupID;

                        if (_groupEntityDB.TryGetValue(toGroupID, out toGroup) == false)
                        {
                            toGroup = _groupEntityDB[toGroupID] = new Dictionary <Type, ITypeSafeDictionary>();
                        }
                    }

                    for (int i = 0; i < entitiesToMove.Length; i++)
                    {
                        MoveEntityView(fromEntityGID, toEntityGID, toGroup, ref fromGroup,
                                       entitiesToMove[i].GetEntityType(), sampler);
                    }
                }
                //otherwise it's a normal static entity descriptor
                else
                {
#if DEBUG && !PROFILER
                    if (correctEntityDescriptorFound == false)
                    {
                        throw new ECSException(INVALID_DYNAMIC_DESCRIPTOR_ERROR.FastConcat(" ID ").FastConcat(fromEntityGID.entityID)
                                               .FastConcat(" group ID ").FastConcat(fromEntityGID.groupID).FastConcat(
                                                   " descriptor found: ", entityInfoView.type.Name, " descriptor Excepted ",
                                                   originalDescriptorType.Name));
                    }
#endif

                    Dictionary <Type, ITypeSafeDictionary> toGroup = null;
                    if (toEntityGID != null)
                    {
                        var toGroupID = toEntityGID.Value.groupID;

                        if (_groupEntityDB.TryGetValue(toGroupID, out toGroup) == false)
                        {
                            toGroup = _groupEntityDB[toGroupID] = new Dictionary <Type, ITypeSafeDictionary>();
                        }
                    }

                    for (var i = 0; i < entityBuilders.Length; i++)
                    {
                        MoveEntityView(fromEntityGID, toEntityGID, toGroup, ref fromGroup,
                                       entityBuilders[i].GetEntityType(), sampler);
                    }
                }
            }
        }