| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using TMPro; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 6 | |
|
| | 7 | | [Serializable] |
| | 8 | | public class PlayerName : MonoBehaviour, IPlayerName |
| | 9 | | { |
| | 10 | | internal const int DEFAULT_CANVAS_SORTING_ORDER = 0; |
| | 11 | | internal const int FORCE_CANVAS_SORTING_ORDER = 40; |
| 1 | 12 | | internal static readonly int TALKING_ANIMATOR_BOOL = Animator.StringToHash("Talking"); |
| | 13 | | internal const float ALPHA_TRANSITION_STEP_PER_SECOND = 1f / 0.25f; |
| | 14 | | internal const float TARGET_ALPHA_SHOW = 1; |
| | 15 | | internal const float TARGET_ALPHA_HIDE = 0; |
| | 16 | | internal const int BACKGROUND_HEIGHT = 30; |
| | 17 | | internal const int BACKGROUND_EXTRA_WIDTH = 10; |
| | 18 | |
|
| | 19 | | [SerializeField] internal Canvas canvas; |
| | 20 | | [SerializeField] internal CanvasGroup canvasGroup; |
| | 21 | | [SerializeField] internal TextMeshProUGUI nameText; |
| | 22 | | [SerializeField] internal Image background; |
| | 23 | | [SerializeField] internal Transform pivot; |
| | 24 | | [SerializeField] internal Animator talkingAnimator; |
| | 25 | |
|
| 2109 | 26 | | internal BaseVariable<float> namesOpacity => DataStore.i.HUDs.avatarNamesOpacity; |
| 2109 | 27 | | internal BaseVariable<bool> namesVisible => DataStore.i.HUDs.avatarNamesVisible; |
| | 28 | |
|
| | 29 | | internal float alpha; |
| | 30 | | internal float targetAlpha; |
| | 31 | | internal bool forceShow = false; |
| | 32 | | internal Color backgroundOriginalColor; |
| | 33 | |
|
| | 34 | | private void Awake() |
| | 35 | | { |
| 703 | 36 | | backgroundOriginalColor = background.color; |
| 703 | 37 | | canvas.sortingOrder = DEFAULT_CANVAS_SORTING_ORDER; |
| 703 | 38 | | namesVisible.OnChange += OnNamesVisibleChanged; |
| 703 | 39 | | namesOpacity.OnChange += OnNamesOpacityChanged; |
| 703 | 40 | | OnNamesVisibleChanged(namesVisible.Get(), true); |
| 703 | 41 | | OnNamesOpacityChanged(namesOpacity.Get(), 1); |
| 703 | 42 | | Show(true); |
| 703 | 43 | | } |
| 1420 | 44 | | internal void OnNamesOpacityChanged(float current, float previous) { background.color = new Color(backgroundOriginal |
| | 45 | |
|
| 1410 | 46 | | internal void OnNamesVisibleChanged(bool current, bool previous) { canvas.enabled = current || forceShow; } |
| | 47 | |
|
| | 48 | | public void SetName(string name) |
| | 49 | | { |
| 4 | 50 | | name = ProfanityFilterSharedInstances.regexFilter.Filter(name); |
| 4 | 51 | | nameText.text = name; |
| 4 | 52 | | background.rectTransform.sizeDelta = new Vector2(nameText.GetPreferredValues().x + BACKGROUND_EXTRA_WIDTH, BACKG |
| 4 | 53 | | } |
| | 54 | |
|
| 0 | 55 | | private void Update() { Update(Time.deltaTime); } |
| | 56 | |
|
| | 57 | | internal void Update(float deltaTime) |
| | 58 | | { |
| 3 | 59 | | float finalTargetAlpha = forceShow ? TARGET_ALPHA_SHOW : targetAlpha; |
| | 60 | |
|
| 3 | 61 | | if (!Mathf.Approximately(alpha, finalTargetAlpha)) |
| 2 | 62 | | alpha = Mathf.MoveTowards(alpha, finalTargetAlpha, ALPHA_TRANSITION_STEP_PER_SECOND * deltaTime); |
| 1 | 63 | | else if (alpha == 0) |
| | 64 | | { |
| 1 | 65 | | UpdateVisuals(0); |
| | 66 | | // We are hidden and we dont have to scale, look at camera or anything, we can disable the gameObject |
| 1 | 67 | | gameObject.SetActive(false); |
| 1 | 68 | | return; |
| | 69 | | } |
| 2 | 70 | | Vector3 cameraPosition = CommonScriptableObjects.cameraPosition.Get(); |
| 2 | 71 | | Vector3 cameraRight = CommonScriptableObjects.cameraRight.Get(); |
| 2 | 72 | | Quaternion cameraRotation = DataStore.i.camera.rotation.Get(); |
| | 73 | |
|
| | 74 | | /* |
| | 75 | | * TODO: We could obtain distance to player from the AvatarLODController but that coupling it's overkill and ugl |
| | 76 | | * instead we should have a provider so all the subsystems can use it |
| | 77 | | */ |
| 2 | 78 | | float distanceToCamera = Vector3.Distance(cameraPosition, gameObject.transform.position); |
| 2 | 79 | | float resolvedAlpha = forceShow ? TARGET_ALPHA_SHOW : ResolveAlphaByDistance(alpha, distanceToCamera, forceShow) |
| 2 | 80 | | UpdateVisuals(resolvedAlpha); |
| 2 | 81 | | ScalePivotByDistance(distanceToCamera); |
| 2 | 82 | | LookAtCamera(cameraRight, cameraRotation.eulerAngles); |
| 2 | 83 | | pivot.transform.localPosition = Vector3.up * GetPivotYOffsetByDistance(distanceToCamera); |
| 2 | 84 | | } |
| | 85 | |
|
| | 86 | | internal void LookAtCamera(Vector3 cameraRight, Vector3 cameraEulerAngles) |
| | 87 | | { |
| 3 | 88 | | Transform cachedTransform = transform; |
| 3 | 89 | | cachedTransform.right = -cameraRight; // This will set the Y rotation |
| | 90 | |
|
| | 91 | | // Now we use the negated X axis rotation to make the rotation steady in horizont |
| 3 | 92 | | Vector3 finalEulerAngle = cachedTransform.localEulerAngles; |
| 3 | 93 | | finalEulerAngle.x = -cameraEulerAngles.x; |
| 3 | 94 | | cachedTransform.localEulerAngles = finalEulerAngle; |
| 3 | 95 | | } |
| | 96 | |
|
| | 97 | | public void Show(bool instant = false) |
| | 98 | | { |
| 703 | 99 | | targetAlpha = TARGET_ALPHA_SHOW; |
| 703 | 100 | | gameObject.SetActive(true); |
| 703 | 101 | | if (instant) |
| 703 | 102 | | alpha = TARGET_ALPHA_SHOW; |
| 703 | 103 | | } |
| | 104 | |
|
| | 105 | | public void Hide(bool instant = false) |
| | 106 | | { |
| 3 | 107 | | targetAlpha = TARGET_ALPHA_HIDE; |
| 3 | 108 | | if (instant && !forceShow) |
| | 109 | | { |
| 3 | 110 | | alpha = TARGET_ALPHA_HIDE; |
| 3 | 111 | | gameObject.SetActive(false); |
| | 112 | | } |
| 3 | 113 | | } |
| | 114 | | public void SetForceShow(bool forceShow) |
| | 115 | | { |
| 0 | 116 | | canvas.enabled = forceShow || namesVisible.Get(); |
| 0 | 117 | | canvas.sortingOrder = forceShow ? FORCE_CANVAS_SORTING_ORDER : DEFAULT_CANVAS_SORTING_ORDER; |
| 0 | 118 | | background.color = new Color(backgroundOriginalColor.r, backgroundOriginalColor.g, backgroundOriginalColor.b, fo |
| 0 | 119 | | this.forceShow = forceShow; |
| 0 | 120 | | if (this.forceShow) |
| 0 | 121 | | gameObject.SetActive(true); |
| 0 | 122 | | } |
| | 123 | |
|
| 0 | 124 | | public void SetIsTalking(bool talking) { talkingAnimator.SetBool(TALKING_ANIMATOR_BOOL, talking); } |
| | 125 | |
|
| 0 | 126 | | public void SetYOffset(float yOffset) { transform.localPosition = Vector3.up * yOffset; } |
| | 127 | |
|
| | 128 | | public Rect ScreenSpaceRect(Camera mainCamera) |
| | 129 | | { |
| 0 | 130 | | Vector3 origin = mainCamera.WorldToScreenPoint(background.transform.position); |
| 0 | 131 | | Vector2 size = background.rectTransform.sizeDelta; |
| 0 | 132 | | return new Rect(origin.x, Screen.height - origin.y, size.x, size.y); |
| | 133 | | } |
| | 134 | |
|
| 0 | 135 | | public Vector3 ScreenSpacePos(Camera mainCamera) { return mainCamera.WorldToScreenPoint(background.transform.positio |
| | 136 | |
|
| | 137 | | internal static float ResolveAlphaByDistance(float alphaValue, float distanceToCamera, bool forceShow) |
| | 138 | | { |
| 2 | 139 | | if (forceShow) |
| 0 | 140 | | return alphaValue; |
| | 141 | |
|
| | 142 | | const float MIN_DISTANCE = 5; |
| 2 | 143 | | if (distanceToCamera < MIN_DISTANCE) |
| 2 | 144 | | return alphaValue; |
| | 145 | |
|
| 0 | 146 | | return alphaValue * Mathf.Lerp(1, 0, (distanceToCamera - MIN_DISTANCE) / (DataStore.i.avatarsLOD.LODDistance.Get |
| | 147 | | } |
| | 148 | |
|
| 6 | 149 | | internal void UpdateVisuals(float resolvedAlpha) { canvasGroup.alpha = resolvedAlpha; } |
| | 150 | |
|
| 4 | 151 | | internal void ScalePivotByDistance(float distanceToCamera) { pivot.transform.localScale = Vector3.one * 0.15f * dist |
| | 152 | |
|
| | 153 | | internal float GetPivotYOffsetByDistance(float distanceToCamera) |
| | 154 | | { |
| | 155 | | const float NEAR_Y_OFFSET = 0f; |
| | 156 | | const float FAR_Y_OFFSET = 0.1f; |
| | 157 | | const float MAX_DISTANCE = 5; |
| 2 | 158 | | if (distanceToCamera >= MAX_DISTANCE) |
| 0 | 159 | | return FAR_Y_OFFSET; |
| | 160 | |
|
| 2 | 161 | | return Mathf.Lerp(NEAR_Y_OFFSET, FAR_Y_OFFSET, distanceToCamera / MAX_DISTANCE); |
| | 162 | | } |
| | 163 | |
|
| | 164 | | private void OnDestroy() |
| | 165 | | { |
| 703 | 166 | | namesVisible.OnChange -= OnNamesVisibleChanged; |
| 703 | 167 | | namesOpacity.OnChange -= OnNamesOpacityChanged; |
| 703 | 168 | | } |
| | 169 | | } |