| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Interface; |
| | 4 | | using DCL.FatalErrorReporter; |
| | 5 | | using DCL.NotificationModel; |
| | 6 | | using UnityEngine; |
| | 7 | | using Type = DCL.NotificationModel.Type; |
| | 8 | |
|
| | 9 | | public 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; |
| 613 | 16 | | public float cameraDistanceToDeactivate = 1.0f; |
| | 17 | |
|
| 2344 | 18 | | private UserProfile userProfile => UserProfile.GetOwnUserProfile(); |
| 0 | 19 | | 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 | | { |
| 585 | 30 | | DataStore.i.common.isPlayerRendererLoaded.Set(false); |
| 585 | 31 | | 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 |
| 585 | 34 | | avatarRenderer.OnSuccessEvent -= OnAvatarRendererReady; |
| 585 | 35 | | avatarRenderer.OnFailEvent -= OnAvatarRendererFail; |
| 585 | 36 | | avatarRenderer.OnSuccessEvent += OnAvatarRendererReady; |
| 585 | 37 | | avatarRenderer.OnFailEvent += OnAvatarRendererFail; |
| | 38 | |
|
| 585 | 39 | | if ( UserProfileController.i != null ) |
| | 40 | | { |
| 0 | 41 | | UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail; |
| 0 | 42 | | UserProfileController.i.OnBaseWereablesFail += OnBaseWereablesFail; |
| | 43 | | } |
| | 44 | |
|
| 585 | 45 | | CommonScriptableObjects.rendererState.AddLock(this); |
| | 46 | |
|
| | 47 | | #if UNITY_WEBGL |
| | 48 | | fatalErrorReporter = new WebFatalErrorReporter(); |
| | 49 | | #else |
| 585 | 50 | | fatalErrorReporter = new DefaultFatalErrorReporter(DataStore.i); |
| | 51 | | #endif |
| | 52 | |
|
| 585 | 53 | | mainCamera = Camera.main; |
| 585 | 54 | | } |
| | 55 | |
|
| | 56 | | private void OnAvatarRendererReady() |
| | 57 | | { |
| 0 | 58 | | enableCameraCheck = true; |
| 0 | 59 | | avatarCollider.gameObject.SetActive(true); |
| 0 | 60 | | CommonScriptableObjects.rendererState.RemoveLock(this); |
| 0 | 61 | | avatarRenderer.OnSuccessEvent -= OnAvatarRendererReady; |
| 0 | 62 | | avatarRenderer.OnFailEvent -= OnAvatarRendererFail; |
| 0 | 63 | | DataStore.i.common.isPlayerRendererLoaded.Set(true); |
| | 64 | |
|
| 0 | 65 | | IAvatarAnchorPoints anchorPoints = new AvatarAnchorPoints(); |
| 0 | 66 | | anchorPoints.Prepare(avatarRenderer.transform, avatarRenderer.GetBones(), avatarRenderer.maxY); |
| | 67 | |
|
| 0 | 68 | | var player = new Player() |
| | 69 | | { |
| | 70 | | id = userProfile.userId, |
| | 71 | | name = userProfile.name, |
| | 72 | | renderer = avatarRenderer, |
| | 73 | | anchorPoints = anchorPoints |
| | 74 | | }; |
| 0 | 75 | | DataStore.i.player.ownPlayer.Set(player); |
| | 76 | |
|
| 0 | 77 | | if (avatarWereablesErrors || baseWereablesErrors) |
| 0 | 78 | | ShowWearablesWarning(); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | private void OnAvatarRendererFail(Exception exception) |
| | 82 | | { |
| 20 | 83 | | avatarWereablesErrors = true; |
| | 84 | |
|
| 20 | 85 | | if (exception is AvatarLoadFatalException) |
| 20 | 86 | | fatalErrorReporter.Report(exception); |
| | 87 | | else |
| 0 | 88 | | OnAvatarRendererReady(); |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | private void OnBaseWereablesFail() |
| | 92 | | { |
| 0 | 93 | | UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail; |
| 0 | 94 | | baseWereablesErrors = true; |
| | 95 | |
|
| 0 | 96 | | if (enableCameraCheck && !avatarWereablesErrors) |
| 0 | 97 | | ShowWearablesWarning(); |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | private void ShowWearablesWarning() |
| | 101 | | { |
| 0 | 102 | | NotificationsController.i.ShowNotification(new Model |
| | 103 | | { |
| | 104 | | message = LOADING_WEARABLES_ERROR_MESSAGE, |
| | 105 | | type = Type.GENERIC, |
| | 106 | | timer = 10f, |
| | 107 | | destroyOnFinish = true |
| | 108 | | }); |
| 0 | 109 | | } |
| | 110 | |
|
| | 111 | | private void Update() |
| | 112 | | { |
| 8674 | 113 | | if (!enableCameraCheck || repositioningWorld) |
| 8674 | 114 | | return; |
| | 115 | |
|
| 0 | 116 | | if (mainCamera == null) |
| | 117 | | { |
| 0 | 118 | | mainCamera = Camera.main; |
| | 119 | |
|
| 0 | 120 | | if (mainCamera == null) |
| 0 | 121 | | return; |
| | 122 | | } |
| | 123 | |
|
| 0 | 124 | | bool shouldBeVisible = Vector3.Distance(mainCamera.transform.position, transform.position) > cameraDistanceToDea |
| 0 | 125 | | avatarVisibility.SetVisibility("PLAYER_AVATAR_CONTROLLER", shouldBeVisible); |
| 0 | 126 | | } |
| | 127 | |
|
| 86 | 128 | | public void SetAvatarVisibility(bool isVisible) { avatarRenderer.SetGOVisibility(isVisible); } |
| | 129 | |
|
| | 130 | | private void OnEnable() |
| | 131 | | { |
| 586 | 132 | | userProfile.OnUpdate += OnUserProfileOnUpdate; |
| 586 | 133 | | userProfile.OnAvatarExpressionSet += OnAvatarExpression; |
| 586 | 134 | | } |
| | 135 | |
|
| | 136 | | private void OnAvatarExpression(string id, long timestamp) |
| | 137 | | { |
| 2 | 138 | | avatarRenderer.SetExpression(id, timestamp); |
| 2 | 139 | | playerAvatarAnalytics.ReportExpression(id); |
| 2 | 140 | | } |
| | 141 | |
|
| 42 | 142 | | private void OnUserProfileOnUpdate(UserProfile profile) { avatarRenderer.ApplyModel(profile.avatar, null, null); } |
| | 143 | |
|
| | 144 | | private void OnDisable() |
| | 145 | | { |
| 586 | 146 | | userProfile.OnUpdate -= OnUserProfileOnUpdate; |
| 586 | 147 | | userProfile.OnAvatarExpressionSet -= OnAvatarExpression; |
| 586 | 148 | | } |
| | 149 | | } |