< Summary

Class:PlayerAvatarController
Assembly:PlayerAvatarController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/PlayerAvatarController/PlayerAvatarController.cs
Covered lines:30
Uncovered lines:32
Coverable lines:62
Total lines:150
Line coverage:48.3% (30 of 62)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PlayerAvatarController()0%110100%
Start()0%2.012085.71%
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 System;
 2using DCL;
 3using DCL.Interface;
 4using DCL.FatalErrorReporter;
 5using DCL.NotificationModel;
 6using UnityEngine;
 7using Type = DCL.NotificationModel.Type;
 8
 9public class PlayerAvatarController : MonoBehaviour
 10{
 11    private const string LOADING_WEARABLES_ERROR_MESSAGE = "There was a problem loading your wearables";
 12
 13    public AvatarRenderer avatarRenderer;
 14    public Collider avatarCollider;
 15    public AvatarVisibility avatarVisibility;
 61216    public float cameraDistanceToDeactivate = 1.0f;
 17
 234418    private UserProfile userProfile => UserProfile.GetOwnUserProfile();
 019    private bool repositioningWorld => DCLCharacterController.i.characterPosition.RepositionedWorldLastFrame();
 20
 21    private bool enableCameraCheck = false;
 22    private Camera mainCamera;
 23    private bool avatarWereablesErrors = false;
 24    private bool baseWereablesErrors = false;
 25    private PlayerAvatarAnalytics playerAvatarAnalytics;
 26    private IFatalErrorReporter fatalErrorReporter;
 27
 28    private void Start()
 29    {
 58530        DataStore.i.common.isPlayerRendererLoaded.Set(false);
 58531        playerAvatarAnalytics = new PlayerAvatarAnalytics(Analytics.i, CommonScriptableObjects.playerCoords);
 32
 33        //NOTE(Brian): We must wait for loading to finish before deactivating the renderer, or the GLTF Loader won't fin
 58534        avatarRenderer.OnSuccessEvent -= OnAvatarRendererReady;
 58535        avatarRenderer.OnFailEvent -= OnAvatarRendererFail;
 58536        avatarRenderer.OnSuccessEvent += OnAvatarRendererReady;
 58537        avatarRenderer.OnFailEvent += OnAvatarRendererFail;
 38
 58539        if ( UserProfileController.i != null )
 40        {
 041            UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail;
 042            UserProfileController.i.OnBaseWereablesFail += OnBaseWereablesFail;
 43        }
 44
 58545        DataStore.i.player.playerCollider.Set(avatarCollider);
 58546        CommonScriptableObjects.rendererState.AddLock(this);
 47
 48#if UNITY_WEBGL
 58549        fatalErrorReporter = new WebFatalErrorReporter();
 50#else
 51        fatalErrorReporter = new DefaultFatalErrorReporter(DataStore.i);
 52#endif
 53
 58554        mainCamera = Camera.main;
 58555    }
 56
 57    private void OnAvatarRendererReady()
 58    {
 059        enableCameraCheck = true;
 060        avatarCollider.gameObject.SetActive(true);
 061        CommonScriptableObjects.rendererState.RemoveLock(this);
 062        avatarRenderer.OnSuccessEvent -= OnAvatarRendererReady;
 063        avatarRenderer.OnFailEvent -= OnAvatarRendererFail;
 064        DataStore.i.common.isPlayerRendererLoaded.Set(true);
 65
 066        IAvatarAnchorPoints anchorPoints = new AvatarAnchorPoints();
 067        anchorPoints.Prepare(avatarRenderer.transform, avatarRenderer.GetBones(), avatarRenderer.maxY);
 68
 069        var player = new Player()
 70        {
 71            id = userProfile.userId,
 72            name = userProfile.name,
 73            renderer = avatarRenderer,
 74            anchorPoints = anchorPoints
 75        };
 076        DataStore.i.player.ownPlayer.Set(player);
 77
 078        if (avatarWereablesErrors || baseWereablesErrors)
 079            ShowWearablesWarning();
 080    }
 81
 82    private void OnAvatarRendererFail(Exception exception)
 83    {
 2084        avatarWereablesErrors = true;
 85
 2086        if (exception is AvatarLoadFatalException)
 2087            fatalErrorReporter.Report(exception);
 88        else
 089            OnAvatarRendererReady();
 090    }
 91
 92    private void OnBaseWereablesFail()
 93    {
 094        UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail;
 095        baseWereablesErrors = true;
 96
 097        if (enableCameraCheck && !avatarWereablesErrors)
 098            ShowWearablesWarning();
 099    }
 100
 101    private void ShowWearablesWarning()
 102    {
 0103        NotificationsController.i.ShowNotification(new Model
 104        {
 105            message = LOADING_WEARABLES_ERROR_MESSAGE,
 106            type = Type.GENERIC,
 107            timer = 10f,
 108            destroyOnFinish = true
 109        });
 0110    }
 111
 112    private void Update()
 113    {
 9002114        if (!enableCameraCheck || repositioningWorld)
 9002115            return;
 116
 0117        if (mainCamera == null)
 118        {
 0119            mainCamera = Camera.main;
 120
 0121            if (mainCamera == null)
 0122                return;
 123        }
 124
 0125        bool shouldBeVisible = Vector3.Distance(mainCamera.transform.position, transform.position) > cameraDistanceToDea
 0126        avatarVisibility.SetVisibility("PLAYER_AVATAR_CONTROLLER", shouldBeVisible);
 0127    }
 128
 86129    public void SetAvatarVisibility(bool isVisible) { avatarRenderer.SetGOVisibility(isVisible); }
 130
 131    private void OnEnable()
 132    {
 586133        userProfile.OnUpdate += OnUserProfileOnUpdate;
 586134        userProfile.OnAvatarExpressionSet += OnAvatarExpression;
 586135    }
 136
 137    private void OnAvatarExpression(string id, long timestamp)
 138    {
 2139        avatarRenderer.SetExpression(id, timestamp);
 2140        playerAvatarAnalytics.ReportExpression(id);
 2141    }
 142
 42143    private void OnUserProfileOnUpdate(UserProfile profile) { avatarRenderer.ApplyModel(profile.avatar, null, null); }
 144
 145    private void OnDisable()
 146    {
 586147        userProfile.OnUpdate -= OnUserProfileOnUpdate;
 586148        userProfile.OnAvatarExpressionSet -= OnAvatarExpression;
 586149    }
 150}