| | 1 | | using DCL; |
| | 2 | | using DCL.Interface; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | public 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; |
| 370 | 12 | | public float cameraDistanceToDeactivate = 1.0f; |
| | 13 | |
|
| 578 | 14 | | private UserProfile userProfile => UserProfile.GetOwnUserProfile(); |
| 0 | 15 | | 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 | | { |
| 123 | 24 | | 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 |
| 123 | 27 | | avatarRenderer.OnSuccessEvent -= OnAvatarRendererReady; |
| 123 | 28 | | avatarRenderer.OnFailEvent -= OnAvatarRendererFail; |
| 123 | 29 | | avatarRenderer.OnSuccessEvent += OnAvatarRendererReady; |
| 123 | 30 | | avatarRenderer.OnFailEvent += OnAvatarRendererFail; |
| | 31 | |
|
| 123 | 32 | | if ( UserProfileController.i != null ) |
| | 33 | | { |
| 123 | 34 | | UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail; |
| 123 | 35 | | UserProfileController.i.OnBaseWereablesFail += OnBaseWereablesFail; |
| | 36 | | } |
| | 37 | |
|
| 123 | 38 | | CommonScriptableObjects.rendererState.AddLock(this); |
| | 39 | |
|
| 123 | 40 | | mainCamera = Camera.main; |
| 123 | 41 | | } |
| | 42 | |
|
| | 43 | | private void OnAvatarRendererReady() |
| | 44 | | { |
| 0 | 45 | | enableCameraCheck = true; |
| 0 | 46 | | avatarCollider.gameObject.SetActive(true); |
| 0 | 47 | | CommonScriptableObjects.rendererState.RemoveLock(this); |
| 0 | 48 | | avatarRenderer.OnSuccessEvent -= OnAvatarRendererReady; |
| 0 | 49 | | avatarRenderer.OnFailEvent -= OnAvatarRendererFail; |
| 0 | 50 | | DataStore.i.isPlayerRendererLoaded.Set(true); |
| | 51 | |
|
| 0 | 52 | | if (avatarWereablesErrors || baseWereablesErrors) |
| 0 | 53 | | ShowWearablesWarning(); |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | private void OnAvatarRendererFail(bool isFatalError) |
| | 57 | | { |
| 6 | 58 | | avatarWereablesErrors = true; |
| | 59 | |
|
| 6 | 60 | | if (isFatalError) |
| 6 | 61 | | WebInterface.ReportAvatarFatalError(); |
| | 62 | | else |
| 0 | 63 | | OnAvatarRendererReady(); |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | private void OnBaseWereablesFail() |
| | 67 | | { |
| 0 | 68 | | UserProfileController.i.OnBaseWereablesFail -= OnBaseWereablesFail; |
| 0 | 69 | | baseWereablesErrors = true; |
| | 70 | |
|
| 0 | 71 | | if (enableCameraCheck && !avatarWereablesErrors) |
| 0 | 72 | | ShowWearablesWarning(); |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | private void ShowWearablesWarning() |
| | 76 | | { |
| 0 | 77 | | 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 | | }); |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | private void Update() |
| | 87 | | { |
| 21212 | 88 | | if (!enableCameraCheck || repositioningWorld) |
| 21212 | 89 | | return; |
| | 90 | |
|
| 0 | 91 | | if (mainCamera == null) |
| | 92 | | { |
| 0 | 93 | | mainCamera = Camera.main; |
| | 94 | |
|
| 0 | 95 | | if (mainCamera == null) |
| 0 | 96 | | return; |
| | 97 | | } |
| | 98 | |
|
| 0 | 99 | | bool shouldBeVisible = Vector3.Distance(mainCamera.transform.position, transform.position) > cameraDistanceToDea |
| 0 | 100 | | avatarVisibility.SetVisibility("PLAYER_AVATAR_CONTROLLER", shouldBeVisible); |
| 0 | 101 | | } |
| | 102 | |
|
| 114 | 103 | | public void SetAvatarVisibility(bool isVisible) { avatarRenderer.SetGOVisibility(isVisible); } |
| | 104 | |
|
| | 105 | | private void OnEnable() |
| | 106 | | { |
| 145 | 107 | | userProfile.OnUpdate += OnUserProfileOnUpdate; |
| 145 | 108 | | userProfile.OnAvatarExpressionSet += OnAvatarExpression; |
| 145 | 109 | | } |
| | 110 | |
|
| 4 | 111 | | private void OnAvatarExpression(string id, long timestamp) { avatarRenderer.SetExpression(id, timestamp); } |
| | 112 | |
|
| 44 | 113 | | private void OnUserProfileOnUpdate(UserProfile profile) { avatarRenderer.ApplyModel(profile.avatar, null, null); } |
| | 114 | |
|
| | 115 | | private void OnDisable() |
| | 116 | | { |
| 144 | 117 | | userProfile.OnUpdate -= OnUserProfileOnUpdate; |
| 144 | 118 | | userProfile.OnAvatarExpressionSet -= OnAvatarExpression; |
| 144 | 119 | | } |
| | 120 | | } |