| | 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 | | { |
| 3673 | 12 | | public IParcelScene scene { get; set; } |
| 239 | 13 | | public bool markedForCleanup { get; set; } = false; |
| | 14 | |
|
| | 15 | | // We let the SceneBoundsChecker update these values later |
| 1009 | 16 | | public bool isInsideSceneOuterBoundaries { get; private set; } = true; |
| 1820 | 17 | | public bool isInsideSceneBoundaries { get; private set; } = true; |
| | 18 | |
|
| 847 | 19 | | public Dictionary<long, IDCLEntity> children { get; private set; } = new Dictionary<long, IDCLEntity>(); |
| 267 | 20 | | public IDCLEntity parent { get; private set; } |
| 11752 | 21 | | public GameObject gameObject { get; set; } |
| 6313 | 22 | | public long entityId { get; set; } |
| 37670 | 23 | | public MeshesInfo meshesInfo { get; set; } |
| 3890 | 24 | | public GameObject meshRootGameObject => meshesInfo.meshRootGameObject; |
| 0 | 25 | | public Renderer[] renderers => meshesInfo.renderers; |
| | 26 | |
|
| | 27 | |
|
| 2568 | 28 | | public Action<IDCLEntity> OnShapeUpdated { get; set; } |
| 141 | 29 | | public Action<IDCLEntity> OnShapeLoaded { get; set; } |
| 0 | 30 | | public Action<object> OnNameChange { get; set; } |
| 470 | 31 | | public Action<Vector3, Quaternion> OnTransformChange { get; set; } |
| 1642 | 32 | | public Action<IDCLEntity> OnRemoved { get; set; } |
| 689 | 33 | | public Action<IDCLEntity> OnMeshesInfoUpdated { get; set; } |
| 171 | 34 | | public Action<IDCLEntity> OnMeshesInfoCleaned { get; set; } |
| 244 | 35 | | public Action<CLASS_ID_COMPONENT, IDCLEntity> OnBaseComponentAdded { get; set; } |
| 326 | 36 | | public Action<IDCLEntity, bool> OnInsideBoundariesChanged { get; set; } |
| 262 | 37 | | public Action<IDCLEntity, bool> OnOuterBoundariesChanged { get; set; } |
| | 38 | |
|
| 3272 | 39 | | public Action<ICleanableEventDispatcher> OnCleanupEvent { get; set; } |
| | 40 | |
|
| 11 | 41 | | public long parentId { get; set; } |
| 304 | 42 | | public IList<long> childrenId { get; } = new List<long>(); |
| | 43 | |
|
| | 44 | | const string MESH_GAMEOBJECT_NAME = "Mesh"; |
| | 45 | |
|
| | 46 | | bool isReleased = false; |
| | 47 | |
|
| 304 | 48 | | public DecentralandEntity() |
| | 49 | | { |
| 304 | 50 | | meshesInfo = new MeshesInfo(); |
| 859 | 51 | | OnShapeUpdated += (entity) => meshesInfo.UpdateRenderersCollection(); |
| 993 | 52 | | meshesInfo.OnUpdated += () => OnMeshesInfoUpdated?.Invoke(this); |
| 475 | 53 | | meshesInfo.OnCleanup += () => OnMeshesInfoCleaned?.Invoke(this); |
| 304 | 54 | | } |
| | 55 | |
|
| | 56 | | public void AddChild(IDCLEntity entity) |
| | 57 | | { |
| 22 | 58 | | if (!children.ContainsKey(entity.entityId)) { children.Add(entity.entityId, entity); } |
| 11 | 59 | | } |
| | 60 | |
|
| | 61 | | public void RemoveChild(IDCLEntity entity) |
| | 62 | | { |
| 4 | 63 | | if (children.ContainsKey(entity.entityId)) { children.Remove(entity.entityId); } |
| 2 | 64 | | } |
| | 65 | |
|
| | 66 | | public void SetParent(IDCLEntity entity) |
| | 67 | | { |
| 21 | 68 | | if (parent != null) { parent.RemoveChild(this); } |
| | 69 | |
|
| 19 | 70 | | if (entity != null) |
| | 71 | | { |
| 11 | 72 | | parentId = entity.entityId; |
| 11 | 73 | | entity.AddChild(this); |
| | 74 | |
|
| 11 | 75 | | if (entity.gameObject && gameObject) |
| 11 | 76 | | gameObject.transform.SetParent(entity.gameObject.transform, false); |
| | 77 | | } |
| 16 | 78 | | else if (gameObject) { gameObject.transform.SetParent(null, false); } |
| | 79 | |
|
| 19 | 80 | | parent = entity; |
| 19 | 81 | | } |
| | 82 | |
|
| | 83 | | public void EnsureMeshGameObject(string gameObjectName = null) |
| | 84 | | { |
| 252 | 85 | | if (meshesInfo.meshRootGameObject != null) return; |
| | 86 | |
|
| 248 | 87 | | meshesInfo.meshRootGameObject = new GameObject { name = gameObjectName ?? MESH_GAMEOBJECT_NAME }; |
| 248 | 88 | | meshesInfo.meshRootGameObject.transform.SetParent(gameObject.transform); |
| 248 | 89 | | meshesInfo.meshRootGameObject.transform.ResetLocalTRS(); |
| 248 | 90 | | } |
| | 91 | |
|
| | 92 | | public void ResetRelease() |
| | 93 | | { |
| 0 | 94 | | isReleased = false; |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | public void Cleanup() |
| | 98 | | { |
| | 99 | | // Don't do anything if this object was already released |
| 240 | 100 | | if (isReleased) |
| 0 | 101 | | return; |
| | 102 | |
|
| 240 | 103 | | OnRemoved?.Invoke(this); |
| | 104 | |
|
| | 105 | | // This will release the poolable objects of the mesh and the entity |
| 240 | 106 | | OnCleanupEvent?.Invoke(this); |
| | 107 | |
|
| 240 | 108 | | scene.componentsManagerLegacy.CleanComponents(this); |
| | 109 | |
|
| 240 | 110 | | if (meshesInfo.meshRootGameObject && !meshesInfo.RootIsPoolableObject) |
| | 111 | | { |
| 1 | 112 | | Utils.SafeDestroy(meshesInfo.meshRootGameObject); |
| 1 | 113 | | meshesInfo.CleanReferences(); |
| | 114 | | } |
| | 115 | |
|
| 240 | 116 | | if (gameObject) |
| | 117 | | { |
| 238 | 118 | | int childCount = gameObject.transform.childCount; |
| | 119 | |
|
| | 120 | | // Destroy any other children |
| 880 | 121 | | for (var i = 0; i < childCount; i++) |
| 202 | 122 | | Utils.SafeDestroy(gameObject.transform.GetChild(i).gameObject); |
| | 123 | |
|
| | 124 | | //NOTE(Brian): This will prevent any component from storing/querying invalid gameObject references. |
| 238 | 125 | | gameObject = null; |
| | 126 | | } |
| | 127 | |
|
| 240 | 128 | | OnTransformChange = null; |
| 240 | 129 | | isReleased = true; |
| 240 | 130 | | } |
| | 131 | |
|
| | 132 | | public void UpdateInsideBoundariesStatus(bool isInsideBoundaries) |
| | 133 | | { |
| 98 | 134 | | isInsideSceneBoundaries = isInsideBoundaries; |
| 98 | 135 | | OnInsideBoundariesChanged?.Invoke(this, isInsideSceneBoundaries); |
| 27 | 136 | | } |
| | 137 | |
|
| | 138 | | public void UpdateOuterBoundariesStatus(bool isInsideOuterBoundaries) |
| | 139 | | { |
| 262 | 140 | | isInsideSceneOuterBoundaries = isInsideOuterBoundaries; |
| 262 | 141 | | OnOuterBoundariesChanged?.Invoke(this, isInsideSceneOuterBoundaries); |
| 0 | 142 | | } |
| | 143 | | } |
| | 144 | | } |