Beispiel #1
0
        // HACK: (Zak) will be removed when we separate each
        // uuid component as a different class id
        public UUIDComponent EntityUUIDComponentUpdate(DecentralandEntity entity, string type, UUIDComponent.Model model)
        {
            if (entity == null)
            {
                Debug.LogError($"Can't update the {type} uuid component of a nonexistent entity!", this);
                return(null);
            }

            if (!entity.uuidComponents.ContainsKey(type))
            {
                Debug.LogError($"Entity {entity.entityId} doesn't have a {type} uuid component to update!", this);
                return(null);
            }

            UUIDComponent targetComponent = entity.uuidComponents[type];

            targetComponent.Setup(this, entity, model);

            return(targetComponent);
        }
Beispiel #2
0
        public BaseComponent EntityComponentCreateOrUpdate(string entityId, string name, int classIdNum, string data, out CleanableYieldInstruction yieldInstruction)
        {
            yieldInstruction = null;

            SceneController.i.OnMessageDecodeStart?.Invoke("UpdateEntityComponent");
            createEntityComponentMessage.name     = name;
            createEntityComponentMessage.classId  = classIdNum;
            createEntityComponentMessage.entityId = entityId;
            createEntityComponentMessage.json     = data;

            SceneController.i.OnMessageDecodeEnds?.Invoke("UpdateEntityComponent");

            DecentralandEntity entity = GetEntityForUpdate(createEntityComponentMessage.entityId);

            if (entity == null)
            {
                Debug.LogError($"scene '{sceneData.id}': Can't create entity component if the entity {createEntityComponentMessage.entityId} doesn't exist!");
                return(null);
            }

            CLASS_ID_COMPONENT classId = (CLASS_ID_COMPONENT)createEntityComponentMessage.classId;

            if (classId == CLASS_ID_COMPONENT.TRANSFORM)
            {
                MessageDecoder.DecodeTransform(data, ref DCLTransform.model);

                if (entity.OnTransformChange != null)
                {
                    entity.OnTransformChange.Invoke(DCLTransform.model);
                }
                else
                {
                    entity.gameObject.transform.localPosition = DCLTransform.model.position;
                    entity.gameObject.transform.localRotation = DCLTransform.model.rotation;
                    entity.gameObject.transform.localScale    = DCLTransform.model.scale;

                    SceneController.i.boundariesChecker?.AddEntityToBeChecked(entity);
                }

                return(null);
            }

            BaseComponent       newComponent = null;
            DCLComponentFactory factory      = ownerController.componentFactory;

            Assert.IsNotNull(factory, "Factory is null?");

            // HACK: (Zak) will be removed when we separate each
            // uuid component as a different class id
            if (classId == CLASS_ID_COMPONENT.UUID_CALLBACK)
            {
                string type = "";

                OnPointerEvent.Model model = JsonUtility.FromJson <OnPointerEvent.Model>(createEntityComponentMessage.json);

                type = model.type;

                if (!entity.uuidComponents.ContainsKey(type))
                {
                    //NOTE(Brian): We have to contain it in a gameObject or it will be pooled with the components attached.
                    var go = new GameObject("UUID Component");
                    go.transform.SetParent(entity.gameObject.transform, false);

                    switch (type)
                    {
                    case OnClick.NAME:
                        newComponent = Utils.GetOrCreateComponent <OnClick>(go);
                        break;

                    case OnPointerDown.NAME:
                        newComponent = Utils.GetOrCreateComponent <OnPointerDown>(go);
                        break;

                    case OnPointerUp.NAME:
                        newComponent = Utils.GetOrCreateComponent <OnPointerUp>(go);
                        break;
                    }

                    if (newComponent != null)
                    {
                        UUIDComponent uuidComponent = newComponent as UUIDComponent;

                        if (uuidComponent != null)
                        {
                            uuidComponent.Setup(this, entity, model);
                            entity.uuidComponents.Add(type, uuidComponent);
                        }
                        else
                        {
                            Debug.LogError("uuidComponent is not of UUIDComponent type!");
                        }
                    }
                    else
                    {
                        Debug.LogError("EntityComponentCreateOrUpdate: Invalid UUID type!");
                    }
                }
                else
                {
                    newComponent = EntityUUIDComponentUpdate(entity, type, model);
                }
            }
            else
            {
                if (!entity.components.ContainsKey(classId))
                {
                    newComponent = factory.CreateItemFromId <BaseComponent>(classId);

                    if (newComponent != null)
                    {
                        newComponent.scene  = this;
                        newComponent.entity = entity;

                        entity.components.Add(classId, newComponent);

                        newComponent.transform.SetParent(entity.gameObject.transform, false);
                        newComponent.UpdateFromJSON(createEntityComponentMessage.json);
                    }
                }
                else
                {
                    newComponent = EntityComponentUpdate(entity, classId, createEntityComponentMessage.json);
                }
            }

            if (newComponent != null && newComponent.isRoutineRunning)
            {
                yieldInstruction = newComponent.yieldInstruction;
            }

            return(newComponent);
        }
Beispiel #3
0
        public BaseComponent EntityComponentCreateOrUpdateFromUnity(string entityId, CLASS_ID_COMPONENT classId, object data)
        {
            DecentralandEntity entity = GetEntityForUpdate(entityId);

            if (entity == null)
            {
                Debug.LogError($"scene '{sceneData.id}': Can't create entity component if the entity {entityId} doesn't exist!");
                return(null);
            }

            if (classId == CLASS_ID_COMPONENT.TRANSFORM)
            {
                if (!(data is DCLTransform.Model))
                {
                    Debug.LogError("Data is not a DCLTransform.Model type!");
                    return(null);
                }

                DCLTransform.Model modelRecovered = (DCLTransform.Model)data;

                if (!entity.components.ContainsKey(classId))
                {
                    entity.components.Add(classId, null);
                }


                if (entity.OnTransformChange != null)
                {
                    entity.OnTransformChange.Invoke(modelRecovered);
                }
                else
                {
                    entity.gameObject.transform.localPosition = modelRecovered.position;
                    entity.gameObject.transform.localRotation = modelRecovered.rotation;
                    entity.gameObject.transform.localScale    = modelRecovered.scale;

                    Environment.i.world.sceneBoundsChecker?.AddEntityToBeChecked(entity);
                }

                Environment.i.platform.physicsSyncController.MarkDirty();
                Environment.i.platform.cullingController.MarkDirty();
                return(null);
            }

            BaseComponent            newComponent = null;
            IRuntimeComponentFactory factory      = ownerController.componentFactory;

            Assert.IsNotNull(factory, "Factory is null?");

            if (classId == CLASS_ID_COMPONENT.UUID_CALLBACK)
            {
                string type = "";
                if (!(data is OnPointerEvent.Model))
                {
                    Debug.LogError("Data is not a DCLTransform.Model type!");
                    return(null);
                }

                OnPointerEvent.Model model = (OnPointerEvent.Model)data;

                type = model.type;

                if (!entity.uuidComponents.ContainsKey(type))
                {
                    //NOTE(Brian): We have to contain it in a gameObject or it will be pooled with the components attached.
                    var go = new GameObject("UUID Component");
                    go.transform.SetParent(entity.gameObject.transform, false);

                    switch (type)
                    {
                    case OnClick.NAME:
                        newComponent = go.GetOrCreateComponent <OnClick>();
                        break;

                    case OnPointerDown.NAME:
                        newComponent = go.GetOrCreateComponent <OnPointerDown>();
                        break;

                    case OnPointerUp.NAME:
                        newComponent = go.GetOrCreateComponent <OnPointerUp>();
                        break;
                    }

                    if (newComponent != null)
                    {
                        UUIDComponent uuidComponent = newComponent as UUIDComponent;

                        if (uuidComponent != null)
                        {
                            uuidComponent.Setup(this, entity, model);
                            entity.uuidComponents.Add(type, uuidComponent);
                        }
                        else
                        {
                            Debug.LogError("uuidComponent is not of UUIDComponent type!");
                        }
                    }
                    else
                    {
                        Debug.LogError("EntityComponentCreateOrUpdate: Invalid UUID type!");
                    }
                }
                else
                {
                    newComponent = EntityUUIDComponentUpdate(entity, type, model);
                }
            }
            else
            {
                if (!entity.components.ContainsKey(classId))
                {
                    newComponent = factory.CreateItemFromId <BaseComponent>(classId);

                    if (newComponent != null)
                    {
                        newComponent.scene  = this;
                        newComponent.entity = entity;

                        entity.components.Add(classId, newComponent);

                        newComponent.transform.SetParent(entity.gameObject.transform, false);
                        newComponent.UpdateFromJSON((string)data);
                    }
                }
                else
                {
                    newComponent = EntityComponentUpdate(entity, classId, (string)data);
                }
            }

            Environment.i.platform.physicsSyncController.MarkDirty();
            Environment.i.platform.cullingController.MarkDirty();
            return(newComponent);
        }