< Summary

Class:DCL.AvatarShape
Assembly:AvatarShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarShape.cs
Covered lines:41
Uncovered lines:57
Coverable lines:98
Total lines:223
Line coverage:41.8% (41 of 98)
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%22.417032%
UpdatePlayerStatus(...)0%90900%
Update()0%2.52050%
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        internal AvatarOnPointerDown onPointerDown;
 22
 23        private StringVariable currentPlayerInfoCardId;
 24
 65525        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        {
 65438            model = new AvatarModel();
 65439            currentPlayerInfoCardId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID);
 65440            avatarRenderer.OnImpostorAlphaValueUpdate += OnImpostorAlphaValueUpdate;
 65441        }
 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        {
 65452            Cleanup();
 53
 65454            if (poolableObject != null && poolableObject.isInsidePool)
 255                poolableObject.RemoveFromPool();
 56
 65457            avatarRenderer.OnImpostorAlphaValueUpdate -= OnImpostorAlphaValueUpdate;
 65458        }
 59
 60        public override IEnumerator ApplyChanges(BaseModel newModel)
 61        {
 262            DisablePassport();
 63
 264            var model = (AvatarModel) newModel;
 65
 266            everythingIsLoaded = false;
 67
 268            bool avatarDone = false;
 269            bool avatarFailed = false;
 70
 271            yield return null; //NOTE(Brian): just in case we have a Object.Destroy waiting to be resolved.
 72
 173            avatarRenderer.ApplyModel(model, () => avatarDone = true, () => avatarFailed = true);
 74
 275            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            KernelConfig.i.EnsureConfigInitialized()
 116                        .Then(config =>
 117                        {
 0118                            if(config.features.enableAvatarLODs)
 0119                                avatarRenderer.InitializeImpostor();
 0120                        });
 0121        }
 122
 123        private void UpdatePlayerStatus(AvatarModel model)
 124        {
 125            // Remove the player status if the userId changes
 0126            if (player != null && (player.id != model.id || player.name != model.name))
 0127                otherPlayers.Remove(player.id);
 128
 0129            if (string.IsNullOrEmpty(model?.id))
 0130                return;
 131
 0132            bool isNew = false;
 0133            if (player == null)
 134            {
 0135                player = new Player();
 0136                isNew = true;
 137            }
 0138            player.id = model.id;
 0139            player.name = model.name;
 0140            player.isTalking = model.talking;
 0141            player.worldPosition = entity.gameObject.transform.position;
 0142            player.renderer = avatarRenderer;
 0143            if (isNew)
 144            {
 0145                visibleNames.Add(player.id);
 0146                otherPlayers.Add(player.id, player);
 147            }
 0148        }
 149
 150        private void Update()
 151        {
 1152            if (player != null)
 153            {
 0154                player.worldPosition = entity.gameObject.transform.position;
 0155                player.forwardDirection = entity.gameObject.transform.forward;
 156            }
 1157        }
 158
 159        public void DisablePassport()
 160        {
 2161            if (onPointerDown.collider == null)
 0162                return;
 163
 2164            onPointerDown.collider.enabled = false;
 2165        }
 166
 167        public void EnablePassport()
 168        {
 0169            if (onPointerDown.collider == null)
 0170                return;
 171
 0172            onPointerDown.collider.enabled = true;
 0173        }
 174
 175        private void OnEntityTransformChanged(object newModel)
 176        {
 0177            DCLTransform.Model newTransformModel = (DCLTransform.Model)newModel;
 0178            lastAvatarPosition = newTransformModel.position;
 0179        }
 180
 181        public override void OnPoolGet()
 182        {
 2183            base.OnPoolGet();
 184
 2185            everythingIsLoaded = false;
 2186            initializedPosition = false;
 2187            oldModel = new AvatarModel();
 2188            model = new AvatarModel();
 2189            lastAvatarPosition = null;
 2190            player = null;
 2191        }
 192
 0193        void OnImpostorAlphaValueUpdate(float newAlphaValue) { avatarMovementController.movementLerpWait = newAlphaValue
 194
 195        public override void Cleanup()
 196        {
 658197            base.Cleanup();
 198
 658199            if (player != null)
 200            {
 0201                otherPlayers.Remove(player.id);
 0202                player = null;
 203            }
 204
 658205            avatarRenderer.CleanupAvatar();
 206
 658207            if (poolableObject != null)
 208            {
 6209                poolableObject.OnRelease -= Cleanup;
 210            }
 211
 658212            onPointerDown.OnPointerDownReport -= PlayerClicked;
 213
 658214            if (entity != null)
 215            {
 2216                entity.OnTransformChange = null;
 2217                entity = null;
 218            }
 658219        }
 220
 0221        public override int GetClassId() { return (int) CLASS_ID_COMPONENT.AVATAR_SHAPE; }
 222    }
 223}