< Summary

Class:DCL.AvatarShape
Assembly:AvatarShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarShape.cs
Covered lines:35
Uncovered lines:55
Coverable lines:90
Total lines:210
Line coverage:38.8% (35 of 90)
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%
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
 65125        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        }
 40
 41        private void PlayerClicked()
 42        {
 043            if (model == null)
 044                return;
 045            currentPlayerInfoCardId.Set(((AvatarModel) model).id);
 046        }
 47
 48        public void OnDestroy()
 49        {
 150            Cleanup();
 51
 152            if (poolableObject != null && poolableObject.isInsidePool)
 153                poolableObject.RemoveFromPool();
 154        }
 55
 56        public override IEnumerator ApplyChanges(BaseModel newModel)
 57        {
 158            DisablePassport();
 59
 160            var model = (AvatarModel) newModel;
 61
 162            everythingIsLoaded = false;
 63
 164            bool avatarDone = false;
 165            bool avatarFailed = false;
 66
 167            yield return null; //NOTE(Brian): just in case we have a Object.Destroy waiting to be resolved.
 68
 069            avatarRenderer.ApplyModel(model, () => avatarDone = true, () => avatarFailed = true);
 70
 071            yield return new WaitUntil(() => avatarDone || avatarFailed);
 72
 073            onPointerDown.Initialize(
 74                new OnPointerDown.Model()
 75                {
 76                    type = OnPointerDown.NAME,
 77                    button = WebInterface.ACTION_BUTTON.POINTER.ToString(),
 78                    hoverText = "view profile"
 79                },
 80                entity
 81            );
 82
 083            entity.OnTransformChange -= avatarMovementController.OnTransformChanged;
 084            entity.OnTransformChange += avatarMovementController.OnTransformChanged;
 85
 086            entity.OnTransformChange -= OnEntityTransformChanged;
 087            entity.OnTransformChange += OnEntityTransformChanged;
 88
 089            onPointerDown.OnPointerDownReport -= PlayerClicked;
 090            onPointerDown.OnPointerDownReport += PlayerClicked;
 91
 92            // To deal with the cases in which the entity transform was configured before the AvatarShape
 093            if (!initializedPosition && entity.components.ContainsKey(DCL.Models.CLASS_ID_COMPONENT.TRANSFORM))
 94            {
 095                initializedPosition = true;
 96
 097                avatarMovementController.MoveTo(
 98                    entity.gameObject.transform.localPosition - Vector3.up * DCLCharacterController.i.characterControlle
 99                    entity.gameObject.transform.localRotation, true);
 100            }
 101
 0102            UpdatePlayerStatus(model);
 103
 0104            avatarCollider.gameObject.SetActive(true);
 105
 0106            everythingIsLoaded = true;
 0107            OnAvatarShapeUpdated?.Invoke(entity, this);
 108
 0109            EnablePassport();
 110
 0111            avatarRenderer.InitializeImpostor();
 0112        }
 113
 114        private void UpdatePlayerStatus(AvatarModel model)
 115        {
 116            // Remove the player status if the userId changes
 0117            if (player != null && (player.id != model.id || player.name != model.name))
 0118                otherPlayers.Remove(player.id);
 119
 0120            if (string.IsNullOrEmpty(model?.id))
 0121                return;
 122
 0123            bool isNew = false;
 0124            if (player == null)
 125            {
 0126                player = new Player();
 0127                isNew = true;
 128            }
 0129            player.id = model.id;
 0130            player.name = model.name;
 0131            player.isTalking = model.talking;
 0132            player.worldPosition = entity.gameObject.transform.position;
 0133            player.renderer = avatarRenderer;
 0134            if (isNew)
 0135                otherPlayers.Add(player.id, player);
 0136        }
 137
 138        private void Update()
 139        {
 0140            if (player != null)
 141            {
 0142                player.worldPosition = entity.gameObject.transform.position;
 0143                player.forwardDirection = entity.gameObject.transform.forward;
 144            }
 0145        }
 146
 147        public void DisablePassport()
 148        {
 1149            if (onPointerDown.collider == null)
 0150                return;
 151
 1152            onPointerDown.collider.enabled = false;
 1153        }
 154
 155        public void EnablePassport()
 156        {
 0157            if (onPointerDown.collider == null)
 0158                return;
 159
 0160            onPointerDown.collider.enabled = true;
 0161        }
 162
 163        private void OnEntityTransformChanged(object newModel)
 164        {
 0165            DCLTransform.Model newTransformModel = (DCLTransform.Model)newModel;
 0166            lastAvatarPosition = newTransformModel.position;
 0167        }
 168
 169        public override void OnPoolGet()
 170        {
 1171            base.OnPoolGet();
 172
 1173            everythingIsLoaded = false;
 1174            initializedPosition = false;
 1175            oldModel = new AvatarModel();
 1176            model = new AvatarModel();
 1177            lastAvatarPosition = null;
 1178            player = null;
 1179        }
 180
 181        public override void Cleanup()
 182        {
 3183            base.Cleanup();
 184
 185
 3186            if (player != null)
 187            {
 0188                otherPlayers.Remove(player.id);
 0189                player = null;
 190            }
 191
 3192            avatarRenderer.CleanupAvatar();
 193
 3194            if (poolableObject != null)
 195            {
 3196                poolableObject.OnRelease -= Cleanup;
 197            }
 198
 3199            onPointerDown.OnPointerDownReport -= PlayerClicked;
 200
 3201            if (entity != null)
 202            {
 1203                entity.OnTransformChange = null;
 1204                entity = null;
 205            }
 3206        }
 207
 0208        public override int GetClassId() { return (int) CLASS_ID_COMPONENT.AVATAR_SHAPE; }
 209    }
 210}