Ejemplo n.º 1
0
        public static T EntityComponentCreate <T, K>(ParcelScene scene, DecentralandEntity entity, K model,
                                                     CLASS_ID_COMPONENT classId = CLASS_ID_COMPONENT.NONE)
            where T : BaseComponent
            where K : new()
        {
            int componentClassId = classId == CLASS_ID_COMPONENT.NONE
                ? (int)scene.ownerController.componentFactory.GetIdForType <T>()
                : (int)classId;

            string componentInstanceId = GetComponentUniqueId(scene, typeof(T).Name, componentClassId, entity.entityId);

            string data;

            if (classId == CLASS_ID_COMPONENT.TRANSFORM)
            {
                PB_Transform transf = GetPBTransformFromModelJson(JsonUtility.ToJson(model));
                data = System.Convert.ToBase64String(transf.ToByteArray());
            }
            else
            {
                data = JsonUtility.ToJson(model);
            }

            return(scene.EntityComponentCreateOrUpdate(
                       entity.entityId,
                       (CLASS_ID_COMPONENT)componentClassId,
                       data
                       , out _) as T);
        }
Ejemplo n.º 2
0
        public static void SetEntityTransform(ParcelScene scene, DecentralandEntity entity, Vector3 position, Quaternion rotation, Vector3 scale)
        {
            PB_Transform pB_Transform = GetPBTransform(position, rotation, scale);

            scene.EntityComponentCreateOrUpdate(
                entity.entityId,
                CLASS_ID_COMPONENT.TRANSFORM,
                System.Convert.ToBase64String(pB_Transform.ToByteArray())
                , out CleanableYieldInstruction routine);
        }
Ejemplo n.º 3
0
        void UpdateEntityTransform(ParcelScene scene, string entityId, Vector3 position, Quaternion rotation, Vector3 scale)
        {
            PB_Transform pB_Transform = GetPBTransform(position, rotation, scale);

            scene.EntityComponentCreateOrUpdate(
                entityId,
                CLASS_ID_COMPONENT.TRANSFORM,
                System.Convert.ToBase64String(pB_Transform.ToByteArray())
                );
        }
Ejemplo n.º 4
0
        private void ProcessMessage(ParcelScene scene, string method, object msgPayload,
                                    out CustomYieldInstruction yieldInstruction)
        {
            yieldInstruction = null;
            IDelayedComponent delayedComponent = null;

            try
            {
                switch (method)
                {
                case MessagingTypes.ENTITY_CREATE:
                {
                    if (msgPayload is Protocol.CreateEntity payload)
                    {
                        scene.CreateEntity(payload.entityId);
                    }

                    break;
                }

                case MessagingTypes.ENTITY_REPARENT:
                {
                    if (msgPayload is Protocol.SetEntityParent payload)
                    {
                        scene.SetEntityParent(payload.entityId, payload.parentId);
                    }

                    break;
                }

                case MessagingTypes.ENTITY_COMPONENT_CREATE_OR_UPDATE:
                {
                    if (msgPayload is Protocol.EntityComponentCreateOrUpdate payload)
                    {
                        delayedComponent = scene.EntityComponentCreateOrUpdate(payload.entityId,
                                                                               (CLASS_ID_COMPONENT)payload.classId, payload.json) as IDelayedComponent;
                    }

                    break;
                }

                case MessagingTypes.ENTITY_COMPONENT_DESTROY:
                {
                    if (msgPayload is Protocol.EntityComponentDestroy payload)
                    {
                        scene.EntityComponentRemove(payload.entityId, payload.name);
                    }

                    break;
                }

                case MessagingTypes.SHARED_COMPONENT_ATTACH:
                {
                    if (msgPayload is Protocol.SharedComponentAttach payload)
                    {
                        scene.SharedComponentAttach(payload.entityId, payload.id);
                    }

                    break;
                }

                case MessagingTypes.SHARED_COMPONENT_CREATE:
                {
                    if (msgPayload is Protocol.SharedComponentCreate payload)
                    {
                        scene.SharedComponentCreate(payload.id, payload.classId);
                    }

                    break;
                }

                case MessagingTypes.SHARED_COMPONENT_DISPOSE:
                {
                    if (msgPayload is Protocol.SharedComponentDispose payload)
                    {
                        scene.SharedComponentDispose(payload.id);
                    }
                    break;
                }

                case MessagingTypes.SHARED_COMPONENT_UPDATE:
                {
                    if (msgPayload is Protocol.SharedComponentUpdate payload)
                    {
                        delayedComponent = scene.SharedComponentUpdate(payload.componentId, payload.json) as IDelayedComponent;
                    }

                    break;
                }

                case MessagingTypes.ENTITY_DESTROY:
                {
                    if (msgPayload is Protocol.RemoveEntity payload)
                    {
                        scene.RemoveEntity(payload.entityId);
                    }
                    break;
                }

                case MessagingTypes.INIT_DONE:
                {
                    scene.sceneLifecycleHandler.SetInitMessagesDone();
                    break;
                }

                case MessagingTypes.QUERY:
                {
                    if (msgPayload is QueryMessage queryMessage)
                    {
                        ParseQuery(queryMessage.payload, scene.sceneData.id);
                    }
                    break;
                }

                case MessagingTypes.OPEN_EXTERNAL_URL:
                {
                    if (msgPayload is Protocol.OpenExternalUrl payload)
                    {
                        OnOpenExternalUrlRequest?.Invoke(scene, payload.url);
                    }
                    break;
                }

                case MessagingTypes.OPEN_NFT_DIALOG:
                {
                    if (msgPayload is Protocol.OpenNftDialog payload)
                    {
                        DataStore.i.onOpenNFTPrompt.Set(new NFTPromptModel(payload.contactAddress, payload.tokenId,
                                                                           payload.comment), true);
                    }
                    break;
                }

                default:
                    Debug.LogError($"Unknown method {method}");
                    break;
                }
            }
            catch (Exception e)
            {
                throw new Exception(
                          $"Scene message error. scene: {scene.sceneData.id} method: {method} payload: {JsonUtility.ToJson(msgPayload)} {e}");
            }

            if (delayedComponent != null)
            {
                if (delayedComponent.isRoutineRunning)
                {
                    yieldInstruction = delayedComponent.yieldInstruction;
                }
            }
        }
    public IEnumerator Initialize()
    {
        var sceneController = Environment.i.world.sceneController;

        DCLCharacterController.i.gravity = 0;
        DCLCharacterController.i.Teleport(JsonConvert.SerializeObject(new
        {
            x = 0f,
            y = 0f,
            z = 0f
        }));

        var scenesToLoad = new LoadParcelScenesMessage.UnityParcelScene()
        {
            id           = sceneName,
            basePosition = new Vector2Int(3, 3),
            parcels      = new[]
            {
                new Vector2Int(3, 3),
                new Vector2Int(3, 4)
            },
            baseUrl = "http://localhost:9991/local-ipfs/contents/"
        };

        Assert.IsTrue(sceneController != null, "Cannot find SceneController");

        sceneController.UnloadAllScenes();
        sceneController.LoadParcelScenes(JsonConvert.SerializeObject(scenesToLoad));

        yield return(new WaitForAllMessagesProcessed());

        ParcelScene scene = Environment.i.world.state.GetScene(sceneName) as ParcelScene;

        //NOTE(Brian): This is making my eyes bleed.
        sceneController.SendSceneMessage(
            TestHelpers.CreateSceneMessage(
                sceneName,
                entityId,
                "CreateEntity",
                JsonConvert.SerializeObject(
                    new Protocol.CreateEntity()
        {
            entityId = entityId
        }))
            );

        //NOTE(Brian): This is making my eyes bleed. (Zak): Twice
        sceneController.SendSceneMessage(
            TestHelpers.CreateSceneMessage(
                sceneName,
                entityId,
                "SetEntityParent",
                JsonConvert.SerializeObject(
                    new
        {
            entityId = entityId,
            parentId = "0"
        })
                )
            );

        yield return(new WaitForAllMessagesProcessed());

        Assert.IsTrue(scene.entities[entityId].meshRootGameObject == null, "meshGameObject must be null");

        // 1st message
        TestHelpers.CreateAndSetShape(scene as ParcelScene, entityId, CLASS_ID.BOX_SHAPE, "{}");

        {
            scene.EntityComponentCreateOrUpdate(
                entityId,
                CLASS_ID_COMPONENT.TRANSFORM,
                "{\"tag\":\"transform\",\"position\":{\"x\":0,\"y\":0,\"z\":0},\"rotation\":{\"x\":0,\"y\":0,\"z\":0,\"w\":1},\"scale\":{\"x\":1,\"y\":1,\"z\":1}}"
                );
        }


        // 2nd message
        TestHelpers.CreateAndSetShape(scene, entityId, CLASS_ID.BOX_SHAPE, "{}");
        {
            scene.EntityComponentCreateOrUpdate(
                entityId,
                CLASS_ID_COMPONENT.TRANSFORM,
                "{\"tag\":\"transform\",\"position\":{\"x\":6,\"y\":0,\"z\":5},\"rotation\":{\"x\":0,\"y\":0.39134957508996265,\"z\":0,\"w\":0.9202420931897769},\"scale\":{\"x\":1,\"y\":1,\"z\":1}}"
                );
        }

        TestHelpers.InstantiateEntityWithTextShape(scene, new Vector3(10, 10, 10),
                                                   new TextShape.Model()
        {
            value = "Hello World!!!"
        });
    }