< Summary

Class:DCL.AvatarShape
Assembly:AvatarShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarShape.cs
Covered lines:37
Uncovered lines:58
Coverable lines:95
Total lines:218
Line coverage:38.9% (37 of 95)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarShape()0%110100%
Awake()0%110100%
PlayerClicked()0%6200%
OnDestroy()0%330100%
ApplyChanges()0%28.517024%
UpdatePlayerStatus(...)0%90900%
Update()0%6200%
DisablePassport()0%2.062075%
EnablePassport()0%6200%
OnEntityTransformChanged(...)0%2100%
OnPoolGet()0%110100%
OnImpostorAlphaValueUpdate(...)0%12300%
Cleanup()0%4.074083.33%
GetClassId()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarShape.cs

#LineLine coverage
 1using System;
 2using DCL.Components;
 3using DCL.Interface;
 4using System.Collections;
 5using DCL.Models;
 6using UnityEngine;
 7
 8namespace DCL
 9{
 10    public class AvatarShape : BaseComponent
 11    {
 12        private const string CURRENT_PLAYER_ID = "CurrentPlayerInfoCardId";
 13
 14        public static event Action<IDCLEntity, AvatarShape> OnAvatarShapeUpdated;
 15
 16        public AvatarRenderer avatarRenderer;
 17        public Collider avatarCollider;
 18        public AvatarMovementController avatarMovementController;
 19
 20        [SerializeField]
 21        private AvatarOnPointerDown onPointerDown;
 22
 23        private StringVariable currentPlayerInfoCardId;
 24
 65425        private AvatarModel oldModel = new AvatarModel();
 26
 27        public bool everythingIsLoaded;
 28
 29        private Vector3? lastAvatarPosition = null;
 30        bool initializedPosition = false;
 31
 32        private Player player = null;
 033        private BaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers;
 034        private BaseHashSet<string> visibleNames => DataStore.i.avatarsLOD.visibleNames;
 35
 36        private void Awake()
 37        {
 65338            model = new AvatarModel();
 65339            currentPlayerInfoCardId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID);
 65340            avatarRenderer.OnImpostorAlphaValueUpdate += OnImpostorAlphaValueUpdate;
 65341        }
 42
 43        private void PlayerClicked()
 44        {
 045            if (model == null)
 046                return;
 047            currentPlayerInfoCardId.Set(((AvatarModel) model).id);
 048        }
 49
 50        public void OnDestroy()
 51        {
 65352            Cleanup();
 53
 65354            if (poolableObject != null && poolableObject.isInsidePool)
 155                poolableObject.RemoveFromPool();
 56
 65357            avatarRenderer.OnImpostorAlphaValueUpdate -= OnImpostorAlphaValueUpdate;
 65358        }
 59
 60        public override IEnumerator ApplyChanges(BaseModel newModel)
 61        {
 162            DisablePassport();
 63
 164            var model = (AvatarModel) newModel;
 65
 166            everythingIsLoaded = false;
 67
 168            bool avatarDone = false;
 169            bool avatarFailed = false;
 70
 171            yield return null; //NOTE(Brian): just in case we have a Object.Destroy waiting to be resolved.
 72
 073            avatarRenderer.ApplyModel(model, () => avatarDone = true, () => avatarFailed = true);
 74
 075            yield return new WaitUntil(() => avatarDone || avatarFailed);
 76
 077            onPointerDown.Initialize(
 78                new OnPointerDown.Model()
 79                {
 80                    type = OnPointerDown.NAME,
 81                    button = WebInterface.ACTION_BUTTON.POINTER.ToString(),
 82                    hoverText = "view profile"
 83                },
 84                entity
 85            );
 86
 087            entity.OnTransformChange -= avatarMovementController.OnTransformChanged;
 088            entity.OnTransformChange += avatarMovementController.OnTransformChanged;
 89
 090            entity.OnTransformChange -= OnEntityTransformChanged;
 091            entity.OnTransformChange += OnEntityTransformChanged;
 92
 093            onPointerDown.OnPointerDownReport -= PlayerClicked;
 094            onPointerDown.OnPointerDownReport += PlayerClicked;
 95
 96            // To deal with the cases in which the entity transform was configured before the AvatarShape
 097            if (!initializedPosition && entity.components.ContainsKey(DCL.Models.CLASS_ID_COMPONENT.TRANSFORM))
 98            {
 099                initializedPosition = true;
 100
 0101                avatarMovementController.MoveTo(
 102                    entity.gameObject.transform.localPosition - Vector3.up * DCLCharacterController.i.characterControlle
 103                    entity.gameObject.transform.localRotation, true);
 104            }
 105
 0106            UpdatePlayerStatus(model);
 107
 0108            avatarCollider.gameObject.SetActive(true);
 109
 0110            everythingIsLoaded = true;
 0111            OnAvatarShapeUpdated?.Invoke(entity, this);
 112
 0113            EnablePassport();
 114
 0115            avatarRenderer.InitializeImpostor();
 0116        }
 117
 118        private void UpdatePlayerStatus(AvatarModel model)
 119        {
 120            // Remove the player status if the userId changes
 0121            if (player != null && (player.id != model.id || player.name != model.name))
 0122                otherPlayers.Remove(player.id);
 123
 0124            if (string.IsNullOrEmpty(model?.id))
 0125                return;
 126
 0127            bool isNew = false;
 0128            if (player == null)
 129            {
 0130                player = new Player();
 0131                isNew = true;
 132            }
 0133            player.id = model.id;
 0134            player.name = model.name;
 0135            player.isTalking = model.talking;
 0136            player.worldPosition = entity.gameObject.transform.position;
 0137            player.renderer = avatarRenderer;
 0138            if (isNew)
 139            {
 0140                visibleNames.Add(player.id);
 0141                otherPlayers.Add(player.id, player);
 142            }
 0143        }
 144
 145        private void Update()
 146        {
 0147            if (player != null)
 148            {
 0149                player.worldPosition = entity.gameObject.transform.position;
 0150                player.forwardDirection = entity.gameObject.transform.forward;
 151            }
 0152        }
 153
 154        public void DisablePassport()
 155        {
 1156            if (onPointerDown.collider == null)
 0157                return;
 158
 1159            onPointerDown.collider.enabled = false;
 1160        }
 161
 162        public void EnablePassport()
 163        {
 0164            if (onPointerDown.collider == null)
 0165                return;
 166
 0167            onPointerDown.collider.enabled = true;
 0168        }
 169
 170        private void OnEntityTransformChanged(object newModel)
 171        {
 0172            DCLTransform.Model newTransformModel = (DCLTransform.Model)newModel;
 0173            lastAvatarPosition = newTransformModel.position;
 0174        }
 175
 176        public override void OnPoolGet()
 177        {
 1178            base.OnPoolGet();
 179
 1180            everythingIsLoaded = false;
 1181            initializedPosition = false;
 1182            oldModel = new AvatarModel();
 1183            model = new AvatarModel();
 1184            lastAvatarPosition = null;
 1185            player = null;
 1186        }
 187
 0188        void OnImpostorAlphaValueUpdate(float newAlphaValue) { avatarMovementController.movementLerpWait = newAlphaValue
 189
 190        public override void Cleanup()
 191        {
 655192            base.Cleanup();
 193
 655194            if (player != null)
 195            {
 0196                otherPlayers.Remove(player.id);
 0197                player = null;
 198            }
 199
 655200            avatarRenderer.CleanupAvatar();
 201
 655202            if (poolableObject != null)
 203            {
 3204                poolableObject.OnRelease -= Cleanup;
 205            }
 206
 655207            onPointerDown.OnPointerDownReport -= PlayerClicked;
 208
 655209            if (entity != null)
 210            {
 1211                entity.OnTransformChange = null;
 1212                entity = null;
 213            }
 655214        }
 215
 0216        public override int GetClassId() { return (int) CLASS_ID_COMPONENT.AVATAR_SHAPE; }
 217    }
 218}