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