< Summary

Class:DCL.ECSComponents.AvatarAttachComponentHandler
Assembly:DCL.ECSComponents.AvatarAttach
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/AvatarAttach/Handler/AvatarAttachComponentHandler.cs
Covered lines:49
Uncovered lines:5
Coverable lines:54
Total lines:130
Line coverage:90.7% (49 of 54)
Covered branches:0
Total branches:0
Covered methods:10
Total methods:11
Method coverage:90.9% (10 of 11)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarAttachComponentHandler(...)0%110100%
OnComponentCreated(...)0%110100%
OnComponentRemoved(...)0%2100%
OnComponentModelUpdated(...)0%550100%
Dispose()0%110100%
Detach()0%220100%
Attach(...)0%110100%
Attach(...)0%110100%
LateUpdate()0%3.143075%
StartComponentUpdate()0%3.043083.33%
StopComponentUpdate()0%330100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/AvatarAttach/Handler/AvatarAttachComponentHandler.cs

#LineLine coverage
 1using System;
 2using DCL.Components;
 3using DCL.Configuration;
 4using DCL.Controllers;
 5using DCL.ECS7.InternalComponents;
 6using DCL.ECSRuntime;
 7using DCL.Models;
 8using UnityEngine;
 9
 10namespace DCL.ECSComponents
 11{
 12    public class AvatarAttachComponentHandler : IECSComponentHandler<PBAvatarAttach>
 13    {
 14        internal PBAvatarAttach prevModel = null;
 15
 16        internal IAvatarAnchorPoints anchorPoints;
 17        internal AvatarAnchorPointIds anchorPointId;
 18        private IDCLEntity entity;
 19        private IParcelScene scene;
 20
 21        private Action componentUpdate = null;
 22
 23        internal readonly GetAnchorPointsHandler getAnchorPointsHandler;
 24        private readonly IUpdateEventHandler updateEventHandler;
 25        private readonly IInternalECSComponent<InternalSceneBoundsCheck> sbcInternalComponent;
 26
 27        private Vector2Int? currentCoords = null;
 528        private bool isInsideScene = true;
 29
 530        public AvatarAttachComponentHandler(IUpdateEventHandler updateEventHandler, IInternalECSComponent<InternalSceneB
 31        {
 532            this.updateEventHandler = updateEventHandler;
 533            this.sbcInternalComponent = sbcInternalComponent;
 34
 535            getAnchorPointsHandler = new GetAnchorPointsHandler();
 536            getAnchorPointsHandler.OnAvatarRemoved += Detach;
 537        }
 38
 39        public void OnComponentCreated(IParcelScene scene, IDCLEntity entity)
 40        {
 341            this.scene = scene;
 342            this.entity = entity;
 343        }
 44
 45        public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity)
 46        {
 047            Dispose();
 048        }
 49
 50        public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBAvatarAttach model)
 51        {
 52            // If is the same model, we skip
 553            if (model == null || (prevModel != null && prevModel.AvatarId == model.AvatarId && model.AnchorPointId == pr
 154                return;
 55
 56            // Detach previous attachments
 457            Detach();
 458            Attach(model.AvatarId, (AvatarAnchorPointIds)model.AnchorPointId);
 59
 460            prevModel = model;
 461        }
 62
 63        public void Dispose()
 64        {
 565            Detach();
 566            getAnchorPointsHandler.OnAvatarRemoved -= Detach;
 567            getAnchorPointsHandler.Dispose();
 568        }
 69
 70        internal virtual void Detach()
 71        {
 1072            StopComponentUpdate();
 73
 1074            if (entity != null)
 775                entity.gameObject.transform.localPosition = EnvironmentSettings.MORDOR;
 76
 1077            getAnchorPointsHandler.CancelCurrentSearch();
 1078        }
 79
 80        internal virtual void Attach(string avatarId, AvatarAnchorPointIds anchorPointId)
 81        {
 482            getAnchorPointsHandler.SearchAnchorPoints(avatarId, anchorPoints =>
 83            {
 384                Attach(anchorPoints, anchorPointId);
 385            }, supportNullId: true);
 486        }
 87
 88        internal virtual void Attach(IAvatarAnchorPoints anchorPoints, AvatarAnchorPointIds anchorPointId)
 89        {
 390            this.anchorPoints = anchorPoints;
 391            this.anchorPointId = anchorPointId;
 92
 393            StartComponentUpdate();
 394        }
 95
 96        internal void LateUpdate()
 97        {
 198            if (entity == null || scene == null)
 99            {
 0100                StopComponentUpdate();
 0101                return;
 102            }
 103
 1104            var anchorPoint = anchorPoints.GetTransform(anchorPointId);
 105
 1106            entity.gameObject.transform.position = anchorPoint.position;
 1107            entity.gameObject.transform.rotation = anchorPoint.rotation;
 1108            sbcInternalComponent.SetPosition(scene, entity, anchorPoint.position);
 1109        }
 110
 111        private void StartComponentUpdate()
 112        {
 3113            if (componentUpdate != null)
 0114                return;
 115
 3116            currentCoords = null;
 3117            componentUpdate = LateUpdate;
 3118            updateEventHandler?.AddListener(IUpdateEventHandler.EventType.LateUpdate, componentUpdate);
 3119        }
 120
 121        private void StopComponentUpdate()
 122        {
 10123            if (componentUpdate == null)
 7124                return;
 125
 3126            updateEventHandler?.RemoveListener(IUpdateEventHandler.EventType.LateUpdate, componentUpdate);
 3127            componentUpdate = null;
 3128        }
 129    }
 130}