< Summary

Class:DCL.CollidersManager
Assembly:CollidersManager
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/CollidersManager/CollidersManager.cs
Covered lines:65
Uncovered lines:13
Coverable lines:78
Total lines:172
Line coverage:83.3% (65 of 78)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CollidersManager()0%110100%
Release()0%12300%
AddOrUpdateColliderInfo(...)0%220100%
RemoveColliderInfo(...)0%220100%
RemoveEntityCollider(...)0%7.464040%
AddOrUpdateEntityCollider(...)0%550100%
RemoveAllEntityColliders(...)0%330100%
OnEntityCleanUpEvent(...)0%110100%
GetColliderInfo(...)0%3.583060%
ConfigureColliders(...)0%110100%
ConfigureColliders(...)0%14.0114096.67%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/CollidersManager/CollidersManager.cs

#LineLine coverage
 1using DCL.Components;
 2using DCL.Configuration;
 3using DCL.Models;
 4using System.Collections.Generic;
 5using UnityEngine;
 6
 7namespace DCL
 8{
 9    public class CollidersManager : Singleton<CollidersManager>
 10    {
 111        private Dictionary<Collider, ColliderInfo> colliderInfo = new Dictionary<Collider, ColliderInfo>();
 112        private Dictionary<IDCLEntity, List<Collider>> collidersByEntity = new Dictionary<IDCLEntity, List<Collider>>();
 13        private static CollidersManager instance = null;
 14
 15        public static void Release()
 16        {
 017            if (instance != null)
 18            {
 019                using (var iterator = instance.collidersByEntity.Keys.GetEnumerator())
 20                {
 021                    while (iterator.MoveNext())
 022                        iterator.Current.OnCleanupEvent -= instance.OnEntityCleanUpEvent;
 023                }
 24
 025                instance = null;
 26            }
 027        }
 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... :'(
 30934            if (colliderInfo.ContainsKey(collider))
 735                colliderInfo[collider] = info;
 36            else
 30237                colliderInfo.Add(collider, info);
 30238        }
 39
 40        void RemoveColliderInfo(Collider collider)
 41        {
 4442            if (colliderInfo.ContainsKey(collider))
 4443                colliderInfo.Remove(collider);
 4444        }
 45
 46        public void RemoveEntityCollider(IDCLEntity entity, Collider collider)
 47        {
 68648            if (entity == null || collider == null || !collidersByEntity.ContainsKey(entity))
 68649                return;
 50
 051            collidersByEntity[entity].Remove(collider);
 052            RemoveColliderInfo(collider);
 053        }
 54
 55        public void AddOrUpdateEntityCollider(IDCLEntity entity, Collider collider)
 56        {
 30957            if (!collidersByEntity.ContainsKey(entity))
 20358                collidersByEntity.Add(entity, new List<Collider>());
 59
 30960            List<Collider> collidersList = collidersByEntity[entity];
 61
 30962            if (!collidersList.Contains(collider))
 30263                collidersList.Add(collider);
 64
 30965            ColliderInfo info = new ColliderInfo();
 30966            info.entity = entity;
 30967            info.meshName = collider.transform.parent != null ? collider.transform.parent.name : "";
 30968            info.scene = entity.scene;
 30969            AddOrUpdateColliderInfo(collider, info);
 70
 71            // Note (Zak): avoid adding the event multiple times
 30972            entity.OnCleanupEvent -= OnEntityCleanUpEvent;
 30973            entity.OnCleanupEvent += OnEntityCleanUpEvent;
 30974        }
 75
 76        void RemoveAllEntityColliders(IDCLEntity entity)
 77        {
 4378            if (collidersByEntity.ContainsKey(entity))
 79            {
 4380                List<Collider> collidersList = collidersByEntity[entity];
 4381                int count = collidersList.Count;
 82
 17483                for (int i = 0; i < count; i++)
 4484                    RemoveColliderInfo(collidersList[i]);
 85
 4386                collidersByEntity.Remove(entity);
 87            }
 4388        }
 89
 90        void OnEntityCleanUpEvent(ICleanableEventDispatcher dispatcher)
 91        {
 4392            dispatcher.OnCleanupEvent -= OnEntityCleanUpEvent;
 93
 4394            RemoveAllEntityColliders((IDCLEntity) dispatcher);
 4395        }
 96
 97        public bool GetColliderInfo(Collider collider, out ColliderInfo info)
 98        {
 38999            if (collider != null && colliderInfo.ContainsKey(collider))
 100            {
 389101                info = colliderInfo[collider];
 389102                return true;
 103            }
 104            else
 105            {
 0106                info = new ColliderInfo();
 107            }
 108
 0109            return false;
 110        }
 111
 2112        public void ConfigureColliders(IDCLEntity entity, bool hasCollision = true, bool filterByColliderName = true) { 
 113
 114        public void ConfigureColliders(GameObject meshGameObject, bool hasCollision, bool filterByColliderName = false, 
 115        {
 284116            if (meshGameObject == null)
 0117                return;
 118
 284119            if (entity != null)
 284120                entity.meshesInfo.colliders.Clear();
 121
 284122            if (colliderLayer == -1)
 4123                colliderLayer = DCL.Configuration.PhysicsLayers.defaultLayer;
 124
 125            Collider collider;
 284126            int onClickLayer = PhysicsLayers.onPointerEventLayer; // meshes can have a child collider for the OnClick th
 284127            MeshFilter[] meshFilters = meshGameObject.GetComponentsInChildren<MeshFilter>(true);
 128
 1356129            for (int i = 0; i < meshFilters.Length; i++)
 130            {
 394131                if (meshFilters[i].gameObject.layer == onClickLayer)
 132                    continue;
 133
 394134                if (filterByColliderName)
 135                {
 171136                    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
 13140                    Object.Destroy(meshFilters[i].GetComponent<Renderer>());
 141                }
 142
 236143                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.
 236147                bool shouldCreateCollider = hasCollision || filterByColliderName;
 148
 236149                if (shouldCreateCollider)
 150                {
 220151                    if (collider == null)
 209152                        collider = meshFilters[i].gameObject.AddComponent<MeshCollider>();
 153
 220154                    if (collider is MeshCollider)
 217155                        ((MeshCollider) collider).sharedMesh = meshFilters[i].sharedMesh;
 156
 220157                    if (entity != null)
 220158                        AddOrUpdateEntityCollider(entity, collider);
 159                }
 160
 236161                if (collider != null)
 162                {
 231163                    collider.gameObject.layer = colliderLayer;
 231164                    collider.enabled = shouldCreateCollider;
 165
 231166                    if (entity != null)
 231167                        entity.meshesInfo.colliders.Add(collider);
 168                }
 169            }
 284170        }
 171    }
 172}