| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Configuration; |
| | 3 | | using DCL.Models; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public class CollidersManager : Singleton<CollidersManager> |
| | 10 | | { |
| 1 | 11 | | private Dictionary<Collider, ColliderInfo> colliderInfo = new Dictionary<Collider, ColliderInfo>(); |
| 1 | 12 | | private Dictionary<IDCLEntity, List<Collider>> collidersByEntity = new Dictionary<IDCLEntity, List<Collider>>(); |
| | 13 | | private static CollidersManager instance = null; |
| | 14 | |
|
| | 15 | | public static void Release() |
| | 16 | | { |
| 0 | 17 | | if (instance != null) |
| | 18 | | { |
| 0 | 19 | | using (var iterator = instance.collidersByEntity.Keys.GetEnumerator()) |
| | 20 | | { |
| 0 | 21 | | while (iterator.MoveNext()) |
| 0 | 22 | | iterator.Current.OnCleanupEvent -= instance.OnEntityCleanUpEvent; |
| 0 | 23 | | } |
| | 24 | |
|
| 0 | 25 | | instance = null; |
| | 26 | | } |
| 0 | 27 | | } |
| | 28 | |
|
| | 29 | | void AddOrUpdateColliderInfo(Collider collider, ColliderInfo info) |
| | 30 | | { |
| | 31 | | // Note (Zak): This could be achieved in one line |
| | 32 | | // just by doing colliderInfo[collider] = info; |
| | 33 | | // but nobody likes it that way... :'( |
| 309 | 34 | | if (colliderInfo.ContainsKey(collider)) |
| 7 | 35 | | colliderInfo[collider] = info; |
| | 36 | | else |
| 302 | 37 | | colliderInfo.Add(collider, info); |
| 302 | 38 | | } |
| | 39 | |
|
| | 40 | | void RemoveColliderInfo(Collider collider) |
| | 41 | | { |
| 44 | 42 | | if (colliderInfo.ContainsKey(collider)) |
| 44 | 43 | | colliderInfo.Remove(collider); |
| 44 | 44 | | } |
| | 45 | |
|
| | 46 | | public void RemoveEntityCollider(IDCLEntity entity, Collider collider) |
| | 47 | | { |
| 686 | 48 | | if (entity == null || collider == null || !collidersByEntity.ContainsKey(entity)) |
| 686 | 49 | | return; |
| | 50 | |
|
| 0 | 51 | | collidersByEntity[entity].Remove(collider); |
| 0 | 52 | | RemoveColliderInfo(collider); |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | public void AddOrUpdateEntityCollider(IDCLEntity entity, Collider collider) |
| | 56 | | { |
| 309 | 57 | | if (!collidersByEntity.ContainsKey(entity)) |
| 203 | 58 | | collidersByEntity.Add(entity, new List<Collider>()); |
| | 59 | |
|
| 309 | 60 | | List<Collider> collidersList = collidersByEntity[entity]; |
| | 61 | |
|
| 309 | 62 | | if (!collidersList.Contains(collider)) |
| 302 | 63 | | collidersList.Add(collider); |
| | 64 | |
|
| 309 | 65 | | ColliderInfo info = new ColliderInfo(); |
| 309 | 66 | | info.entity = entity; |
| 309 | 67 | | info.meshName = collider.transform.parent != null ? collider.transform.parent.name : ""; |
| 309 | 68 | | info.scene = entity.scene; |
| 309 | 69 | | AddOrUpdateColliderInfo(collider, info); |
| | 70 | |
|
| | 71 | | // Note (Zak): avoid adding the event multiple times |
| 309 | 72 | | entity.OnCleanupEvent -= OnEntityCleanUpEvent; |
| 309 | 73 | | entity.OnCleanupEvent += OnEntityCleanUpEvent; |
| 309 | 74 | | } |
| | 75 | |
|
| | 76 | | void RemoveAllEntityColliders(IDCLEntity entity) |
| | 77 | | { |
| 43 | 78 | | if (collidersByEntity.ContainsKey(entity)) |
| | 79 | | { |
| 43 | 80 | | List<Collider> collidersList = collidersByEntity[entity]; |
| 43 | 81 | | int count = collidersList.Count; |
| | 82 | |
|
| 174 | 83 | | for (int i = 0; i < count; i++) |
| 44 | 84 | | RemoveColliderInfo(collidersList[i]); |
| | 85 | |
|
| 43 | 86 | | collidersByEntity.Remove(entity); |
| | 87 | | } |
| 43 | 88 | | } |
| | 89 | |
|
| | 90 | | void OnEntityCleanUpEvent(ICleanableEventDispatcher dispatcher) |
| | 91 | | { |
| 43 | 92 | | dispatcher.OnCleanupEvent -= OnEntityCleanUpEvent; |
| | 93 | |
|
| 43 | 94 | | RemoveAllEntityColliders((IDCLEntity) dispatcher); |
| 43 | 95 | | } |
| | 96 | |
|
| | 97 | | public bool GetColliderInfo(Collider collider, out ColliderInfo info) |
| | 98 | | { |
| 389 | 99 | | if (collider != null && colliderInfo.ContainsKey(collider)) |
| | 100 | | { |
| 389 | 101 | | info = colliderInfo[collider]; |
| 389 | 102 | | return true; |
| | 103 | | } |
| | 104 | | else |
| | 105 | | { |
| 0 | 106 | | info = new ColliderInfo(); |
| | 107 | | } |
| | 108 | |
|
| 0 | 109 | | return false; |
| | 110 | | } |
| | 111 | |
|
| 2 | 112 | | public void ConfigureColliders(IDCLEntity entity, bool hasCollision = true, bool filterByColliderName = true) { |
| | 113 | |
|
| | 114 | | public void ConfigureColliders(GameObject meshGameObject, bool hasCollision, bool filterByColliderName = false, |
| | 115 | | { |
| 284 | 116 | | if (meshGameObject == null) |
| 0 | 117 | | return; |
| | 118 | |
|
| 284 | 119 | | if (entity != null) |
| 284 | 120 | | entity.meshesInfo.colliders.Clear(); |
| | 121 | |
|
| 284 | 122 | | if (colliderLayer == -1) |
| 4 | 123 | | colliderLayer = DCL.Configuration.PhysicsLayers.defaultLayer; |
| | 124 | |
|
| | 125 | | Collider collider; |
| 284 | 126 | | int onClickLayer = PhysicsLayers.onPointerEventLayer; // meshes can have a child collider for the OnClick th |
| 284 | 127 | | MeshFilter[] meshFilters = meshGameObject.GetComponentsInChildren<MeshFilter>(true); |
| | 128 | |
|
| 1356 | 129 | | for (int i = 0; i < meshFilters.Length; i++) |
| | 130 | | { |
| 394 | 131 | | if (meshFilters[i].gameObject.layer == onClickLayer) |
| | 132 | | continue; |
| | 133 | |
|
| 394 | 134 | | if (filterByColliderName) |
| | 135 | | { |
| 171 | 136 | | if (!meshFilters[i].transform.parent.name.ToLower().Contains("_collider")) |
| | 137 | | continue; |
| | 138 | |
|
| | 139 | | // we remove the Renderer of the '_collider' object, as its true renderer is in another castle |
| 13 | 140 | | Object.Destroy(meshFilters[i].GetComponent<Renderer>()); |
| | 141 | | } |
| | 142 | |
|
| 236 | 143 | | collider = meshFilters[i].GetComponent<Collider>(); |
| | 144 | |
|
| | 145 | | //HACK(Pravus): Hack to bring back compatibility with old builder scenes that have withCollision = false |
| | 146 | | // Remove when we fix this changing the property name or something similar. |
| 236 | 147 | | bool shouldCreateCollider = hasCollision || filterByColliderName; |
| | 148 | |
|
| 236 | 149 | | if (shouldCreateCollider) |
| | 150 | | { |
| 220 | 151 | | if (collider == null) |
| 209 | 152 | | collider = meshFilters[i].gameObject.AddComponent<MeshCollider>(); |
| | 153 | |
|
| 220 | 154 | | if (collider is MeshCollider) |
| 217 | 155 | | ((MeshCollider) collider).sharedMesh = meshFilters[i].sharedMesh; |
| | 156 | |
|
| 220 | 157 | | if (entity != null) |
| 220 | 158 | | AddOrUpdateEntityCollider(entity, collider); |
| | 159 | | } |
| | 160 | |
|
| 236 | 161 | | if (collider != null) |
| | 162 | | { |
| 231 | 163 | | collider.gameObject.layer = colliderLayer; |
| 231 | 164 | | collider.enabled = shouldCreateCollider; |
| | 165 | |
|
| 231 | 166 | | if (entity != null) |
| 231 | 167 | | entity.meshesInfo.colliders.Add(collider); |
| | 168 | | } |
| | 169 | | } |
| 284 | 170 | | } |
| | 171 | | } |
| | 172 | | } |