| | 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 | | { |
| 2844 | 12 | | public IParcelScene scene { get; set; } |
| 522 | 13 | | public bool markedForCleanup { get; set; } = false; |
| 103 | 14 | | public bool isInsideBoundaries { get; set; } = false; |
| | 15 | |
|
| 1262 | 16 | | public Dictionary<long, IDCLEntity> children { get; private set; } = new Dictionary<long, IDCLEntity>(); |
| 514 | 17 | | public IDCLEntity parent { get; private set; } |
| 6103 | 18 | | public GameObject gameObject { get; set; } |
| 9948 | 19 | | public long entityId { get; set; } |
| 9577 | 20 | | public MeshesInfo meshesInfo { get; set; } |
| 3793 | 21 | | public GameObject meshRootGameObject => meshesInfo.meshRootGameObject; |
| 6 | 22 | | public Renderer[] renderers => meshesInfo.renderers; |
| | 23 | |
|
| 2461 | 24 | | public Action<IDCLEntity> OnShapeUpdated { get; set; } |
| 188 | 25 | | public Action<IDCLEntity> OnShapeLoaded { get; set; } |
| 213 | 26 | | public Action<object> OnNameChange { get; set; } |
| 285 | 27 | | public Action<object> OnTransformChange { get; set; } |
| 1906 | 28 | | public Action<IDCLEntity> OnRemoved { get; set; } |
| 0 | 29 | | public Action<IDCLEntity> OnMeshesInfoUpdated { get; set; } |
| 0 | 30 | | public Action<IDCLEntity> OnMeshesInfoCleaned { get; set; } |
| | 31 | |
|
| 3190 | 32 | | public Action<ICleanableEventDispatcher> OnCleanupEvent { get; set; } |
| | 33 | |
|
| 0 | 34 | | public long parentId { get; set; } |
| | 35 | |
|
| | 36 | | const string MESH_GAMEOBJECT_NAME = "Mesh"; |
| | 37 | |
|
| | 38 | | bool isReleased = false; |
| | 39 | |
|
| 600 | 40 | | public DecentralandEntity() |
| | 41 | | { |
| 600 | 42 | | meshesInfo = new MeshesInfo(); |
| 1206 | 43 | | OnShapeUpdated += (entity) => meshesInfo.UpdateRenderersCollection(); |
| 1361 | 44 | | meshesInfo.OnUpdated += () => OnMeshesInfoUpdated?.Invoke(this); |
| 839 | 45 | | meshesInfo.OnCleanup += () => OnMeshesInfoCleaned?.Invoke(this); |
| 600 | 46 | | } |
| | 47 | |
|
| | 48 | | public void AddChild(IDCLEntity entity) |
| | 49 | | { |
| 12 | 50 | | if (!children.ContainsKey(entity.entityId)) |
| | 51 | | { |
| 12 | 52 | | children.Add(entity.entityId, entity); |
| | 53 | | } |
| 12 | 54 | | } |
| | 55 | |
|
| | 56 | | public void RemoveChild(IDCLEntity entity) |
| | 57 | | { |
| 2 | 58 | | if (children.ContainsKey(entity.entityId)) |
| | 59 | | { |
| 2 | 60 | | children.Remove(entity.entityId); |
| | 61 | | } |
| 2 | 62 | | } |
| | 63 | |
|
| | 64 | | public void SetParent(IDCLEntity entity) |
| | 65 | | { |
| 17 | 66 | | if (parent != null) |
| | 67 | | { |
| 2 | 68 | | parent.RemoveChild(this); |
| | 69 | | } |
| | 70 | |
|
| 17 | 71 | | if (entity != null) |
| | 72 | | { |
| 12 | 73 | | parentId = entity.entityId; |
| 12 | 74 | | entity.AddChild(this); |
| | 75 | |
|
| 12 | 76 | | if (entity.gameObject && gameObject) |
| 12 | 77 | | gameObject.transform.SetParent(entity.gameObject.transform, false); |
| 12 | 78 | | } |
| 5 | 79 | | else if (gameObject) |
| | 80 | | { |
| 5 | 81 | | gameObject.transform.SetParent(null, false); |
| | 82 | | } |
| | 83 | |
|
| 17 | 84 | | parent = entity; |
| 17 | 85 | | } |
| | 86 | |
|
| | 87 | | public void EnsureMeshGameObject(string gameObjectName = null) |
| | 88 | | { |
| 284 | 89 | | if (meshesInfo.meshRootGameObject == null) |
| | 90 | | { |
| 282 | 91 | | meshesInfo.meshRootGameObject = new GameObject(); |
| 282 | 92 | | meshesInfo.meshRootGameObject.name = gameObjectName == null ? MESH_GAMEOBJECT_NAME : gameObjectName; |
| 282 | 93 | | meshesInfo.meshRootGameObject.transform.SetParent(gameObject.transform); |
| 282 | 94 | | Utils.ResetLocalTRS(meshesInfo.meshRootGameObject.transform); |
| | 95 | | } |
| 284 | 96 | | } |
| | 97 | |
|
| 0 | 98 | | public void ResetRelease() { isReleased = false; } |
| | 99 | |
|
| | 100 | | public void Cleanup() |
| | 101 | | { |
| | 102 | | // Don't do anything if this object was already released |
| 530 | 103 | | if (isReleased) |
| 0 | 104 | | return; |
| | 105 | |
|
| 530 | 106 | | OnRemoved?.Invoke(this); |
| | 107 | |
|
| | 108 | | // This will release the poolable objects of the mesh and the entity |
| 530 | 109 | | OnCleanupEvent?.Invoke(this); |
| | 110 | |
|
| 530 | 111 | | scene.componentsManagerLegacy.CleanComponents(this); |
| | 112 | |
|
| 530 | 113 | | if (meshesInfo.meshRootGameObject) |
| | 114 | | { |
| 8 | 115 | | Utils.SafeDestroy(meshesInfo.meshRootGameObject); |
| 8 | 116 | | meshesInfo.CleanReferences(); |
| | 117 | | } |
| | 118 | |
|
| 530 | 119 | | if (gameObject) |
| | 120 | | { |
| 525 | 121 | | int childCount = gameObject.transform.childCount; |
| | 122 | |
|
| | 123 | | // Destroy any other children |
| 1580 | 124 | | for (int i = 0; i < childCount; i++) |
| | 125 | | { |
| 265 | 126 | | Utils.SafeDestroy(gameObject.transform.GetChild(i).gameObject); |
| | 127 | | } |
| | 128 | |
|
| | 129 | | //NOTE(Brian): This will prevent any component from storing/querying invalid gameObject references. |
| 525 | 130 | | gameObject = null; |
| | 131 | | } |
| | 132 | |
|
| 530 | 133 | | OnTransformChange = null; |
| 530 | 134 | | isReleased = true; |
| 530 | 135 | | } |
| | 136 | | } |
| | 137 | | } |