< Summary

Class:DCL.AvatarShape
Assembly:AvatarShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarShape.cs
Covered lines:51
Uncovered lines:64
Coverable lines:115
Total lines:249
Line coverage:44.3% (51 of 115)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarShape()0%110100%
Awake()0%110100%
Start()0%220100%
PlayerClicked()0%6200%
OnDestroy()0%330100%
ApplyChanges()0%17.587040%
PlayerPointerExit()0%6200%
PlayerPointerEnter()0%6200%
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%5.065086.67%
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        private const float MINIMUM_PLAYERNAME_HEIGHT = 2.7f;
 14        private const float AVATAR_PASSPORT_TOGGLE_ALPHA_THRESHOLD = 0.9f;
 15
 16        public static event Action<IDCLEntity, AvatarShape> OnAvatarShapeUpdated;
 17
 18        public AvatarRenderer avatarRenderer;
 19        public Collider avatarCollider;
 20        public AvatarMovementController avatarMovementController;
 21
 22        [SerializeField] internal AvatarOnPointerDown onPointerDown;
 23        internal IPlayerName playerName;
 24
 25        private StringVariable currentPlayerInfoCardId;
 26
 67227        private AvatarModel oldModel = new AvatarModel();
 28
 29        public bool everythingIsLoaded;
 30
 31        private Vector3? lastAvatarPosition = null;
 32        bool initializedPosition = false;
 33
 34        private Player player = null;
 035        private BaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers;
 36
 37        private void Awake()
 38        {
 67139            model = new AvatarModel();
 67140            currentPlayerInfoCardId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID);
 67141            avatarRenderer.OnImpostorAlphaValueUpdate += OnImpostorAlphaValueUpdate;
 67142        }
 43
 44        private void Start()
 45        {
 146            playerName = GetComponentInChildren<IPlayerName>();
 147            playerName?.Hide(true);
 148        }
 49
 50        private void PlayerClicked()
 51        {
 052            if (model == null)
 053                return;
 054            currentPlayerInfoCardId.Set(((AvatarModel) model).id);
 055        }
 56
 57        public void OnDestroy()
 58        {
 67159            Cleanup();
 60
 67161            if (poolableObject != null && poolableObject.isInsidePool)
 262                poolableObject.RemoveFromPool();
 63
 67164            avatarRenderer.OnImpostorAlphaValueUpdate -= OnImpostorAlphaValueUpdate;
 67165        }
 66
 67        public override IEnumerator ApplyChanges(BaseModel newModel)
 68        {
 269            DisablePassport();
 70
 271            var model = (AvatarModel) newModel;
 72#if UNITY_EDITOR
 273            gameObject.name = $"Avatar Shape {model.name}";
 74#endif
 275            everythingIsLoaded = false;
 76
 277            bool avatarDone = false;
 278            bool avatarFailed = false;
 79
 280            yield return null; //NOTE(Brian): just in case we have a Object.Destroy waiting to be resolved.
 81
 82            // To deal with the cases in which the entity transform was configured before the AvatarShape
 183            if (!initializedPosition && entity.components.ContainsKey(DCL.Models.CLASS_ID_COMPONENT.TRANSFORM))
 84            {
 185                initializedPosition = true;
 86
 187                avatarMovementController.MoveTo(
 88                    entity.gameObject.transform.localPosition - Vector3.up * DCLCharacterController.i.characterControlle
 89                    entity.gameObject.transform.localRotation, true);
 90            }
 91
 192            avatarRenderer.ApplyModel(model, () => avatarDone = true, () => avatarFailed = true);
 93
 294            yield return new WaitUntil(() => avatarDone || avatarFailed);
 95
 096            onPointerDown.Initialize(
 97                new OnPointerDown.Model()
 98                {
 99                    type = OnPointerDown.NAME,
 100                    button = WebInterface.ACTION_BUTTON.POINTER.ToString(),
 101                    hoverText = "view profile"
 102                },
 103                entity
 104            );
 105
 0106            entity.OnTransformChange -= avatarMovementController.OnTransformChanged;
 0107            entity.OnTransformChange += avatarMovementController.OnTransformChanged;
 108
 0109            entity.OnTransformChange -= OnEntityTransformChanged;
 0110            entity.OnTransformChange += OnEntityTransformChanged;
 111
 0112            onPointerDown.OnPointerDownReport -= PlayerClicked;
 0113            onPointerDown.OnPointerDownReport += PlayerClicked;
 0114            onPointerDown.OnPointerEnterReport -= PlayerPointerEnter;
 0115            onPointerDown.OnPointerEnterReport += PlayerPointerEnter;
 0116            onPointerDown.OnPointerExitReport -= PlayerPointerExit;
 0117            onPointerDown.OnPointerExitReport += PlayerPointerExit;
 118
 119
 0120            UpdatePlayerStatus(model);
 121
 0122            avatarCollider.gameObject.SetActive(true);
 123
 0124            everythingIsLoaded = true;
 0125            OnAvatarShapeUpdated?.Invoke(entity, this);
 126
 0127            EnablePassport();
 128
 0129            KernelConfig.i.EnsureConfigInitialized()
 130                        .Then(config =>
 131                        {
 0132                            if (config.features.enableAvatarLODs)
 0133                                avatarRenderer.InitializeImpostor();
 0134                        });
 0135        }
 0136        private void PlayerPointerExit() { playerName?.SetForceShow(false); }
 0137        private void PlayerPointerEnter() { playerName?.SetForceShow(true); }
 138
 139        private void UpdatePlayerStatus(AvatarModel model)
 140        {
 141            // Remove the player status if the userId changes
 0142            if (player != null && (player.id != model.id || player.name != model.name))
 0143                otherPlayers.Remove(player.id);
 144
 0145            if (string.IsNullOrEmpty(model?.id))
 0146                return;
 147
 0148            bool isNew = false;
 0149            if (player == null)
 150            {
 0151                player = new Player();
 0152                isNew = true;
 153            }
 154
 0155            player.id = model.id;
 0156            player.name = model.name;
 0157            player.isTalking = model.talking;
 0158            player.worldPosition = entity.gameObject.transform.position;
 0159            player.renderer = avatarRenderer;
 0160            player.onPointerDownCollider = onPointerDown;
 161
 0162            if (isNew)
 163            {
 0164                player.playerName = playerName;
 0165                player.playerName.SetName(player.name);
 0166                player.playerName.Show();
 0167                otherPlayers.Add(player.id, player);
 168            }
 0169            player.playerName.SetIsTalking(model.talking);
 0170            player.playerName.SetYOffset(Mathf.Max(MINIMUM_PLAYERNAME_HEIGHT, avatarRenderer.maxY));
 0171        }
 172
 173        private void Update()
 174        {
 1175            if (player != null)
 176            {
 0177                player.worldPosition = entity.gameObject.transform.position;
 0178                player.forwardDirection = entity.gameObject.transform.forward;
 179            }
 1180        }
 181
 182        public void DisablePassport()
 183        {
 2184            if (onPointerDown.collider == null)
 0185                return;
 186
 2187            onPointerDown.SetColliderEnabled(false);
 2188        }
 189
 190        public void EnablePassport()
 191        {
 0192            if (onPointerDown.collider == null)
 0193                return;
 194
 0195            onPointerDown.SetColliderEnabled(true);
 0196        }
 197
 198        private void OnEntityTransformChanged(object newModel)
 199        {
 0200            DCLTransform.Model newTransformModel = (DCLTransform.Model)newModel;
 0201            lastAvatarPosition = newTransformModel.position;
 0202        }
 203
 204        public override void OnPoolGet()
 205        {
 2206            base.OnPoolGet();
 207
 2208            everythingIsLoaded = false;
 2209            initializedPosition = false;
 2210            oldModel = new AvatarModel();
 2211            model = new AvatarModel();
 2212            lastAvatarPosition = null;
 2213            player = null;
 2214        }
 215
 0216        void OnImpostorAlphaValueUpdate(float newAlphaValue) { avatarMovementController.movementLerpWait = newAlphaValue
 217
 218        public override void Cleanup()
 219        {
 675220            base.Cleanup();
 221
 675222            playerName?.Hide(true);
 675223            if (player != null)
 224            {
 0225                otherPlayers.Remove(player.id);
 0226                player = null;
 227            }
 228
 675229            avatarRenderer.CleanupAvatar();
 230
 675231            if (poolableObject != null)
 232            {
 6233                poolableObject.OnRelease -= Cleanup;
 234            }
 235
 675236            onPointerDown.OnPointerDownReport -= PlayerClicked;
 675237            onPointerDown.OnPointerEnterReport -= PlayerPointerEnter;
 675238            onPointerDown.OnPointerExitReport -= PlayerPointerExit;
 239
 675240            if (entity != null)
 241            {
 2242                entity.OnTransformChange = null;
 2243                entity = null;
 244            }
 675245        }
 246
 0247        public override int GetClassId() { return (int) CLASS_ID_COMPONENT.AVATAR_SHAPE; }
 248    }
 249}