< Summary

Class:DCL.Components.CameraModeArea
Assembly:DCL.Components.CameraModeArea
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/CameraModeArea/CameraModeArea.cs
Covered lines:51
Uncovered lines:24
Coverable lines:75
Total lines:191
Line coverage:68% (51 of 75)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Model()0%110100%
GetDataFromJSON(...)0%2100%
CameraModeArea()0%110100%
CameraModeArea()0%110100%
get_cameraMode()0%110100%
get_entity()0%2100%
get_scene()0%2100%
get_componentName()0%2100%
UpdateFromJSON(...)0%2100%
UpdateFromModel(...)0%2100%
ApplyChanges(...)0%2100%
RaiseOnAppliedChanges()0%2100%
IsValid()0%2100%
GetModel()0%2100%
GetClassId()0%2100%
GetTransform()0%6200%
Cleanup()0%2100%
Initialize(...)0%12300%
OnModelUpdated(...)0%440100%
Initialize(...)0%110100%
Update()0%440100%
Dispose()0%110100%
IsValidCameraMode(...)0%220100%
IsPlayerInsideArea()0%7.017093.33%
OnAreaDisabled()0%220100%
OnOwnPlayerChange(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/CameraModeArea/CameraModeArea.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using DCL.Configuration;
 4using DCL.Controllers;
 5using DCL.Helpers;
 6using DCL.Models;
 7using UnityEngine;
 8
 9namespace DCL.Components
 10{
 11    public class CameraModeArea : IEntityComponent, ICameraModeArea
 12    {
 13        [Serializable]
 14        public class Model : BaseModel
 15        {
 16            [Serializable]
 17            public class Area
 18            {
 19                public Vector3 box;
 20            }
 21
 2022            public Area area = new Area();
 2023            public CameraMode.ModeId cameraMode = CameraMode.ModeId.ThirdPerson;
 24
 25            public override BaseModel GetDataFromJSON(string json)
 26            {
 027                return Utils.SafeFromJson<Model>(json);
 28            }
 29        }
 30
 131        private static CameraModeAreasController areasController { get; } = new CameraModeAreasController();
 32
 33        private Collider playerCollider;
 34        internal IUpdateEventHandler updateEventHandler;
 935        internal int validCameraModes = 1 << (int)CameraMode.ModeId.FirstPerson | 1 << (int)CameraMode.ModeId.ThirdPerso
 36
 37        internal bool isPlayerInside = false;
 38
 939        public Model areaModel { private set; get; } = new Model();
 040        public IParcelScene areaScene { private set; get; }
 141        public IDCLEntity areaEntity { private set; get; }
 42
 843        CameraMode.ModeId ICameraModeArea.cameraMode => areaModel.cameraMode;
 44
 045        IDCLEntity IEntityComponent.entity => areaEntity;
 46
 047        IParcelScene IComponent.scene => areaScene;
 48
 049        string IComponent.componentName => "CameraModeArea";
 50
 51        void IComponent.UpdateFromJSON(string json)
 52        {
 053            OnModelUpdated(areaModel.GetDataFromJSON(json) as Model);
 054        }
 55
 56        void IComponent.UpdateFromModel(BaseModel model)
 57        {
 058            OnModelUpdated(model as Model);
 059        }
 60
 061        IEnumerator IComponent.ApplyChanges(BaseModel model) => null;
 62
 063        void IComponent.RaiseOnAppliedChanges() { }
 64
 065        bool IComponent.IsValid() => true;
 66
 067        BaseModel IComponent.GetModel() => areaModel;
 68
 069        int IComponent.GetClassId() => (int)CLASS_ID_COMPONENT.CAMERA_MODE_AREA;
 70
 071        Transform IMonoBehaviour.GetTransform() => areaEntity?.gameObject.transform;
 72
 73        void ICleanable.Cleanup()
 74        {
 075            Dispose();
 076            DataStore.i.player.ownPlayer.OnChange -= OnOwnPlayerChange;
 077        }
 78
 79        void IEntityComponent.Initialize(IParcelScene scene, IDCLEntity entity)
 80        {
 081            Initialize(scene, entity, Environment.i.platform.updateEventHandler, DataStore.i.player.ownPlayer.Get()?.col
 082            DataStore.i.player.ownPlayer.OnChange += OnOwnPlayerChange;
 083        }
 84
 85        internal void OnModelUpdated(in Model newModel)
 86        {
 87            // NOTE: we don't want the ecs to be able to set "builder in world" camera or any other camera
 88            // that might be added in the future. Only first and third person camera are allowed
 1189            if (!IsValidCameraMode(newModel.cameraMode))
 90            {
 191                newModel.cameraMode = CommonScriptableObjects.cameraMode.Get();
 92            }
 93
 1194            bool cameraModeChanged = newModel.cameraMode != areaModel.cameraMode;
 1195            areaModel = newModel;
 96
 1197            if (cameraModeChanged && isPlayerInside)
 98            {
 199                areasController.ChangeAreaMode(this, areaModel.cameraMode);
 100            }
 11101        }
 102
 103        internal void Initialize(in IParcelScene scene, in IDCLEntity entity, in IUpdateEventHandler updateEventHandler,
 104        {
 9105            areaScene = scene;
 9106            areaEntity = entity;
 9107            this.updateEventHandler = updateEventHandler;
 9108            this.playerCollider = playerCollider;
 109
 9110            updateEventHandler.AddListener(IUpdateEventHandler.EventType.Update, Update);
 9111        }
 112
 113        internal void Update()
 114        {
 22115            bool playerInside = IsPlayerInsideArea();
 116
 117            switch (playerInside)
 118            {
 11119                case true when !isPlayerInside:
 8120                    areasController.AddInsideArea(this);
 8121                    break;
 11122                case false when isPlayerInside:
 7123                    areasController.RemoveInsideArea(this);
 124                    break;
 125            }
 22126            isPlayerInside = playerInside;
 22127        }
 128
 129        internal void Dispose()
 130        {
 9131            OnAreaDisabled();
 9132            updateEventHandler.RemoveListener(IUpdateEventHandler.EventType.Update, Update);
 9133        }
 134
 135        private bool IsValidCameraMode(in CameraMode.ModeId mode)
 136        {
 11137            if (validCameraModes == -1)
 9138                return true;
 139
 2140            return ((1 << (int)mode) & validCameraModes) != 0;
 141        }
 142
 143        private bool IsPlayerInsideArea()
 144        {
 22145            if (areaEntity == null || areaScene == null)
 146            {
 0147                return false;
 148            }
 149
 22150            if (areaScene.sceneData.id != CommonScriptableObjects.sceneID.Get())
 151            {
 1152                return false;
 153            }
 154
 21155            Vector3 center = areaEntity.gameObject.transform.position;
 21156            Quaternion rotation = areaEntity.gameObject.transform.rotation;
 157
 21158            Collider[] colliders = Physics.OverlapBox(center, areaModel.area.box * 0.5f, rotation,
 159                PhysicsLayers.avatarTriggerMask, QueryTriggerInteraction.Collide);
 160
 21161            if (colliders.Length == 0)
 162            {
 5163                return false;
 164            }
 165
 84166            for (int i = 0; i < colliders.Length; i++)
 167            {
 37168                if (colliders[i] == playerCollider)
 169                {
 11170                    return true;
 171                }
 172            }
 5173            return false;
 174        }
 175
 176        private void OnAreaDisabled()
 177        {
 9178            if (!isPlayerInside)
 179            {
 8180                return;
 181            }
 1182            areasController.RemoveInsideArea(this);
 1183            isPlayerInside = false;
 1184        }
 185
 186        private void OnOwnPlayerChange(Player current, Player previous)
 187        {
 0188            playerCollider = current.collider;
 0189        }
 190    }
 191}