| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.Models |
| | 8 | | { |
| | 9 | | [Serializable] |
| | 10 | | public class DecentralandEntity : IDCLEntity |
| | 11 | | { |
| 6156 | 12 | | public IParcelScene scene { get; set; } |
| 528 | 13 | | public bool markedForCleanup { get; set; } = false; |
| | 14 | |
|
| | 15 | | // We let the SceneBoundsChecker update these values later |
| 1508 | 16 | | public bool isInsideSceneOuterBoundaries { get; set; } = true; |
| 2787 | 17 | | public bool isInsideSceneBoundaries { get; set; } = true; |
| | 18 | |
|
| 1509 | 19 | | public Dictionary<long, IDCLEntity> children { get; private set; } = new Dictionary<long, IDCLEntity>(); |
| 508 | 20 | | public IDCLEntity parent { get; private set; } |
| 5500 | 21 | | public GameObject gameObject { get; set; } |
| 9239 | 22 | | public long entityId { get; set; } |
| 10041 | 23 | | public MeshesInfo meshesInfo { get; set; } |
| 4590 | 24 | | public GameObject meshRootGameObject => meshesInfo.meshRootGameObject; |
| 6 | 25 | | public Renderer[] renderers => meshesInfo.renderers; |
| | 26 | |
|
| | 27 | |
|
| 2498 | 28 | | public Action<IDCLEntity> OnShapeUpdated { get; set; } |
| 196 | 29 | | public Action<IDCLEntity> OnShapeLoaded { get; set; } |
| 213 | 30 | | public Action<object> OnNameChange { get; set; } |
| 290 | 31 | | public Action<object> OnTransformChange { get; set; } |
| 1930 | 32 | | public Action<IDCLEntity> OnRemoved { get; set; } |
| 0 | 33 | | public Action<IDCLEntity> OnMeshesInfoUpdated { get; set; } |
| 0 | 34 | | public Action<IDCLEntity> OnMeshesInfoCleaned { get; set; } |
| 299 | 35 | | public Action<CLASS_ID_COMPONENT, IDCLEntity> OnBaseComponentAdded { get; set; } |
| | 36 | |
|
| 3292 | 37 | | public Action<ICleanableEventDispatcher> OnCleanupEvent { get; set; } |
| | 38 | |
|
| 0 | 39 | | public long parentId { get; set; } |
| 607 | 40 | | public IList<long> childrenId { get; } = new List<long>(); |
| | 41 | |
|
| | 42 | | const string MESH_GAMEOBJECT_NAME = "Mesh"; |
| | 43 | |
|
| | 44 | | bool isReleased = false; |
| | 45 | |
|
| 607 | 46 | | public DecentralandEntity() |
| | 47 | | { |
| 607 | 48 | | meshesInfo = new MeshesInfo(); |
| 1226 | 49 | | OnShapeUpdated += (entity) => meshesInfo.UpdateRenderersCollection(); |
| 1385 | 50 | | meshesInfo.OnUpdated += () => OnMeshesInfoUpdated?.Invoke(this); |
| 853 | 51 | | meshesInfo.OnCleanup += () => OnMeshesInfoCleaned?.Invoke(this); |
| 607 | 52 | | } |
| | 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 | | { |
| 2 | 64 | | if (children.ContainsKey(entity.entityId)) |
| | 65 | | { |
| 2 | 66 | | children.Remove(entity.entityId); |
| | 67 | | } |
| 2 | 68 | | } |
| | 69 | |
|
| | 70 | | public void SetParent(IDCLEntity entity) |
| | 71 | | { |
| 18 | 72 | | if (parent != null) |
| | 73 | | { |
| 2 | 74 | | parent.RemoveChild(this); |
| | 75 | | } |
| | 76 | |
|
| 18 | 77 | | if (entity != null) |
| | 78 | | { |
| 13 | 79 | | parentId = entity.entityId; |
| 13 | 80 | | entity.AddChild(this); |
| | 81 | |
|
| 13 | 82 | | if (entity.gameObject && gameObject) |
| 13 | 83 | | gameObject.transform.SetParent(entity.gameObject.transform, false); |
| 13 | 84 | | } |
| 5 | 85 | | else if (gameObject) |
| | 86 | | { |
| 5 | 87 | | gameObject.transform.SetParent(null, false); |
| | 88 | | } |
| | 89 | |
|
| 18 | 90 | | parent = entity; |
| 18 | 91 | | } |
| | 92 | |
|
| | 93 | | public void EnsureMeshGameObject(string gameObjectName = null) |
| | 94 | | { |
| 289 | 95 | | if (meshesInfo.meshRootGameObject == null) |
| | 96 | | { |
| 287 | 97 | | meshesInfo.meshRootGameObject = new GameObject(); |
| 287 | 98 | | meshesInfo.meshRootGameObject.name = gameObjectName == null ? MESH_GAMEOBJECT_NAME : gameObjectName; |
| 287 | 99 | | meshesInfo.meshRootGameObject.transform.SetParent(gameObject.transform); |
| 287 | 100 | | Utils.ResetLocalTRS(meshesInfo.meshRootGameObject.transform); |
| | 101 | | } |
| 289 | 102 | | } |
| | 103 | |
|
| 0 | 104 | | public void ResetRelease() { isReleased = false; } |
| | 105 | |
|
| | 106 | | public void Cleanup() |
| | 107 | | { |
| | 108 | | // Don't do anything if this object was already released |
| 537 | 109 | | if (isReleased) |
| 0 | 110 | | return; |
| | 111 | |
|
| 537 | 112 | | OnRemoved?.Invoke(this); |
| | 113 | |
|
| | 114 | | // This will release the poolable objects of the mesh and the entity |
| 537 | 115 | | OnCleanupEvent?.Invoke(this); |
| | 116 | |
|
| 537 | 117 | | scene.componentsManagerLegacy.CleanComponents(this); |
| | 118 | |
|
| 537 | 119 | | if (meshesInfo.meshRootGameObject) |
| | 120 | | { |
| 8 | 121 | | Utils.SafeDestroy(meshesInfo.meshRootGameObject); |
| 8 | 122 | | meshesInfo.CleanReferences(); |
| | 123 | | } |
| | 124 | |
|
| 537 | 125 | | if (gameObject) |
| | 126 | | { |
| 532 | 127 | | int childCount = gameObject.transform.childCount; |
| | 128 | |
|
| | 129 | | // Destroy any other children |
| 1658 | 130 | | for (int i = 0; i < childCount; i++) |
| | 131 | | { |
| 297 | 132 | | Utils.SafeDestroy(gameObject.transform.GetChild(i).gameObject); |
| | 133 | | } |
| | 134 | |
|
| | 135 | | //NOTE(Brian): This will prevent any component from storing/querying invalid gameObject references. |
| 532 | 136 | | gameObject = null; |
| | 137 | | } |
| | 138 | |
|
| 537 | 139 | | OnTransformChange = null; |
| 537 | 140 | | isReleased = true; |
| 537 | 141 | | } |
| | 142 | | } |
| | 143 | | } |