< 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:56
Coverable lines:93
Total lines:214
Line coverage:39.7% (37 of 93)
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;
 34
 35        private void Awake()
 36        {
 137            model = new AvatarModel();
 138            currentPlayerInfoCardId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID);
 139            avatarRenderer.OnImpostorAlphaValueUpdate += OnImpostorAlphaValueUpdate;
 140        }
 41
 42        private void PlayerClicked()
 43        {
 044            if (model == null)
 045                return;
 046            currentPlayerInfoCardId.Set(((AvatarModel) model).id);
 047        }
 48
 49        public void OnDestroy()
 50        {
 151            Cleanup();
 52
 153            if (poolableObject != null && poolableObject.isInsidePool)
 154                poolableObject.RemoveFromPool();
 55
 156            avatarRenderer.OnImpostorAlphaValueUpdate -= OnImpostorAlphaValueUpdate;
 157        }
 58
 59        public override IEnumerator ApplyChanges(BaseModel newModel)
 60        {
 161            DisablePassport();
 62
 163            var model = (AvatarModel) newModel;
 64
 165            everythingIsLoaded = false;
 66
 167            bool avatarDone = false;
 168            bool avatarFailed = false;
 69
 170            yield return null; //NOTE(Brian): just in case we have a Object.Destroy waiting to be resolved.
 71
 072            avatarRenderer.ApplyModel(model, () => avatarDone = true, () => avatarFailed = true);
 73
 074            yield return new WaitUntil(() => avatarDone || avatarFailed);
 75
 076            onPointerDown.Initialize(
 77                new OnPointerDown.Model()
 78                {
 79                    type = OnPointerDown.NAME,
 80                    button = WebInterface.ACTION_BUTTON.POINTER.ToString(),
 81                    hoverText = "view profile"
 82                },
 83                entity
 84            );
 85
 086            entity.OnTransformChange -= avatarMovementController.OnTransformChanged;
 087            entity.OnTransformChange += avatarMovementController.OnTransformChanged;
 88
 089            entity.OnTransformChange -= OnEntityTransformChanged;
 090            entity.OnTransformChange += OnEntityTransformChanged;
 91
 092            onPointerDown.OnPointerDownReport -= PlayerClicked;
 093            onPointerDown.OnPointerDownReport += PlayerClicked;
 94
 95            // To deal with the cases in which the entity transform was configured before the AvatarShape
 096            if (!initializedPosition && entity.components.ContainsKey(DCL.Models.CLASS_ID_COMPONENT.TRANSFORM))
 97            {
 098                initializedPosition = true;
 99
 0100                avatarMovementController.MoveTo(
 101                    entity.gameObject.transform.localPosition - Vector3.up * DCLCharacterController.i.characterControlle
 102                    entity.gameObject.transform.localRotation, true);
 103            }
 104
 0105            UpdatePlayerStatus(model);
 106
 0107            avatarCollider.gameObject.SetActive(true);
 108
 0109            everythingIsLoaded = true;
 0110            OnAvatarShapeUpdated?.Invoke(entity, this);
 111
 0112            EnablePassport();
 113
 0114            avatarRenderer.InitializeImpostor();
 0115        }
 116
 117        private void UpdatePlayerStatus(AvatarModel model)
 118        {
 119            // Remove the player status if the userId changes
 0120            if (player != null && (player.id != model.id || player.name != model.name))
 0121                otherPlayers.Remove(player.id);
 122
 0123            if (string.IsNullOrEmpty(model?.id))
 0124                return;
 125
 0126            bool isNew = false;
 0127            if (player == null)
 128            {
 0129                player = new Player();
 0130                isNew = true;
 131            }
 0132            player.id = model.id;
 0133            player.name = model.name;
 0134            player.isTalking = model.talking;
 0135            player.worldPosition = entity.gameObject.transform.position;
 0136            player.renderer = avatarRenderer;
 0137            if (isNew)
 0138                otherPlayers.Add(player.id, player);
 0139        }
 140
 141        private void Update()
 142        {
 0143            if (player != null)
 144            {
 0145                player.worldPosition = entity.gameObject.transform.position;
 0146                player.forwardDirection = entity.gameObject.transform.forward;
 147            }
 0148        }
 149
 150        public void DisablePassport()
 151        {
 1152            if (onPointerDown.collider == null)
 0153                return;
 154
 1155            onPointerDown.collider.enabled = false;
 1156        }
 157
 158        public void EnablePassport()
 159        {
 0160            if (onPointerDown.collider == null)
 0161                return;
 162
 0163            onPointerDown.collider.enabled = true;
 0164        }
 165
 166        private void OnEntityTransformChanged(object newModel)
 167        {
 0168            DCLTransform.Model newTransformModel = (DCLTransform.Model)newModel;
 0169            lastAvatarPosition = newTransformModel.position;
 0170        }
 171
 172        public override void OnPoolGet()
 173        {
 1174            base.OnPoolGet();
 175
 1176            everythingIsLoaded = false;
 1177            initializedPosition = false;
 1178            oldModel = new AvatarModel();
 1179            model = new AvatarModel();
 1180            lastAvatarPosition = null;
 1181            player = null;
 1182        }
 183
 0184        void OnImpostorAlphaValueUpdate(float newAlphaValue) { avatarMovementController.movementLerpWait = newAlphaValue
 185
 186        public override void Cleanup()
 187        {
 3188            base.Cleanup();
 189
 3190            if (player != null)
 191            {
 0192                otherPlayers.Remove(player.id);
 0193                player = null;
 194            }
 195
 3196            avatarRenderer.CleanupAvatar();
 197
 3198            if (poolableObject != null)
 199            {
 3200                poolableObject.OnRelease -= Cleanup;
 201            }
 202
 3203            onPointerDown.OnPointerDownReport -= PlayerClicked;
 204
 3205            if (entity != null)
 206            {
 1207                entity.OnTransformChange = null;
 1208                entity = null;
 209            }
 3210        }
 211
 0212        public override int GetClassId() { return (int) CLASS_ID_COMPONENT.AVATAR_SHAPE; }
 213    }
 214}