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); }
private void RemoveComponentType <T>(DecentralandEntity entity, CLASS_ID_COMPONENT classId) where T : MonoBehaviour { var component = entity.components[classId].GetComponent <T>(); if (component != null) { Utils.SafeDestroy(component); } }
public static Coroutine EntityComponentUpdate <T, K>(T component, K model = null) where T : BaseComponent where K : class, new() { if (model == null) { model = new K(); } CLASS_ID_COMPONENT classId = component.scene.ownerController.componentFactory.GetIdForType <T>(); component.scene.EntityComponentUpdate(component.entity, classId, JsonUtility.ToJson(model)); return(component.routine); }
private void RemoveComponentType <T>(IDCLEntity entity, CLASS_ID_COMPONENT classId) where T : MonoBehaviour { var component = entity.components[classId] as IEntityComponent; if (component == null) { return; } var monoBehaviour = component.GetTransform().GetComponent <T>(); if (monoBehaviour != null) { Utils.SafeDestroy(monoBehaviour); } }
public ItemType CreateItemFromId <ItemType>(CLASS_ID_COMPONENT id) { EnsueFactoryDictionary(); if (!factoryDict.ContainsKey(id)) { #if UNITY_EDITOR Debug.LogError("Class " + id + " can't be instantiated because the field doesn't exist!"); #endif return(default(ItemType)); } if (factoryDict[id].prefab == null) { Debug.LogError("Prefab for class " + id + " is null!"); return(default(ItemType)); } return(Instantiate(factoryDict[id].prefab).GetComponent <ItemType>()); }
// The EntityComponentUpdate() parameters differ from other similar methods because there is no EntityComponentUpdate protocol message yet. public BaseComponent EntityComponentUpdate(DecentralandEntity entity, CLASS_ID_COMPONENT classId, string componentJson) { if (entity == null) { Debug.LogError($"Can't update the {classId} component of a nonexistent entity!", this); return(null); } if (!entity.components.ContainsKey(classId)) { Debug.LogError($"Entity {entity.entityId} doesn't have a {classId} component to update!", this); return(null); } BaseComponent targetComponent = entity.components[classId]; targetComponent.UpdateFromJSON(componentJson); return(targetComponent); }
public ItemType CreateItemFromId <ItemType>(CLASS_ID_COMPONENT id) where ItemType : BaseComponent { EnsureFactoryDictionary(); if (!factoryDict.ContainsKey(id)) { #if UNITY_EDITOR Debug.LogError("Class " + id + " can't be instantiated because the field doesn't exist!"); #endif return(default(ItemType)); } var factoryItem = factoryDict[id]; if (factoryItem.prefab == null) { Debug.LogError("Prefab for class " + id + " is null!"); return(default(ItemType)); } GameObject instancedGo; PoolableObject poolableObject = null; if (factoryItem.usePool) { EnsurePoolForItem(factoryItem); poolableObject = GetPoolForItem(factoryItem).Get(); instancedGo = poolableObject.gameObject; } else { instancedGo = Instantiate(factoryItem.prefab.gameObject); } ItemType item = instancedGo.GetComponent <ItemType>(); item.poolableObject = poolableObject; return(item); }
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); }
public bool TryGetBaseComponent(CLASS_ID_COMPONENT componentId, out IEntityComponent component) { return(components.TryGetValue(componentId, out component)); }
public IEntityComponent EntityComponentCreateOrUpdate(string entityId, CLASS_ID_COMPONENT classId, string data) { return(EntityComponentCreateOrUpdateWithModel(entityId, classId, data)); }
public IEntityComponent EntityComponentCreateOrUpdateWithModel(string entityId, CLASS_ID_COMPONENT classId, object data) { IDCLEntity 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); } IEntityComponent newComponent = null; if (classId == CLASS_ID_COMPONENT.UUID_CALLBACK) { OnPointerEvent.Model model = JsonUtility.FromJson <OnPointerEvent.Model>(data as string); classId = model.GetClassIdFromType(); } if (!entity.components.ContainsKey(classId)) { var factory = Environment.i.world.componentFactory; newComponent = factory.CreateComponent((int)classId) as IEntityComponent; if (newComponent != null) { entity.components.Add(classId, newComponent); OnComponentAdded?.Invoke(newComponent); newComponent.Initialize(this, entity); if (data is string json) { newComponent.UpdateFromJSON(json); } else { newComponent.UpdateFromModel(data as BaseModel); } } } else { newComponent = EntityComponentUpdate(entity, classId, data as string); } if (newComponent != null && newComponent is IOutOfSceneBoundariesHandler) { Environment.i.world.sceneBoundsChecker?.AddEntityToBeChecked(entity); } OnChanged?.Invoke(); Environment.i.platform.physicsSyncController.MarkDirty(); Environment.i.platform.cullingController.MarkDirty(); return(newComponent); }
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); }