< 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:55
Uncovered lines:8
Coverable lines:63
Total lines:153
Line coverage:87.3% (55 of 63)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarAttachComponentHandler(...)0%110100%
OnComponentCreated(...)0%2100%
OnComponentRemoved(...)0%2100%
OnComponentModelUpdated(...)0%660100%
Dispose()0%110100%
Detach()0%220100%
Attach(...)0%110100%
Attach(...)0%110100%
LateUpdate()0%4.134080%
IsInsideScene(...)0%770100%
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.ECSRuntime;
 6using DCL.Models;
 7using UnityEngine;
 8
 9namespace DCL.ECSComponents
 10{
 11    public class AvatarAttachComponentHandler : IECSComponentHandler<PBAvatarAttach>
 12    {
 13        private const float BOUNDARIES_CHECK_INTERVAL = 5;
 14
 15        internal PBAvatarAttach prevModel = null;
 16
 17        internal IAvatarAnchorPoints anchorPoints;
 18        internal AvatarAnchorPointIds anchorPointId;
 19        private IDCLEntity entity;
 20        private IParcelScene scene;
 21
 22        private Action componentUpdate = null;
 23
 24        internal readonly GetAnchorPointsHandler getAnchorPointsHandler;
 25        private readonly IUpdateEventHandler updateEventHandler;
 26
 27        private Vector2Int? currentCoords = null;
 728        private bool isInsideScene = true;
 29
 730        public AvatarAttachComponentHandler(IUpdateEventHandler updateEventHandler)
 31        {
 732            this.updateEventHandler = updateEventHandler;
 733            getAnchorPointsHandler = new GetAnchorPointsHandler();
 734            getAnchorPointsHandler.OnAvatarRemoved += Detach;
 735        }
 36
 37        public void OnComponentCreated(IParcelScene scene, IDCLEntity entity)
 38        {
 039            this.scene = scene;
 040            this.entity = entity;
 041        }
 42
 43        public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity)
 44        {
 045            Dispose();
 046        }
 47
 48        public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBAvatarAttach model)
 49        {
 50            // If is the same model, we skip
 751            if (model == null || (prevModel != null && prevModel.AvatarId == model.AvatarId && model.AnchorPointId == pr
 152                return;
 53
 54            // Detach previous attachments
 655            Detach();
 56
 657            if (!string.IsNullOrEmpty(model.AvatarId))
 58            {
 59                // Attach the anchor point
 560                Attach(model.AvatarId, (AvatarAnchorPointIds)model.AnchorPointId);
 61            }
 62
 663            prevModel = model;
 664        }
 65
 66        public void Dispose()
 67        {
 768            Detach();
 769            getAnchorPointsHandler.OnAvatarRemoved -= Detach;
 770            getAnchorPointsHandler.Dispose();
 771        }
 72
 73        internal virtual void Detach()
 74        {
 1475            StopComponentUpdate();
 76
 1477            if (entity != null)
 978                entity.gameObject.transform.localPosition = EnvironmentSettings.MORDOR;
 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;
 1113            }
 114            else
 115            {
 1116                entity.gameObject.transform.localPosition = EnvironmentSettings.MORDOR;
 117            }
 1118        }
 119
 120        internal virtual bool IsInsideScene(UnityEngine.Vector3 position)
 121        {
 1122            bool result = isInsideScene;
 123
 1124            Vector2Int coords = Helpers.Utils.WorldToGridPosition(position);
 125
 1126            if (currentCoords == null || currentCoords != coords)
 1127                result = scene.IsInsideSceneBoundaries(coords, position.y);
 128
 1129            currentCoords = coords;
 1130            isInsideScene = result;
 1131            return result;
 132        }
 133
 134        private void StartComponentUpdate()
 135        {
 4136            if (componentUpdate != null)
 0137                return;
 138
 4139            currentCoords = null;
 4140            componentUpdate = LateUpdate;
 4141            updateEventHandler?.AddListener(IUpdateEventHandler.EventType.LateUpdate, componentUpdate);
 4142        }
 143
 144        private void StopComponentUpdate()
 145        {
 14146            if (componentUpdate == null)
 10147                return;
 148
 4149            updateEventHandler?.RemoveListener(IUpdateEventHandler.EventType.LateUpdate, componentUpdate);
 4150            componentUpdate = null;
 4151        }
 152    }
 153}