< 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:52
Uncovered lines:13
Coverable lines:65
Total lines:160
Line coverage:80% (52 of 65)
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%330100%
Attach(...)0%110100%
Attach(...)0%220100%
ComponentUpdate()0%7.027092.86%
IsInsideScene(...)0%56700%
CheckSceneBoundaries(...)0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/AvatarAttach/AvatarAttachHandler.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    internal class AvatarAttachHandler : IDisposable
 12    {
 13        private const float BOUNDARIES_CHECK_INTERVAL = 5;
 14
 715        public AvatarAttachComponent.Model model { internal set; get; } = new AvatarAttachComponent.Model();
 016        public IParcelScene scene { private set; get; }
 017        public IDCLEntity entity { private set; get; }
 18
 19        private AvatarAttachComponent.Model prevModel = null;
 20
 21        private IAvatarAnchorPoints anchorPoints;
 22        private AvatarAnchorPointIds anchorPointId;
 23
 24        private Coroutine componentUpdate = null;
 25
 726        private readonly GetAnchorPointsHandler getAnchorPointsHandler = new GetAnchorPointsHandler();
 127        private ISceneBoundsChecker sceneBoundsChecker => Environment.i?.world?.sceneBoundsChecker;
 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)
 34        {
 235            this.scene = scene;
 236            this.entity = entity;
 237            getAnchorPointsHandler.OnAvatarRemoved += Detach;
 238        }
 39
 40        public void OnModelUpdated(string json)
 41        {
 042            OnModelUpdated(model.GetDataFromJSON(json) as AvatarAttachComponent.Model);
 043        }
 44
 45        public void OnModelUpdated(AvatarAttachComponent.Model newModel)
 46        {
 747            prevModel = model;
 748            model = newModel;
 49
 750            if (model == null)
 51            {
 052                return;
 53            }
 54
 755            if (prevModel.avatarId != model.avatarId)
 56            {
 657                Detach();
 58
 659                if (!string.IsNullOrEmpty(model.avatarId))
 60                {
 561                    Attach(model.avatarId, (AvatarAnchorPointIds)model.anchorPointId);
 62                }
 63            }
 764        }
 65
 66        public void Dispose()
 67        {
 768            Detach();
 769            getAnchorPointsHandler.OnAvatarRemoved -= Detach;
 770            getAnchorPointsHandler.Dispose();
 771        }
 72
 73        internal virtual void Detach()
 74        {
 1375            if (componentUpdate != null)
 76            {
 277                CoroutineStarter.Stop(componentUpdate);
 278                componentUpdate = null;
 79            }
 80
 1381            if (entity != null)
 82            {
 483                entity.gameObject.transform.localPosition = EnvironmentSettings.MORDOR;
 84            }
 85
 1386            getAnchorPointsHandler.CancelCurrentSearch();
 1387        }
 88
 89        internal virtual void Attach(string avatarId, AvatarAnchorPointIds anchorPointId)
 90        {
 591            getAnchorPointsHandler.SearchAnchorPoints(avatarId, anchorPoints =>
 92            {
 493                Attach(anchorPoints, anchorPointId);
 494            });
 595        }
 96
 97        internal virtual void Attach(IAvatarAnchorPoints anchorPoints, AvatarAnchorPointIds anchorPointId)
 98        {
 499            this.anchorPoints = anchorPoints;
 4100            this.anchorPointId = anchorPointId;
 101
 4102            if (componentUpdate == null)
 103            {
 4104                componentUpdate = CoroutineStarter.Start(ComponentUpdate());
 105            }
 4106        }
 107
 108        IEnumerator ComponentUpdate()
 109        {
 4110            currentCoords = null;
 111
 0112            while (true)
 113            {
 4114                if (entity == null || scene == null)
 115                {
 2116                    componentUpdate = null;
 2117                    yield break;
 118                }
 119
 2120                var anchorPoint = anchorPoints.GetTransform(anchorPointId);
 121
 2122                if (IsInsideScene(CommonScriptableObjects.worldOffset + anchorPoint.position))
 123                {
 1124                    entity.gameObject.transform.position = anchorPoint.position;
 1125                    entity.gameObject.transform.rotation = anchorPoint.rotation;
 126
 1127                    if (Time.unscaledTime - lastBoundariesCheckTime > BOUNDARIES_CHECK_INTERVAL)
 128                    {
 1129                        CheckSceneBoundaries(entity);
 130                    }
 1131                }
 132                else
 133                {
 1134                    entity.gameObject.transform.localPosition = EnvironmentSettings.MORDOR;
 135                }
 136
 2137                yield return null;
 138            }
 139        }
 140
 141        internal virtual bool IsInsideScene(Vector3 position)
 142        {
 0143            bool result = isInsideScene;
 0144            Vector2Int coords = Utils.WorldToGridPosition(position);
 0145            if (currentCoords == null || currentCoords != coords)
 146            {
 0147                result = scene.IsInsideSceneBoundaries(coords, position.y);
 148            }
 0149            currentCoords = coords;
 0150            isInsideScene = result;
 0151            return result;
 152        }
 153
 154        private void CheckSceneBoundaries(IDCLEntity entity)
 155        {
 1156            sceneBoundsChecker?.AddEntityToBeChecked(entity);
 1157            lastBoundariesCheckTime = Time.unscaledTime;
 1158        }
 159    }
 160}