< 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:56
Uncovered lines:5
Coverable lines:61
Total lines:148
Line coverage:91.8% (56 of 61)
Covered branches:0
Total branches:0

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%4.184077.78%
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;
 628        private bool isInsideScene = true;
 29
 630        public AvatarAttachComponentHandler(IUpdateEventHandler updateEventHandler)
 31        {
 632            this.updateEventHandler = updateEventHandler;
 633            getAnchorPointsHandler = new GetAnchorPointsHandler();
 634            getAnchorPointsHandler.OnAvatarRemoved += Detach;
 635        }
 36
 37        public void OnComponentCreated(IParcelScene scene, IDCLEntity entity)
 38        {
 439            this.scene = scene;
 440            this.entity = entity;
 441        }
 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
 651            if (model == null || (prevModel != null && prevModel.AvatarId == model.AvatarId && model.AnchorPointId == pr
 152                return;
 53
 54            // Detach previous attachments
 555            Detach();
 556            Attach(model.AvatarId, (AvatarAnchorPointIds)model.AnchorPointId);
 57
 558            prevModel = model;
 559        }
 60
 61        public void Dispose()
 62        {
 663            Detach();
 664            getAnchorPointsHandler.OnAvatarRemoved -= Detach;
 665            getAnchorPointsHandler.Dispose();
 666        }
 67
 68        internal virtual void Detach()
 69        {
 1270            StopComponentUpdate();
 71
 1272            if (entity != null)
 973                entity.gameObject.transform.localPosition = EnvironmentSettings.MORDOR;
 74
 1275            getAnchorPointsHandler.CancelCurrentSearch();
 1276        }
 77
 78        internal virtual void Attach(string avatarId, AvatarAnchorPointIds anchorPointId)
 79        {
 580            getAnchorPointsHandler.SearchAnchorPoints(avatarId, anchorPoints =>
 81            {
 482                Attach(anchorPoints, anchorPointId);
 483            }, supportNullId: true);
 584        }
 85
 86        internal virtual void Attach(IAvatarAnchorPoints anchorPoints, AvatarAnchorPointIds anchorPointId)
 87        {
 488            this.anchorPoints = anchorPoints;
 489            this.anchorPointId = anchorPointId;
 90
 491            StartComponentUpdate();
 492        }
 93
 94        internal void LateUpdate()
 95        {
 296            if (entity == null || scene == null)
 97            {
 098                StopComponentUpdate();
 099                return;
 100            }
 101
 2102            var anchorPoint = anchorPoints.GetTransform(anchorPointId);
 103
 2104            if (IsInsideScene(CommonScriptableObjects.worldOffset + anchorPoint.position))
 105            {
 1106                entity.gameObject.transform.position = anchorPoint.position;
 1107                entity.gameObject.transform.rotation = anchorPoint.rotation;
 108            }
 109            else
 110            {
 1111                entity.gameObject.transform.localPosition = EnvironmentSettings.MORDOR;
 112            }
 1113        }
 114
 115        internal virtual bool IsInsideScene(UnityEngine.Vector3 position)
 116        {
 1117            bool result = isInsideScene;
 118
 1119            Vector2Int coords = Helpers.Utils.WorldToGridPosition(position);
 120
 1121            if (currentCoords == null || currentCoords != coords)
 1122                result = scene.IsInsideSceneBoundaries(coords, position.y);
 123
 1124            currentCoords = coords;
 1125            isInsideScene = result;
 1126            return result;
 127        }
 128
 129        private void StartComponentUpdate()
 130        {
 4131            if (componentUpdate != null)
 0132                return;
 133
 4134            currentCoords = null;
 4135            componentUpdate = LateUpdate;
 4136            updateEventHandler?.AddListener(IUpdateEventHandler.EventType.LateUpdate, componentUpdate);
 4137        }
 138
 139        private void StopComponentUpdate()
 140        {
 12141            if (componentUpdate == null)
 8142                return;
 143
 4144            updateEventHandler?.RemoveListener(IUpdateEventHandler.EventType.LateUpdate, componentUpdate);
 4145            componentUpdate = null;
 4146        }
 147    }
 148}