| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace DCL.Models |
| | 10 | | { |
| | 11 | | [Serializable] |
| | 12 | | public class DecentralandEntity : IDCLEntity |
| | 13 | | { |
| 1936 | 14 | | public IParcelScene scene { get; set; } |
| 71 | 15 | | public bool markedForCleanup { get; set; } = false; |
| 110 | 16 | | public bool isInsideBoundaries { get; set; } = false; |
| | 17 | |
|
| 1940 | 18 | | public Dictionary<string, IDCLEntity> children { get; private set; } = new Dictionary<string, IDCLEntity>(); |
| 515 | 19 | | public IDCLEntity parent { get; private set; } |
| | 20 | |
|
| 2754 | 21 | | public Dictionary<CLASS_ID_COMPONENT, IEntityComponent> components { get; private set; } = new Dictionary<CLASS_ |
| 1840 | 22 | | public Dictionary<System.Type, ISharedComponent> sharedComponents { get; private set; } = new Dictionary<System. |
| | 23 | |
|
| 4811 | 24 | | public GameObject gameObject { get; set; } |
| 2029 | 25 | | public string entityId { get; set; } |
| 5093 | 26 | | public MeshesInfo meshesInfo { get; set; } |
| 3014 | 27 | | public GameObject meshRootGameObject => meshesInfo.meshRootGameObject; |
| 6 | 28 | | public Renderer[] renderers => meshesInfo.renderers; |
| | 29 | |
|
| 1422 | 30 | | public System.Action<IDCLEntity> OnShapeUpdated { get; set; } |
| 129 | 31 | | public System.Action<IDCLEntity> OnShapeLoaded { get; set; } |
| 215 | 32 | | public System.Action<object> OnNameChange { get; set; } |
| 273 | 33 | | public System.Action<object> OnTransformChange { get; set; } |
| 1414 | 34 | | public System.Action<IDCLEntity> OnRemoved { get; set; } |
| 0 | 35 | | public System.Action<IDCLEntity> OnMeshesInfoUpdated { get; set; } |
| 0 | 36 | | public System.Action<IDCLEntity> OnMeshesInfoCleaned { get; set; } |
| | 37 | |
|
| 1858 | 38 | | public System.Action<ICleanableEventDispatcher> OnCleanupEvent { get; set; } |
| | 39 | |
|
| | 40 | | const string MESH_GAMEOBJECT_NAME = "Mesh"; |
| | 41 | |
|
| | 42 | | bool isReleased = false; |
| | 43 | |
|
| 1730 | 44 | | public DecentralandEntity() |
| | 45 | | { |
| 1730 | 46 | | meshesInfo = new MeshesInfo(); |
| 2151 | 47 | | OnShapeUpdated += (entity) => meshesInfo.UpdateRenderersCollection(); |
| 2362 | 48 | | meshesInfo.OnUpdated += () => OnMeshesInfoUpdated?.Invoke(this); |
| 1810 | 49 | | meshesInfo.OnCleanup += () => OnMeshesInfoCleaned?.Invoke(this); |
| 1730 | 50 | | } |
| | 51 | |
|
| 0 | 52 | | public Dictionary<System.Type, ISharedComponent> GetSharedComponents() { return sharedComponents; } |
| | 53 | |
|
| | 54 | | public void AddChild(IDCLEntity entity) |
| | 55 | | { |
| 13 | 56 | | if (!children.ContainsKey(entity.entityId)) |
| | 57 | | { |
| 13 | 58 | | children.Add(entity.entityId, entity); |
| | 59 | | } |
| 13 | 60 | | } |
| | 61 | |
|
| | 62 | | public void RemoveChild(IDCLEntity entity) |
| | 63 | | { |
| 3 | 64 | | if (children.ContainsKey(entity.entityId)) |
| | 65 | | { |
| 3 | 66 | | children.Remove(entity.entityId); |
| | 67 | | } |
| 3 | 68 | | } |
| | 69 | |
|
| | 70 | | public void SetParent(IDCLEntity entity) |
| | 71 | | { |
| 16 | 72 | | if (parent != null) |
| | 73 | | { |
| 3 | 74 | | parent.RemoveChild(this); |
| | 75 | | } |
| | 76 | |
|
| 16 | 77 | | if (entity != null) |
| | 78 | | { |
| 13 | 79 | | entity.AddChild(this); |
| | 80 | |
|
| 13 | 81 | | if (entity.gameObject && gameObject) |
| 13 | 82 | | gameObject.transform.SetParent(entity.gameObject.transform, false); |
| 13 | 83 | | } |
| 3 | 84 | | else if (gameObject) |
| | 85 | | { |
| 3 | 86 | | gameObject.transform.SetParent(null, false); |
| | 87 | | } |
| | 88 | |
|
| 16 | 89 | | parent = entity; |
| 16 | 90 | | } |
| | 91 | |
|
| | 92 | | public void EnsureMeshGameObject(string gameObjectName = null) |
| | 93 | | { |
| 277 | 94 | | if (meshesInfo.meshRootGameObject == null) |
| | 95 | | { |
| 275 | 96 | | meshesInfo.meshRootGameObject = new GameObject(); |
| 275 | 97 | | meshesInfo.meshRootGameObject.name = gameObjectName == null ? MESH_GAMEOBJECT_NAME : gameObjectName; |
| 275 | 98 | | meshesInfo.meshRootGameObject.transform.SetParent(gameObject.transform); |
| 275 | 99 | | Utils.ResetLocalTRS(meshesInfo.meshRootGameObject.transform); |
| | 100 | | } |
| 277 | 101 | | } |
| | 102 | |
|
| 0 | 103 | | public void ResetRelease() { isReleased = false; } |
| | 104 | |
|
| | 105 | | public void Cleanup() |
| | 106 | | { |
| | 107 | | // Don't do anything if this object was already released |
| 71 | 108 | | if (isReleased) |
| 0 | 109 | | return; |
| | 110 | |
|
| 71 | 111 | | OnRemoved?.Invoke(this); |
| | 112 | |
|
| | 113 | | // This will release the poolable objects of the mesh and the entity |
| 71 | 114 | | OnCleanupEvent?.Invoke(this); |
| | 115 | |
|
| 272 | 116 | | foreach (var kvp in components) |
| | 117 | | { |
| 65 | 118 | | if (kvp.Value == null) |
| | 119 | | continue; |
| | 120 | |
|
| 65 | 121 | | if (kvp.Value is ICleanable cleanableComponent) |
| 65 | 122 | | cleanableComponent.Cleanup(); |
| | 123 | |
|
| 65 | 124 | | if (!(kvp.Value is IPoolableObjectContainer poolableContainer)) |
| | 125 | | continue; |
| | 126 | |
|
| 4 | 127 | | if (poolableContainer.poolableObject == null) |
| | 128 | | continue; |
| | 129 | |
|
| 1 | 130 | | poolableContainer.poolableObject.Release(); |
| | 131 | | } |
| | 132 | |
|
| 71 | 133 | | components.Clear(); |
| | 134 | |
|
| 71 | 135 | | if (meshesInfo.meshRootGameObject) |
| | 136 | | { |
| 0 | 137 | | Utils.SafeDestroy(meshesInfo.meshRootGameObject); |
| 0 | 138 | | meshesInfo.CleanReferences(); |
| | 139 | | } |
| | 140 | |
|
| 71 | 141 | | if (gameObject) |
| | 142 | | { |
| 70 | 143 | | int childCount = gameObject.transform.childCount; |
| | 144 | |
|
| | 145 | | // Destroy any other children |
| 266 | 146 | | for (int i = 0; i < childCount; i++) |
| | 147 | | { |
| 63 | 148 | | Utils.SafeDestroy(gameObject.transform.GetChild(i).gameObject); |
| | 149 | | } |
| | 150 | |
|
| | 151 | | //NOTE(Brian): This will prevent any component from storing/querying invalid gameObject references. |
| 70 | 152 | | gameObject = null; |
| | 153 | | } |
| | 154 | |
|
| 71 | 155 | | OnTransformChange = null; |
| 71 | 156 | | isReleased = true; |
| 71 | 157 | | } |
| | 158 | |
|
| | 159 | | public void AddSharedComponent(System.Type componentType, ISharedComponent component) |
| | 160 | | { |
| 450 | 161 | | if (component == null) |
| | 162 | | { |
| 0 | 163 | | return; |
| | 164 | | } |
| | 165 | |
|
| 450 | 166 | | RemoveSharedComponent(componentType); |
| | 167 | |
|
| 450 | 168 | | sharedComponents.Add(componentType, component); |
| 450 | 169 | | } |
| | 170 | |
|
| | 171 | | public void RemoveSharedComponent(Type targetType, bool triggerDetaching = true) |
| | 172 | | { |
| 682 | 173 | | if (sharedComponents.TryGetValue(targetType, out ISharedComponent component)) |
| | 174 | | { |
| 159 | 175 | | if (component == null) |
| 0 | 176 | | return; |
| | 177 | |
|
| 159 | 178 | | sharedComponents.Remove(targetType); |
| | 179 | |
|
| 159 | 180 | | if (triggerDetaching) |
| 31 | 181 | | component.DetachFrom(this, targetType); |
| | 182 | | } |
| 682 | 183 | | } |
| | 184 | |
|
| | 185 | | /// <summary> |
| | 186 | | /// This function is designed to get interfaces implemented by diverse components. |
| | 187 | | /// |
| | 188 | | /// If you want to get the component itself please use TryGetBaseComponent or TryGetSharedComponent. |
| | 189 | | /// </summary> |
| | 190 | | /// <typeparam name="T"></typeparam> |
| | 191 | | /// <returns></returns> |
| | 192 | | public T TryGetComponent<T>() where T : class |
| | 193 | | { |
| | 194 | | //Note (Adrian): If you are going to call this function frequently, please refactor it to avoid using LinQ f |
| 168 | 195 | | T component = components.Values.FirstOrDefault(x => x is T) as T; |
| | 196 | |
|
| 168 | 197 | | if (component != null) |
| 1 | 198 | | return component; |
| | 199 | |
|
| 167 | 200 | | component = sharedComponents.Values.FirstOrDefault(x => x is T) as T; |
| | 201 | |
|
| 167 | 202 | | if (component != null) |
| 48 | 203 | | return component; |
| | 204 | |
|
| 119 | 205 | | return null; |
| | 206 | | } |
| | 207 | |
|
| 268 | 208 | | public bool TryGetBaseComponent(CLASS_ID_COMPONENT componentId, out IEntityComponent component) { return compone |
| | 209 | |
|
| | 210 | | public bool TryGetSharedComponent(CLASS_ID componentId, out ISharedComponent component) |
| | 211 | | { |
| 382 | 212 | | foreach (KeyValuePair<Type, ISharedComponent> keyValuePairBaseDisposable in sharedComponents) |
| | 213 | | { |
| 53 | 214 | | if (keyValuePairBaseDisposable.Value.GetClassId() == (int) componentId) |
| | 215 | | { |
| 10 | 216 | | component = keyValuePairBaseDisposable.Value; |
| 10 | 217 | | return true; |
| | 218 | | } |
| | 219 | | } |
| | 220 | |
|
| 133 | 221 | | component = null; |
| 133 | 222 | | return false; |
| 10 | 223 | | } |
| | 224 | |
|
| | 225 | | public ISharedComponent GetSharedComponent(System.Type targetType) |
| | 226 | | { |
| 64 | 227 | | if (sharedComponents.TryGetValue(targetType, out ISharedComponent component)) |
| | 228 | | { |
| 61 | 229 | | return component; |
| | 230 | | } |
| | 231 | |
|
| 3 | 232 | | return null; |
| | 233 | | } |
| | 234 | | } |
| | 235 | | } |