Exemplo n.º 1
0
        public void SetEntityParent(string entityId, string parentId)
        {
            if (entityId == parentId)
            {
                return;
            }

            IDCLEntity me = GetEntityForUpdate(entityId);

            if (me == null)
            {
                return;
            }

            if (parentId == "FirstPersonCameraEntityReference" || parentId == "PlayerEntityReference") // PlayerEntityReference is for compatibility purposes
            {
                // In this case, the entity will attached to the first person camera
                // On first person mode, the entity will rotate with the camera. On third person mode, the entity will rotate with the avatar
                me.SetParent(DCLCharacterController.i.firstPersonCameraReference);
                Environment.i.world.sceneBoundsChecker.AddPersistent(me);
            }
            else if (parentId == "AvatarEntityReference" || parentId == "AvatarPositionEntityReference") // AvatarPositionEntityReference is for compatibility purposes
            {
                // In this case, the entity will be attached to the avatar
                // It will simply rotate with the avatar, regardless of where the camera is pointing
                me.SetParent(DCLCharacterController.i.avatarReference);
                Environment.i.world.sceneBoundsChecker.AddPersistent(me);
            }
            else
            {
                if (me.parent == DCLCharacterController.i.firstPersonCameraReference || me.parent == DCLCharacterController.i.avatarReference)
                {
                    Environment.i.world.sceneBoundsChecker.RemoveEntityToBeChecked(me);
                }

                if (parentId == "0")
                {
                    // The entity will be child of the scene directly
                    me.SetParent(null);
                    me.gameObject.transform.SetParent(gameObject.transform, false);
                }
                else
                {
                    IDCLEntity myParent = GetEntityForUpdate(parentId);

                    if (myParent != null)
                    {
                        me.SetParent(myParent);
                    }
                }
            }

            Environment.i.platform.cullingController.MarkDirty();
            Environment.i.platform.physicsSyncController.MarkDirty();
        }
Exemplo n.º 2
0
        public static IDCLEntity DuplicateEntity(ParcelScene scene, IDCLEntity entity)
        {
            if (!scene.entities.ContainsKey(entity.entityId))
            {
                return(null);
            }

            IDCLEntity newEntity = scene.CreateEntity(System.Guid.NewGuid().ToString());

            if (entity.children.Count > 0)
            {
                using (var iterator = entity.children.GetEnumerator())
                {
                    while (iterator.MoveNext())
                    {
                        IDCLEntity childDuplicate = DuplicateEntity(scene, iterator.Current.Value);
                        childDuplicate.SetParent(newEntity);
                    }
                }
            }

            if (entity.parent != null)
            {
                scene.SetEntityParent(newEntity.entityId, entity.parent.entityId);
            }

            DCLTransform.model.position = WorldStateUtils.ConvertUnityToScenePosition(entity.gameObject.transform.position);
            DCLTransform.model.rotation = entity.gameObject.transform.rotation;
            DCLTransform.model.scale    = entity.gameObject.transform.lossyScale;

            foreach (KeyValuePair <CLASS_ID_COMPONENT, IEntityComponent> component in entity.components)
            {
                scene.EntityComponentCreateOrUpdateWithModel(newEntity.entityId, component.Key, component.Value.GetModel());
            }

            foreach (KeyValuePair <System.Type, ISharedComponent> component in entity.sharedComponents)
            {
                ISharedComponent sharedComponent = scene.SharedComponentCreate(System.Guid.NewGuid().ToString(), component.Value.GetClassId());
                sharedComponent.UpdateFromModel(component.Value.GetModel());
                scene.SharedComponentAttach(newEntity.entityId, sharedComponent.id);
            }

            return(newEntity);
        }
        public void ForceCleanup()
        {
            while (disposableComponentsMarkedForCleanup.Count > 0)
            {
                MarkedSharedComponentInfo markedSharedComponentInfo = disposableComponentsMarkedForCleanup.Dequeue();
                markedSharedComponentInfo.scene.SharedComponentDispose(markedSharedComponentInfo.componentId);
            }

            HashSet <ParcelScene> scenesToRemove = new HashSet <ParcelScene>();

            // If we have root entities queued for removal, we call Parcel Scene's RemoveEntity()
            // so that the child entities end up recursively in the entitiesMarkedForCleanup queue
            while (rootEntitiesMarkedForCleanup.Count > 0)
            {
                MarkedEntityInfo markedEntityInfo = rootEntitiesMarkedForCleanup.Dequeue();
                markedEntityInfo.scene.RemoveEntity(markedEntityInfo.entity.entityId, false);

                if (!scenesToRemove.Contains(markedEntityInfo.scene))
                {
                    scenesToRemove.Add(markedEntityInfo.scene);
                }
            }

            while (entitiesMarkedForCleanup.Count > 0)
            {
                IDCLEntity entity = entitiesMarkedForCleanup.Dequeue();
                entity.SetParent(null);
                entity.Cleanup();
            }

            foreach (var scene in scenesToRemove)
            {
                if (scene != null && !Environment.i.world.state.loadedScenes.ContainsKey(scene.sceneData.id))
                {
                    Object.Destroy(scene.gameObject);
                }
            }
        }
        IEnumerator CleanupEntitiesCoroutine()
        {
            while (true)
            {
                float lastTime = Time.unscaledTime;

                while (disposableComponentsMarkedForCleanup.Count > 0)
                {
                    MarkedSharedComponentInfo markedSharedComponentInfo = disposableComponentsMarkedForCleanup.Dequeue();
                    markedSharedComponentInfo.scene.SharedComponentDispose(markedSharedComponentInfo.componentId);

                    if (DCLTime.realtimeSinceStartup - lastTime >= MAX_TIME_BUDGET)
                    {
                        yield return(null);

                        lastTime = Time.unscaledTime;
                    }
                }

                HashSet <ParcelScene> scenesToRemove = new HashSet <ParcelScene>();

                // If we have root entities queued for removal, we call Parcel Scene's RemoveEntity()
                // so that the child entities end up recursively in the entitiesMarkedForCleanup queue
                while (rootEntitiesMarkedForCleanup.Count > 0)
                {
                    MarkedEntityInfo markedEntityInfo = rootEntitiesMarkedForCleanup.Dequeue();
                    markedEntityInfo.scene.RemoveEntity(markedEntityInfo.entity.entityId, false);

                    if (!scenesToRemove.Contains(markedEntityInfo.scene))
                    {
                        scenesToRemove.Add(markedEntityInfo.scene);
                    }

                    if (DCLTime.realtimeSinceStartup - lastTime >= MAX_TIME_BUDGET)
                    {
                        yield return(null);

                        lastTime = Time.unscaledTime;
                    }
                }

                while (entitiesMarkedForCleanup.Count > 0)
                {
                    IDCLEntity entity = entitiesMarkedForCleanup.Dequeue();
                    entity.SetParent(null);
                    entity.Cleanup();

                    if (DCLTime.realtimeSinceStartup - lastTime >= MAX_TIME_BUDGET)
                    {
                        yield return(null);

                        lastTime = Time.unscaledTime;
                    }
                }

                foreach (var scene in scenesToRemove)
                {
                    if (scene != null && !Environment.i.world.state.loadedScenes.ContainsKey(scene.sceneData.id))
                    {
                        Object.Destroy(scene.gameObject);

                        if (DCLTime.realtimeSinceStartup - lastTime >= MAX_TIME_BUDGET)
                        {
                            yield return(null);

                            lastTime = Time.unscaledTime;
                        }
                    }
                }

                yield return(null);
            }
        }