< Summary

Class:DCL.Components.AvatarAttachHandler
Assembly:AvatarAttach
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/AvatarAttach/AvatarAttachHandler.cs
Covered lines:57
Uncovered lines:15
Coverable lines:72
Total lines:166
Line coverage:79.1% (57 of 72)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarAttachHandler()0%110100%
Initialize(...)0%110100%
OnModelUpdated(...)0%2100%
OnModelUpdated(...)0%4.024088.89%
Dispose()0%110100%
Detach()0%220100%
Attach(...)0%110100%
Attach(...)0%110100%
LateUpdate()0%5.125083.33%
IsInsideScene(...)0%56700%
CheckSceneBoundaries(...)0%220100%
StartComponentUpdate()0%3.043083.33%
StopComponentUpdate()0%330100%

File(s)

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

#LineLine coverage
 1using System;
 2using DCL.Configuration;
 3using DCL.Controllers;
 4using DCL.Helpers;
 5using DCL.Models;
 6using UnityEngine;
 7
 8namespace DCL.Components
 9{
 10    internal class AvatarAttachHandler : IDisposable
 11    {
 12        private const float BOUNDARIES_CHECK_INTERVAL = 5;
 13
 714        public AvatarAttachComponent.Model model { internal set; get; } = new AvatarAttachComponent.Model();
 015        public IParcelScene scene { private set; get; }
 016        public IDCLEntity entity { private set; get; }
 17
 18        private AvatarAttachComponent.Model prevModel = null;
 19
 20        private IAvatarAnchorPoints anchorPoints;
 21        private AvatarAnchorPointIds anchorPointId;
 22
 23        private Action componentUpdate = null;
 24
 725        private readonly GetAnchorPointsHandler getAnchorPointsHandler = new GetAnchorPointsHandler();
 126        private ISceneBoundsChecker sceneBoundsChecker => Environment.i?.world?.sceneBoundsChecker;
 27        private IUpdateEventHandler updateEventHandler;
 28
 29        private Vector2Int? currentCoords = null;
 730        private bool isInsideScene = true;
 31        private float lastBoundariesCheckTime = 0;
 32
 33        public void Initialize(IParcelScene scene, IDCLEntity entity, IUpdateEventHandler updateEventHandler)
 34        {
 235            this.scene = scene;
 236            this.entity = entity;
 237            this.updateEventHandler = updateEventHandler;
 238            getAnchorPointsHandler.OnAvatarRemoved += Detach;
 239        }
 40
 41        public void OnModelUpdated(string json)
 42        {
 043            OnModelUpdated(model.GetDataFromJSON(json) as AvatarAttachComponent.Model);
 044        }
 45
 46        public void OnModelUpdated(AvatarAttachComponent.Model newModel)
 47        {
 748            prevModel = model;
 749            model = newModel;
 50
 751            if (model == null)
 52            {
 053                return;
 54            }
 55
 756            if (prevModel.avatarId != model.avatarId)
 57            {
 658                Detach();
 59
 660                if (!string.IsNullOrEmpty(model.avatarId))
 61                {
 562                    Attach(model.avatarId, (AvatarAnchorPointIds)model.anchorPointId);
 63                }
 64            }
 765        }
 66
 67        public void Dispose()
 68        {
 769            Detach();
 770            getAnchorPointsHandler.OnAvatarRemoved -= Detach;
 771            getAnchorPointsHandler.Dispose();
 772        }
 73
 74        internal virtual void Detach()
 75        {
 1376            StopComponentUpdate();
 77
 1378            if (entity != null)
 79            {
 480                entity.gameObject.transform.localPosition = EnvironmentSettings.MORDOR;
 81            }
 82
 1383            getAnchorPointsHandler.CancelCurrentSearch();
 1384        }
 85
 86        internal virtual void Attach(string avatarId, AvatarAnchorPointIds anchorPointId)
 87        {
 588            getAnchorPointsHandler.SearchAnchorPoints(avatarId, anchorPoints =>
 89            {
 490                Attach(anchorPoints, anchorPointId);
 491            });
 592        }
 93
 94        internal virtual void Attach(IAvatarAnchorPoints anchorPoints, AvatarAnchorPointIds anchorPointId)
 95        {
 496            this.anchorPoints = anchorPoints;
 497            this.anchorPointId = anchorPointId;
 98
 499            StartComponentUpdate();
 4100        }
 101
 102        internal void LateUpdate()
 103        {
 2104            if (entity == null || scene == null)
 105            {
 0106                StopComponentUpdate();
 0107                return;
 108            }
 109
 2110            var anchorPoint = anchorPoints.GetTransform(anchorPointId);
 111
 2112            if (IsInsideScene(CommonScriptableObjects.worldOffset + anchorPoint.position))
 113            {
 1114                entity.gameObject.transform.position = anchorPoint.position;
 1115                entity.gameObject.transform.rotation = anchorPoint.rotation;
 116
 1117                if (Time.unscaledTime - lastBoundariesCheckTime > BOUNDARIES_CHECK_INTERVAL)
 118                {
 1119                    CheckSceneBoundaries(entity);
 120                }
 1121            }
 122            else
 123            {
 1124                entity.gameObject.transform.localPosition = EnvironmentSettings.MORDOR;
 125            }
 1126        }
 127
 128        internal virtual bool IsInsideScene(Vector3 position)
 129        {
 0130            bool result = isInsideScene;
 0131            Vector2Int coords = Utils.WorldToGridPosition(position);
 0132            if (currentCoords == null || currentCoords != coords)
 133            {
 0134                result = scene.IsInsideSceneBoundaries(coords, position.y);
 135            }
 0136            currentCoords = coords;
 0137            isInsideScene = result;
 0138            return result;
 139        }
 140
 141        private void CheckSceneBoundaries(IDCLEntity entity)
 142        {
 1143            sceneBoundsChecker?.AddEntityToBeChecked(entity);
 1144            lastBoundariesCheckTime = Time.unscaledTime;
 1145        }
 146
 147        private void StartComponentUpdate()
 148        {
 4149            if (componentUpdate != null)
 0150                return;
 151
 4152            currentCoords = null;
 4153            componentUpdate = LateUpdate;
 4154            updateEventHandler?.AddListener(IUpdateEventHandler.EventType.LateUpdate, componentUpdate);
 2155        }
 156
 157        private void StopComponentUpdate()
 158        {
 13159            if (componentUpdate == null)
 9160                return;
 161
 4162            updateEventHandler?.RemoveListener(IUpdateEventHandler.EventType.LateUpdate, componentUpdate);
 4163            componentUpdate = null;
 4164        }
 165    }
 166}