< Summary

Class:PlayerAvatarController
Assembly:PlayerAvatarController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/PlayerAvatarController/PlayerAvatarController.cs
Covered lines:27
Uncovered lines:26
Coverable lines:53
Total lines:120
Line coverage:50.9% (27 of 53)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PlayerAvatarController()0%110100%
Start()0%220100%
OnAvatarRendererReady()0%12300%
OnAvatarRendererFail(...)0%2.262060%
OnBaseWereablesFail()0%12300%
ShowWearablesWarning()0%2100%
Update()0%16.765022.22%
SetAvatarVisibility(...)0%110100%
OnEnable()0%110100%
OnAvatarExpression(...)0%110100%
OnUserProfileOnUpdate(...)0%110100%
OnDisable()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/PlayerAvatarController/PlayerAvatarController.cs

#LineLine coverage
 1using DCL;
 2using DCL.Interface;
 3using UnityEngine;
 4
 5public class PlayerAvatarController : MonoBehaviour
 6{
 7    private const string LOADING_WEARABLES_ERROR_MESSAGE = "There was a problem loading your wearables";
 8
 9    public AvatarRenderer avatarRenderer;
 10    public Collider avatarCollider;
 11    public AvatarVisibility avatarVisibility;
 37012    public float cameraDistanceToDeactivate = 1.0f;
 13
 57814    private UserProfile userProfile => UserProfile.GetOwnUserProfile();
 015    private bool repositioningWorld => DCLCharacterController.i.characterPosition.RepositionedWorldLastFrame();
 16
 17    private bool enableCameraCheck = false;
 18    private Camera mainCamera;
 19    private bool avatarWereablesErrors = false;
 20    private bool baseWereablesErrors = false;
 21
 22    private void Start()
 23    {
 12324        DataStore.i.isPlayerRendererLoaded.Set(false);
 25
 26        //NOTE(Brian): We must wait for loading to finish before deactivating the renderer, or the GLTF Loader won't fin
 12327        avatarRenderer.OnSuccessEvent -= OnAvatarRendererReady;
 12328        avatarRenderer.OnFailEvent -= OnAvatarRendererFail;
 12329        avatarRenderer.OnSuccessEvent += OnAvatarRendererReady;
 12330        avatarRenderer.OnFailEvent += OnAvatarRendererFail;
 31
 12332        if ( UserProfileController.i != null )
 33        {
 12334            UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail;
 12335            UserProfileController.i.OnBaseWereablesFail += OnBaseWereablesFail;
 36        }
 37
 12338        CommonScriptableObjects.rendererState.AddLock(this);
 39
 12340        mainCamera = Camera.main;
 12341    }
 42
 43    private void OnAvatarRendererReady()
 44    {
 045        enableCameraCheck = true;
 046        avatarCollider.gameObject.SetActive(true);
 047        CommonScriptableObjects.rendererState.RemoveLock(this);
 048        avatarRenderer.OnSuccessEvent -= OnAvatarRendererReady;
 049        avatarRenderer.OnFailEvent -= OnAvatarRendererFail;
 050        DataStore.i.isPlayerRendererLoaded.Set(true);
 51
 052        if (avatarWereablesErrors || baseWereablesErrors)
 053            ShowWearablesWarning();
 054    }
 55
 56    private void OnAvatarRendererFail(bool isFatalError)
 57    {
 658        avatarWereablesErrors = true;
 59
 660        if (isFatalError)
 661            WebInterface.ReportAvatarFatalError();
 62        else
 063            OnAvatarRendererReady();
 064    }
 65
 66    private void OnBaseWereablesFail()
 67    {
 068        UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail;
 069        baseWereablesErrors = true;
 70
 071        if (enableCameraCheck && !avatarWereablesErrors)
 072            ShowWearablesWarning();
 073    }
 74
 75    private void ShowWearablesWarning()
 76    {
 077        NotificationsController.i.ShowNotification(new DCL.NotificationModel.Model
 78        {
 79            message = LOADING_WEARABLES_ERROR_MESSAGE,
 80            type = DCL.NotificationModel.Type.GENERIC,
 81            timer = 10f,
 82            destroyOnFinish = true
 83        });
 084    }
 85
 86    private void Update()
 87    {
 2121288        if (!enableCameraCheck || repositioningWorld)
 2121289            return;
 90
 091        if (mainCamera == null)
 92        {
 093            mainCamera = Camera.main;
 94
 095            if (mainCamera == null)
 096                return;
 97        }
 98
 099        bool shouldBeVisible = Vector3.Distance(mainCamera.transform.position, transform.position) > cameraDistanceToDea
 0100        avatarVisibility.SetVisibility("PLAYER_AVATAR_CONTROLLER", shouldBeVisible);
 0101    }
 102
 114103    public void SetAvatarVisibility(bool isVisible) { avatarRenderer.SetGOVisibility(isVisible); }
 104
 105    private void OnEnable()
 106    {
 145107        userProfile.OnUpdate += OnUserProfileOnUpdate;
 145108        userProfile.OnAvatarExpressionSet += OnAvatarExpression;
 145109    }
 110
 4111    private void OnAvatarExpression(string id, long timestamp) { avatarRenderer.SetExpression(id, timestamp); }
 112
 44113    private void OnUserProfileOnUpdate(UserProfile profile) { avatarRenderer.ApplyModel(profile.avatar, null, null); }
 114
 115    private void OnDisable()
 116    {
 144117        userProfile.OnUpdate -= OnUserProfileOnUpdate;
 144118        userProfile.OnAvatarExpressionSet -= OnAvatarExpression;
 144119    }
 120}