< 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:12
Coverable lines:69
Total lines:155
Line coverage:82.6% (57 of 69)
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%110100%
OnModelUpdated(...)0%5.035088.89%
Dispose()0%110100%
Detach()0%220100%
Attach(...)0%110100%
Attach(...)0%110100%
LateUpdate()0%4.184077.78%
IsInsideScene(...)0%56700%
StartComponentUpdate()0%4.374071.43%
StopComponentUpdate()0%550100%

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    {
 6012        public AvatarAttachComponent.Model model { internal set; get; } = new AvatarAttachComponent.Model();
 513        public IParcelScene scene { private set; get; }
 2714        public IDCLEntity entity { private set; get; }
 15
 16        private AvatarAttachComponent.Model prevModel = null;
 17
 18        private IAvatarAnchorPoints anchorPoints;
 19        private AvatarAnchorPointIds anchorPointId;
 20
 21        private Action componentUpdate = null;
 22
 823        private readonly GetAnchorPointsHandler getAnchorPointsHandler = new GetAnchorPointsHandler();
 1224        private ISceneBoundsChecker sceneBoundsChecker => Environment.i?.world?.sceneBoundsChecker;
 25        private IUpdateEventHandler updateEventHandler;
 26
 27        private Vector2Int? currentCoords = null;
 828        private bool isInsideScene = true;
 29
 30        public void Initialize(IParcelScene scene, IDCLEntity entity, IUpdateEventHandler updateEventHandler)
 31        {
 332            this.scene = scene;
 333            this.entity = entity;
 334            this.updateEventHandler = updateEventHandler;
 335            getAnchorPointsHandler.OnAvatarRemoved += Detach;
 336        }
 37
 38        public void OnModelUpdated(string json)
 39        {
 140            OnModelUpdated(model.GetDataFromJSON(json) as AvatarAttachComponent.Model);
 141        }
 42
 43        public void OnModelUpdated(AvatarAttachComponent.Model newModel)
 44        {
 845            prevModel = model;
 846            model = newModel;
 47
 848            if (model == null)
 49            {
 050                return;
 51            }
 52
 853            if (prevModel.avatarId != model.avatarId || prevModel.anchorPointId != model.anchorPointId)
 54            {
 655                Detach();
 56
 657                if (!string.IsNullOrEmpty(model.avatarId))
 58                {
 559                    Attach(model.avatarId, (AvatarAnchorPointIds)model.anchorPointId);
 60                }
 61            }
 862        }
 63
 64        public void Dispose()
 65        {
 866            Detach();
 867            getAnchorPointsHandler.OnAvatarRemoved -= Detach;
 868            getAnchorPointsHandler.Dispose();
 869        }
 70
 71        internal virtual void Detach()
 72        {
 1473            StopComponentUpdate();
 74
 1475            if (entity != null)
 76            {
 577                entity.gameObject.transform.localPosition = EnvironmentSettings.MORDOR;
 78            }
 79
 1480            getAnchorPointsHandler.CancelCurrentSearch();
 1481        }
 82
 83        internal virtual void Attach(string avatarId, AvatarAnchorPointIds anchorPointId)
 84        {
 585            getAnchorPointsHandler.SearchAnchorPoints(avatarId, anchorPoints =>
 86            {
 487                Attach(anchorPoints, anchorPointId);
 488            });
 589        }
 90
 91        internal virtual void Attach(IAvatarAnchorPoints anchorPoints, AvatarAnchorPointIds anchorPointId)
 92        {
 493            this.anchorPoints = anchorPoints;
 494            this.anchorPointId = anchorPointId;
 95
 496            StartComponentUpdate();
 497        }
 98
 99        internal void LateUpdate()
 100        {
 2101            if (entity == null || scene == null)
 102            {
 0103                StopComponentUpdate();
 0104                return;
 105            }
 106
 2107            var anchorPoint = anchorPoints.GetTransform(anchorPointId);
 108
 2109            if (IsInsideScene(CommonScriptableObjects.worldOffset + anchorPoint.position))
 110            {
 1111                entity.gameObject.transform.position = anchorPoint.position;
 1112                entity.gameObject.transform.rotation = anchorPoint.rotation;
 113            }
 114            else
 115            {
 1116                entity.gameObject.transform.localPosition = EnvironmentSettings.MORDOR;
 117            }
 1118        }
 119
 120        internal virtual bool IsInsideScene(Vector3 position)
 121        {
 0122            bool result = isInsideScene;
 0123            Vector2Int coords = Utils.WorldToGridPosition(position);
 0124            if (currentCoords == null || currentCoords != coords)
 125            {
 0126                result = scene.IsInsideSceneBoundaries(coords, position.y);
 127            }
 0128            currentCoords = coords;
 0129            isInsideScene = result;
 0130            return result;
 131        }
 132
 133        private void StartComponentUpdate()
 134        {
 4135            if (componentUpdate != null)
 0136                return;
 137
 4138            currentCoords = null;
 4139            componentUpdate = LateUpdate;
 4140            updateEventHandler?.AddListener(IUpdateEventHandler.EventType.LateUpdate, componentUpdate);
 4141            sceneBoundsChecker?.AddEntityToBeChecked(entity, isPersistent: true, runPreliminaryEvaluation: true);
 0142        }
 143
 144        private void StopComponentUpdate()
 145        {
 14146            if (componentUpdate == null)
 10147                return;
 148
 4149            updateEventHandler?.RemoveListener(IUpdateEventHandler.EventType.LateUpdate, componentUpdate);
 4150            sceneBoundsChecker?.RemoveEntity(entity, removeIfPersistent: true);
 4151            sceneBoundsChecker?.AddEntityToBeChecked(entity, isPersistent: false, runPreliminaryEvaluation: false);
 4152            componentUpdate = null;
 4153        }
 154    }
 155}